<mosaic.cnfolio.com>
PROGRAM
  1. /*********************************PSEUDO CODE********************************/
  2. /*START*/
  3. /*The cipher key must be 2 to 16 characters*/
  4. /*The plain text must be 4- 256 characters*/
  5. /*INITIALIZE variable substituted[256] with zero*/
  6. /*POSITION N in the cipher key which is the target value*/
  7. /*POSITION N+1 in the cipher key which is the substitution vlaue;
  8. /*N is the last character in the cipher key*/
  9. /*Substitution vlaue is the first character of the cipher key*/
  10. /*REPLACE all instances of the target value with in the subsititution value*/
  11. /*INCREASE N by one and repeat all steps until the end of the cipher key*/
  12. /*Display number of steps, target vlaue/substitution value at each step and the encrypted text at each substitution step*/
  13. /*STOP*/
  14.  
  15.  
  16. /*********************************SOURCE CODE*******************************/
  17. #include <stdio.h>
  18.        
  19. /***************************************************************************/
  20.  
  21.     int main() {
  22.         int  i,j;             
  23.         char data[256];
  24.         char key[16];
  25.         char substituted[256];
  26.         char Sublength[10]; /*Scratch space for lengths*/
  27.        
  28.        
  29. /***************************************************************************/       
  30.         for(i=0;i<256;i++) {
  31.         substituted[i]='0';
  32.         }
  33.         /*Set all subsitiution make to '0'*/
  34. /***************************************************************************/       
  35.  
  36.         printf("\n What is the length of the key? \n");
  37.         int x;
  38.         gets(Sublength);/*Input the length as a string*/
  39.         x=atoi(Sublength); /*The atoi function converts string to an interger numerical representation*/
  40.  
  41.  
  42.         printf("\nEnter the key \n");
  43.         gets(key)/*key*/
  44.  
  45.         printf("\n What is the length of the plain text? \n");
  46.         int y;
  47.         gets(Sublength); /*Input the length as a string*/
  48.         y=atoi(Sublength);/*Convert to int*/
  49.    
  50.  
  51.         printf("\nEnter the alphabetical plain text in lowercase to be encrypted:\n");
  52.         gets(data)/*Plain text*/
  53.        
  54.          
  55. /***************************************************************************/       
  56.  for (j = 0; j < x; j++) {
  57.  /*This line of code is to prepare for the swap we are going to do*/
  58.  
  59.  
  60. /***************************************************************************/
  61.  
  62.             char char_from = key[j];
  63.             char char_to;
  64.                        
  65. /***************************************************************************/
  66.             if (j ==x-1)
  67.             /* This is to check if we are not wrapping off end of the key*/
  68.                 char_to = key[0];
  69.             else
  70.                 char_to = key[j+1];
  71.             for (i = 0; i < y; i++) {
  72.            
  73.              /*This is where the swap actually happens*/
  74.  
  75.                 if (data[i] == key[j] && (j != x - 1) && (substituted[i]=='0') ) {
  76.                     data[i] = key[j+1];
  77.                     substituted[i]='N' ;
  78.                 } else if (data[i] == key[j] && (j == x - 1)&&(substituted[i]=='0')) {
  79.                     data[i] = key[0];
  80.                     substituted[i] = 'N'; /* Set the subsitiution maker*/
  81.                 }
  82.  
  83.             }
  84.            
  85.                    
  86. /***************************************************************************/
  87.  
  88. /*Displays step description*/
  89.             printf("Step %d substitute %c with %c \n",j,char_from,char_to);
  90.             printf("Your cipher text is: %s\n", data); /*Cipher text*/
  91.         }
  92.  
  93.         return 0;
  94.     }