<mosaic.cnfolio.com>
PROGRAM
  1. /* Psuedo code
  2.      
  3.     START
  4. ------------------Integer Variables----------------------------------     
  5.       DECLARE integer "keylen"
  6.          GET input for length
  7.          
  8.       DECLARE integer "textlen"
  9.          GET input for textlen
  10.          
  11.       DECLARE integer "target"
  12.          SET target to 0
  13.          
  14.       DECLARE integer "substitution"
  15.          SET substitution to 1
  16.          
  17.       DECLARE integer "counter"
  18.          SET counter to 0
  19.          
  20.       DECLARE integer "repeat"   
  21.          SET repeat to 0
  22.      
  23.       DECLARE integer "step"
  24.          
  25. ---------------------String declaration and string error checks-------------------------         
  26.       DECLARE Character String for crypto key
  27.          input for string length
  28.          input for the string contents
  29.      
  30.       IF crpyt key string length is greater than 16
  31.          THEN
  32.             DISPLAY given crypt key is too long
  33.             END program
  34.       END IF
  35.      
  36.       IF crpyt key string length is less than 2
  37.          THEN
  38.             DISPLAY given crypt key is too short
  39.             END program
  40.       END IF
  41.      
  42.       DECLARE Character String for crypto key
  43.          use keylen for string length
  44.          input for string contents
  45.      
  46.       IF plain text string length is greater than 256
  47.          THEN
  48.             DISPLAY given crypt key is too long
  49.             END program
  50.       END IF
  51.      
  52.       IF plain text string length is less than 4
  53.          THEN
  54.             DISPLAY given crypt key is too short
  55.             END program
  56.       END IF
  57. ---------------------------Substitution---------------------------------------     
  58.  
  59.       DECLARE char string "original"
  60.       COPY plain text into original string
  61.      
  62.       WHILE value at counter in plaintext does not equal the null value
  63.          
  64.          IF value at counter in plaintext is equal to value at target in crypto_key
  65.             THEN
  66.                value at counter in plaintext equals value at substitution in crypto_key
  67.       ENDIF
  68.       INCREASE counter by 1
  69.            
  70.       END WHILE   
  71.  
  72.       SET counter to 0
  73.       INCREASE step by 1
  74.       DISPLAY plain text string
  75.       DISPLAY "current step, substitute 'substitute value' with 'target value'"
  76.       INCREASE target by 1
  77.       INCREASE substitution by 1
  78.       INCREASE repeat by 1
  79.      
  80.       IF target is equal to keylen
  81.          THEN
  82.             SET target to 0
  83.       END IF   
  84.      
  85.       IF substitution is equal to keylen
  86.          THEN
  87.             SET substitution to 0
  88.       END IF   
  89.              
  90.       DECLARE char string "placeholder"
  91.       COPY original string into placeholder string 
  92.  
  93.       WHILE repeat is less than or equal to keylen-1
  94.  
  95.          WHILE value at counter in placeholder does not equal the null value
  96.          
  97.             IF value at counter in placeholder is equal to value at target in crypto_key
  98.                THEN
  99.                   value at counter in placeholder equals value at substitution in crypto_key
  100.             ENDIF
  101.             INCREASE counter by 1
  102.            
  103.          END WHILE
  104.  
  105.          SET counter to 0
  106.  
  107.          WHILE value at counter in placeholder does not equal the null value
  108.          
  109.             IF value at counter in placeholder is equal to value at substitute in crypto_key
  110.                THEN
  111.                   value at counter in plain text equals value at counter in placeholder
  112.             ENDIF
  113.             INCREASE counter by 1
  114.            
  115.          END WHILE
  116.  
  117.          SET counter to 0
  118.          INCREASE step by 1
  119.          DISPLAY plain text string
  120.          DISPLAY "current step, substitute 'substitute value' with 'target value'"
  121.          INCREASE target by 1
  122.          INCREASE substitution by 1
  123.          INCREASE repeat by 1
  124.      
  125.          IF target is equal to keylen
  126.             THEN
  127.                SET target to 0
  128.          END IF   
  129.      
  130.          IF substitution is equal to keylen
  131.             THEN
  132.                SET substitution to 0
  133.          END IF
  134.  
  135.          COPY original string into placeholder string     
  136.  
  137.       END WHILE
  138.      
  139.      
  140.     END*/
  141.  
  142.  
  143.  #include <stdio.h>
  144.  #include <string.h>
  145.  
  146.  int main()
  147.  {
  148. /*-------------------------------------- Integer/string declaration -------------------------------------*/
  149.  
  150.    /* Integers used because the number of letters will be a whole number */
  151.    /* Floats were not used because there are no decimal positions in an array */
  152.    int keylen;  /* key len is used as a shortened crypt key length */
  153.    int textlen;  /* text len is used as a shortened plain text length */
  154.    int target;  /* sets the target value in the crypt key array and the plain text array */
  155.    int substitute;  /* substitute is the value in the crypt key that will be placed in the crypt key */
  156.    int counter;  /* counter to check position in plain text array */
  157.    int repeat;  /* reapeat used as a full substitution loop counter */
  158.    int step;  /* used to count steps and will be printed to display */
  159.    target=0/* setting target value at postition 0 in array */
  160.    substitute=1/* setting substitute value at postition 1 in array */
  161.    counter=0/* use to count complete substitution loops */
  162.    step=0/* same as counter but with a different name to help identify amount of loops completed */
  163.    
  164.    scanf( "%d\n", &keylen )/* scans the first input as the crypt key length, \n to select new line */
  165.    keylen++;  /*adds a space for the null valuse after the input text is placed into the array*/
  166.    char crypt_key[keylen]/*array for the crypt key using keylen as the max value*/
  167.    fgets( crypt_key, keylen, stdin )/*using fgets so the entire line of input is read, input is the second line with the max value set with keylen*/
  168.    
  169.    scanf( "%d\n", &textlen )/* scans the first input as the plain text length, \n to select new line */
  170.    textlen++;  /*adds a space for the null valuse after the input text is placed into the array*/
  171.    char plain_text [textlen]/*array for the plain text using textlen as the max value*/
  172.    fgets( plain_text, textlen, stdin )/*using fgets so the entire line of input is read, input is the fourth line with the max value set with keylen*/
  173.    
  174. /*-------------------------------------------- Error checking --------------------------------------------*/
  175.    
  176.    if( keylen>17 )  /*key length must not be greater than 16, if used becasue of short complex statement*/
  177.    {
  178.      printf( "Error: Crypt key is too long" ); /* short but consice error message to ease fixing the error */
  179.      return 0; /* exits the code early if the key length is too long */
  180.    }
  181.    
  182.    if( textlen>257 )  /*text must not be longer than 256 characters*/
  183.    {
  184.      printf( "Error: Plain text is too long" ); /* short but consice error message to ease fixing the error */
  185.      return 0; /* exits the code early if the plain text is too long */
  186.    } 
  187.    
  188. /*------------------------------------------ Substitution code ------------------------------------------*/   
  189.  
  190.    char original[textlen]; /* "original" string to hold a clean copy of the plain text */
  191.    strcpy( original, plain_text ); /* copies the plain text into the "original" string */
  192.    
  193.    while( plain_text[counter] != '\0') /* '\0' was used to keep the counter going until the position in the string is at the null value */
  194.      {
  195.         if( plain_text[counter] == crypt_key[target] ) /* counter selects a value in the plaint text string and checks if it is the same as the target value*/
  196.         {                                              /* target has the same function of counter by selecting a value in the crypt key*/
  197.             plain_text[counter] =  crypt_key[substitute]/* when the value in plain text is the same as the target,, it is changed to the sub value*/
  198.         }
  199.         counter++; /* increase the counter to change the selected value in the plain text string */
  200.      }
  201.  
  202.    /* the above code is seperated from the code below because of overwrite issues if they were to be combined */
  203.  
  204.    step++;  /* increases step by one to show that a new step was completed in the following printf */
  205.    printf( "Step %d substitute %c with %c\n", step, crypt_key[target], crypt_key[substitute] ); /* used to show the current step as displayed in the coursework sheet */
  206.    printf( "Plain text: %s\n", plain_text)/* prints the newly substituted plain text to show whether any letters were changed */
  207.    counter=0/* resets the counter to be used in the next substitution */
  208.    target++;  /* increases the target value to go to the next value in the crypt key that will be targetted for substitution */
  209.    substitute++;  /* moves the substitution value to the next in the string */
  210.    repeat++;  /* repeat keeps track of how many times a full substitution cycle has occured */
  211.    
  212.    if( target==keylen-1 ) /* if the target value is equal to the last value that is not the null value then the statement is true */
  213.    {
  214.      target=0/* resets target to allow further substitution cycles when the last value in the key string has been reached */
  215.    }
  216.    if( substitute==keylen-1 )  /* if the substitution value is equal to the last value that is not the null value then the statement is true */
  217.    {
  218.      substitute=0/* resets substitution value to allow further substitution cycles when the last value in the key string has been reached */
  219.    }
  220.    
  221.    char placeholder[textlen]/* creates a new string to hold the target values in positions that will not overwrite previous substitutions in plain text */
  222.    strcpy( placeholder, original )/* copies original plain text into the placeholder string */
  223.    /* placeholder string will be used to hold the required substitutions in the correct places while also preventing overwrites of already substituted values*/
  224.    
  225.    while(repeat <= keylen-1)  /* runs until all values in the plain text string is met as a target to allow a full rotation of required substitution */
  226.    {
  227.      while(placeholder[counter] != '\0')  /* same as above but using the new placeholder string */
  228.      {
  229.          if(placeholder[counter] == crypt_key[target] ) 
  230.          {
  231.            placeholder[counter] =  crypt_key[substitute]/* inserts the substitute in the position that will be used in the following substitute code */
  232.          }   
  233.          else
  234.          {   
  235.            placeholder[counter] = '7'/* This placeholder value can be anything that is not in the plain text */
  236.          }
  237.        counter++;  /* increase the counter to change the selected value in the placeholder string */
  238.      }
  239.      
  240.    counter=0; /* resets the counter to be used in the next substitution */
  241.    
  242.    while(placeholder[counter] != '\0')
  243.    {
  244.       if(placeholder[counter] == crypt_key[substitute])  /* Because the placeholder string has been substituted already the sub values
  245.                                                             will be in the places that will not overwrite any previously substituted values*/
  246.       {
  247.           (plain_text[counter] = placeholder[counter])/* the substituted value will then replace the target value value in the counter value */
  248.       }
  249.    counter++; 
  250.    }
  251.    
  252.    step++; 
  253.    printf( "Step %d substitute %c with %c\n", step, crypt_key[target], crypt_key[substitute] )
  254.    printf( "Plain text: %s\n", plain_text )
  255.    target++;
  256.    substitute++;
  257.    repeat++;
  258.    counter=0;
  259.    
  260.    if( target==keylen-1 )
  261.    {
  262.       target=0;
  263.    }
  264.    
  265.    if( substitute==keylen-1 )
  266.    {
  267.       substitute=0;
  268.    }
  269.    
  270.    strcpy( placeholder, original )/* resets the place holder to be used in the next substitution loop */
  271.    }
  272.  }