<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. PSEUDO CODE
  3. START
  4.   Read the length of the cipher key (2-16), the cipher key text and the length of the plain text  (4-256)
  5.   Initialize variable (array pos[256] with the value of zero)
  6.   Encrypt the message with the proposed method
  7. 1) The character found at position N in the cipher key is the target value
  8. 2) The character found at position N+1 in the cipher key is the substitution value; in the situation that N is the last character in the cipher key, then, the substitution value is the first character of the cipher key
  9. 3) In the plain text, replace all instances of the target value with the substitution value
  10. 4) Increase N by one and repeat all steps until the end of the cipher key
  11.   Print the encrypted message
  12. END
  13. )*/
  14.  
  15.  
  16. #include<stdio.h>
  17. int main()
  18. {
  19.     char cipher[16],message[256], ch, n_ch, cht;
  20.     int i, j, length1, length2,pos[256];
  21.    
  22.     do{
  23.        //printf("Enter the length of the cipher key (2-16):");
  24.        scanf("%d", &length1);    // length1 - the length of the cipher key
  25.     } while (length1<2 || length1>16);
  26.    
  27.     //printf("Please enter the cipher key text:");
  28.     scanf("%s",cipher);          // cipher - the cipher key text
  29.    
  30.     do{
  31.        //printf("Enter the length of the plain text (4-256):");
  32.        scanf("%d", &length2);    // length2 - the length of the plain text
  33.     } while (length2<4 || length2>256);
  34.  
  35.    // printf("Enter the plain text to be encrypted:");
  36.     i=0;
  37.  
  38.     scanf(" %[^\n]s",message);      // read the message - the plain text to be encrypted
  39.     for(i=0;i<256;i++)
  40.         pos[i]=0;                                 // initialize the array pos[256] with the zero value
  41.      
  42.     for(j=0;j<length1;j++){          // loop with the value of iterations to be the variable length1
  43. // (length of the  cipher key)
  44.  
  45.         ch = cipher[j];
  46.         if (j+1<length1)
  47.             n_ch=cipher[j+1];  //  the substitution value
  48.  
  49.         else
  50.             n_ch=cipher[0];                      //  the substitution value is the first character of the
  51.          // cipher key
  52.            
  53.         for(i = 0; i<length2; ++i)            // loop with the value of iterations to be the length2
  54.                   // (length of the plain text)
  55.             if (ch==message[i] && pos[i]==0){
  56.                  message[i]=n_ch;            // replace the value of the plain text at i position with the
  57.               // the new value of character
  58.                  pos[i]=1;                      // change the value of array pos at position i from zero to one
  59.             }
  60.                
  61. printf("Step %d substitute %c with %c\n",j+1,ch,n_ch)//print the
  62.                                       // substitute character with the new character
  63.         //printf("Encrypted message: %s\n",message);
  64.         printf("%s\n",message);        //  print the encrypted message
  65.     }       
  66.    return 0;
  67. }