<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2.  
  3. /*
  4.  
  5. get number of letters to be input for the cipher make sure its between 2 and 16
  6. loop until leters are read in, make sure all letters and not repeated
  7. repeat same method for plain text make sure its between 4 andd 256
  8. if not a lower case letter or a space then it is an invalid character so output error message. do for both key and plain text.
  9. sub CK(1) with CK(2) till all done then sub CK(n) with CK(1)
  10. set array related to the character if its been changed then ignore or skip this character
  11. output each step as it changes with the number of step and whats happening
  12.  
  13. */
  14.  
  15. int main( void ) {
  16.   char CipherKey[16];
  17.   char PlainText[255];
  18.   int ChangedAlready[255] = {0};
  19.   char  c;
  20.   int NumberOfCipher, NumberOfPT,i,j,k, LetterError;
  21.  
  22.   LetterError=0; /*If set to 1 then it wont execute the encryption*/
  23.   scanf("%d",&NumberOfCipher);
  24.  
  25.   if (NumberOfCipher>16 || NumberOfCipher<2){ /* makes sure length of the input is between 2 and 16*/
  26.     printf("The size of the cipher key is too big or too small");/**/
  27.   }
  28.   else{
  29.     for (i = 0; i < NumberOfCipher; i++)
  30.     {
  31.       scanf(" %c", &c);
  32.       if  (c>=97){
  33.         if (c<=122){ /*makes sure that only lower case letters have been inputted*/
  34.           CipherKey[i]=c;
  35.           printf("%c", CipherKey[i]);
  36.         }
  37.       }
  38.       else{
  39.         printf("%c is not a valid character for the cipher key", c);
  40.         LetterError=1;
  41.       }
  42.     }
  43.     for(i=0; i< NumberOfCipher; i++){
  44.       for (j=0; j< NumberOfCipher; j++){
  45.         if (i==j){ /*makes sure it doesnt check itself*/
  46.         }
  47.         else if (CipherKey[i]==CipherKey[j]){ /*Checks to see if any letters have been doubled up*/ 
  48.           LetterError=1;
  49.           printf("\n %c is a duplicate letter which can't happen with this cypher", CipherKey[i]);
  50.         }
  51.       }
  52.     }
  53.    
  54.     printf("\n");
  55.     scanf("%d",&NumberOfPT); /*used so the array doesnt read the enter as a character*/
  56.     if (NumberOfPT>256 || NumberOfPT<4){ /*makes sure the text is between 4 and 256 characters long*/
  57.       printf("The size of the plain text is too big or too small");
  58.     }
  59.     else{ 
  60.      
  61.       /* Reads the enter as a space so uses fake variable to read it */
  62.       scanf("%c", &c)
  63.       for (i = 0; i < NumberOfPT; i++){
  64.         scanf("%c", &c);
  65.         if  (c>=97){
  66.           if (c<=122){ /*makes sure the characters are all lower case*/
  67.             PlainText[i]=c;
  68.             printf("%c", PlainText[i]);
  69.           }
  70.         }
  71.         else if (c==32){ /*if the characters aren't lower case checks if they are a space*/
  72.           PlainText[i]=c;
  73.           printf("%c", PlainText[i]);
  74.         }
  75.         else{
  76.           LetterError=1;
  77.           printf("\n %c is not a valid character", c);
  78.         }
  79.        
  80.       }   
  81.       printf("\n");
  82.      
  83.       /*replace and set Changed array*/
  84.       if (LetterError==1){ /*stops the encryption part from running if there is an input error*/
  85.         printf("One or more of the letters entered were invalid to rectify this make sure the input for the cipher key and the plaintext is only a lower case letter or a space is accepted in the plain text");
  86.       }
  87.       else if(LetterError==0){
  88.         for (i = 0; i < NumberOfCipher; i++){
  89.           for (j = 0; j < NumberOfPT; j++){
  90.             if (ChangedAlready[j] == 0){ /*checks if the letter has been previously changed if so it skips the encryption*/
  91.               if (i == NumberOfCipher - 1) { /*if at the end of the cipher key array then it will loop back to the start*/
  92.                 if (PlainText[j] == CipherKey[i]){ /*if the letter to be changed is the same as the letter to be changed then it sets the plain texts letter to that in the array*/
  93.                   PlainText[j] = CipherKey[0];
  94.                   ChangedAlready[j] = 1;               
  95.                 }
  96.               }
  97.               else{
  98.                 if (PlainText[j] == CipherKey[i]){ /*This is the same as above only it swaps the letter with the next one in the cipher text rather than the starting letter*/
  99.                   PlainText[j] = CipherKey[ (i+1) ];
  100.                   ChangedAlready[j] = 1;               
  101.                 }             
  102.               }           
  103.             }         
  104.           }
  105.          
  106.          
  107.           if (i==NumberOfCipher-1){ /*displays the step with the conversion for when its at the end of the cipher key*/
  108.             printf("Step %d replace %c with %c \n", (i+1), CipherKey[i], CipherKey[0]);
  109.           }
  110.           else{ /*displays the step with the conversion*/
  111.             printf("Step %d replace %c with %c \n", (i+1), CipherKey[i], CipherKey[i+1]);
  112.           }
  113.           for (k = 0; k < NumberOfPT; k++){ /*displays the plain text after every position of the cipher key which shows how it changes each time*/
  114.             printf("%c", PlainText[k]);
  115.           }
  116.           printf("\n");
  117.          
  118.         }
  119.       }
  120.     }
  121.   }
  122. }