<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.  
  4. Get the number of cipher key from the input
  5. Get the cipher key characters from the input
  6. Get the number of plain text from the input
  7. Get the plain text characters from the input
  8.  
  9. IF
  10.      the size of cipher key is between 2 and 16 and the size of the plain text is between 4 and 256
  11.      THEN
  12.      Get the first position of cipher key and apply is as N is equal to one
  13.        
  14.      //Start assinging the characters of Target value and Substitution value using the following steps://
  15.  
  16.          Set the first cipher key position to be a target value
  17.          Set the next cipher key position to be a substitution value
  18.       IF
  19.       the position of the cipher key is less than the string length
  20.       THEN
  21.       The Substitution value is the next character after the Target value
  22.       ELSE
  23.       IF
  24.       the position of cipher key is the last letter
  25.     THEN
  26.       the next cipher key position is the first cipher key in the string
  27.      
  28.     END IF
  29.    
  30.     //Start encryption using the following steps://
  31.    
  32.     Check whether the target value exists in the plain text
  33.     IF
  34.        Target value exists in the plain text
  35.     THEN
  36.         replace the target value from the plain text with the subsitution value from the cipher key
  37.         Increase N everytime and repeat the steps until cipher keys are over
  38.     END IF
  39.  
  40. ELSE
  41. Display an error that asks to check the inputs.
  42. END IF
  43. */
  44.  
  45.  
  46. #include <string.h>
  47. #include <stdio.h>
  48.  
  49. int main(void)
  50.  
  51. {
  52.      char PlainTextString[256]; /* The plain text from the input limited between 4 and 256*/
  53.      char EncryptedPlainText[256];/* copied verison of the plain text from the input */
  54.      int N;/* The position of the current the cipher key */
  55.      int s;  /* variable to use as N but at the encryption process */
  56.      int Target_value;/* The character found at position of N, starts at N=1 */
  57.      int Substitution_value;/* The character found at N+1 in the cipher key string */
  58.      int cipher_key;/* The number of cipher key used*/
  59.      char CipherKeyString[16];/* The cipher key string, limited between 2 and 16 */
  60.      int plain_text;/* The number of the Plain text characters */
  61.      int Encrypted_letter;/* Checking if Target value is in the plain text*/
  62.      
  63.      
  64.       scanf("%d", &cipher_key); /* Read the number of cipher key characters and assign it to cipher_key  */
  65.       scanf (" %[a-z]c", &CipherKeyString); /* Read the Cipher key characters, limiting them between a to z low cases*/
  66.       scanf("%d", &plain_text); /* Read the number of Plain text characters and assign it to the plain_text */
  67.       scanf(" %[^\t\n][a-z]c",&PlainTextString); /*Read the Plain text characters, limited with a to z low cases, including all spaces in the tab until the next line */
  68.       strcpy(EncryptedPlainText,PlainTextString); /* copy the original plain text to the EncryptedPlainText */
  69.       if (cipher_key <=16 && cipher_key>=2 && cipher_key==strlen(CipherKeyString)&&
  70.       (plain_text <=256 && plain_text>=4 && plain_text==strlen(PlainTextString)))
  71.       /* Limit the cipher key 2 to 16 and plain text to 4 to 256, otherwise show error message*/
  72.       {
  73.            
  74.            for(N=1; (N<= strlen(CipherKeyString)) ; N++) /* loop for every position of N, starting at first character N=1, until all characters are covered */
  75.          {   
  76.          
  77.             if (N<strlen(CipherKeyString)) /* When the position of the current Target is not yet the last chartacter of the cipher key*/
  78.             {
  79.             Target_value= CipherKeyString[N-1]; /* Target value is the first character in cipher key string*/
  80.             Substitution_value= CipherKeyString[N]/* Substitution is the next character after the Target value*/   
  81.             }
  82.             else
  83.             if (N==strlen(CipherKeyString)) /* When the position of the current Target value is the last character */
  84.             {
  85.             Target_value=CipherKeyString[N-1]; /*  Target value is the last character of the cipher key*/
  86.             Substitution_value=CipherKeyString[0]/* Substitution value goes back to the first character in cipher key string */     
  87.             } 
  88.            
  89.             for(s=0; (s<= strlen(PlainTextString)) ; s++)
  90.    
  91.             {
  92.              Encrypted_letter=(strchr(PlainTextString, Target_value)); /* Check if the Target value is in the plain text string */
  93.    
  94.             if (PlainTextString[s]== Target_value )
  95.            
  96.              {
  97.             EncryptedPlainText[s]=Substitution_value; /* Replace the substituion value with the Target value from the plain text */
  98.              }
  99.              
  100.              
  101.             }
  102.            
  103.              
  104.              printf("step %d: substitute Target value %c with Substitution value %c \n",(strlen(CipherKeyString)-cipher_key+N),Target_value,Substitution_value);
  105.              /* print each step of the substitution, showing both Target and substitution values, until all cipher keys are covered*/
  106.              printf("%s \n", EncryptedPlainText); /* print the encrypted plain text*/
  107.                  
  108.              
  109.              }
  110.  
  111.        
  112.       }
  113.       else
  114.        {
  115.        printf("check the length of plain text and cipher key! \n");
  116.        }
  117. }