<mosaic.cnfolio.com>
PROGRAM
  1. /*----------------------------------------------PSEUDO CODE---------------------------------------------
  2.  
  3. START
  4.  
  5.     IMPORT string library
  6.  
  7.     VARIABLE Length of Cipher Key, Length of Plain Text
  8.     VARIABLE Cipher Key Text[array], Plain Text[array], Encrypted Text[array]
  9.     VARIABLE ERRORFLAGNUM, ERRORFLAGCHAR, X, Y, A, B, LETTERSWAP
  10.  
  11. ------------------------------1st line of input and validation - length of the cipher key------------------------------
  12.  
  13.     INPUT Length of Cipher Key
  14.  
  15.     IF Length of Cipher Key < 2 OR >16
  16.         OUTPUT Error Message
  17.     END IF
  18.  
  19. -----------------------------------2nd line of input and validation - cipher key text----------------------------------
  20.  
  21.     INPUT Cipher Key Text[array]
  22.  
  23.     IF Length of Cipher Key DOES NOT EQUAL number of letters in Cipher Key Text[array]
  24.         OUTPUT Error Message
  25.     END IF
  26.  
  27.     FOR LOOP to check each letter in Cipher Key Text[array]
  28.         IF letter < a OR > z
  29.             OUTPUT Error Message
  30.         END IF
  31.         FOR LOOP to check next letter in Cipher Key Text[array]
  32.           IF letter from first loop is equal to letter in this loop
  33.                OUTPUT Error Message
  34.             END IF
  35.         END LOOP
  36.     END LOOP
  37.  
  38. ------------------------------3rd line of input and validation - length of the plain text------------------------------
  39.  
  40.     INPUT Length of Plain Text
  41.  
  42.     IF Length of Plain Text < 4 OR > 256
  43.         OUTPUT Error Message
  44.     END IF
  45.  
  46. -------------------------------------4th line of input and validation - plain text-------------------------------------
  47.  
  48.     INPUT Plain Text[array]
  49.  
  50.     IF Length of Plain Text DOES NOT EQUAL number of characters in Plain Text[array]
  51.         OUTPUT Error Message
  52.     END IF
  53.  
  54.     FOR LOOP to check each character in Plain Text[array]
  55.         IF letter < a OR > z AND NOT a space
  56.             OUTPUT Error Message
  57.         END IF
  58.     END LOOP
  59.  
  60. -------------------------------------Encryption of plain text using cipher key text------------------------------------
  61.  
  62.     Copy contents of Plain Text[array] into Encrypted Text[array]
  63.  
  64.     FOR LOOP to check each letter in Cipher Key Text[array] and identify what letter will be used to encrypt that letter from the Plain Text[array] into the Encrypted Text[array]
  65.         IF letter in last position in Cipher Key Text[array]
  66.             SET LETTERSWAP for that letter to the first letter in Cipher Key Text[array]
  67.         ELSE
  68.             SET LETTERSWAP for that letter to the next letter in Cipher Key Text[array]
  69.         END
  70.     END LOOP
  71.  
  72.     OUTPUT Confirmation of each letter to be swapped
  73.  
  74.     FOR LOOP to check each letter in Plain Text[array] and then update Encrypted Text[array]
  75.         IF letter in Plain Text[array] is EQUAL TO letter in Cipher Key Text[array]
  76.             SET Encrypted Text[array] for that letter EQUAL TO LETTERSWAP for that letter from Cipher Key Text[array]
  77.         END IF
  78.     END LOOP
  79.  
  80.     OUTPUT Encrypted Text[array]
  81.  
  82. END
  83.  
  84. ----------------------------------------------------END PSEUDO CODE----------------------------------------------------
  85.  
  86.  
  87. /*-----------------------------------------------------MAIN PROGRAM--------------------------------------------------*/
  88.  
  89. #include <stdio.h>
  90. #include <string.h> /*Imports the string library to be able to use the length and copy functions*/
  91.  
  92. int len_cipher_key, len_plain_txt, swap; /*Variables to store the lengths of the cipher key and the plain text*/
  93. int x, y, z, a, b, c, error1=0, error2=0; /*Temporary variables used for loops and errors*/
  94. char cipher[16], plaintxt[256]; /*Arrays to store the cipher key text and the plain text*/
  95. char errorchar1, errorchar2; /*Temporary variable used for errors*/
  96.  
  97. int main (void)
  98. /*----------------------------1st line of input and validation - length of the cipher key----------------------------*/
  99.  
  100. {
  101.     scanf("%d", &len_cipher_key); /*Gets the length of the cipher key*/
  102.     if(len_cipher_key<2) /*Displays error message if the cipher key is too short*/
  103.     {
  104.         printf("Erroneous input for length of the cipher key. Input is too small, please enter an integer between 2 and 16.\nYou inserted: %d.", len_cipher_key);
  105.     }
  106.     else if(len_cipher_key>16) /*Displays error message if the cipher key is too long*/
  107.     {
  108.         printf("Erroneous input for length of the cipher key. Input is too large, please enter an integer between 2 and 16.\nYou inserted: %d.", len_cipher_key);
  109.     }
  110.  
  111. /*---------------------------------2nd line of input and validation - cipher key text--------------------------------*/
  112.  
  113.     else
  114.     {
  115.         scanf("%s", cipher); /*Gets the cipher text and puts all the letters in the array cipher*/
  116.         if(strlen(cipher)!=len_cipher_key) /*Displays error message if an incorrect number of letters is entered*/
  117.         {
  118.             printf("Erroneous input for cipher key text.\nIncorrect number of letters entered.\nOnly lower case letters between a and z are valid.\nCipher key text input should have been %d letters.", len_cipher_key);
  119.         }
  120.         else
  121.         {
  122.             for(x=0; x<len_cipher_key; x++) /*Checks each letter in the array cipher*/
  123.             {
  124.                 if(cipher[x]<'a' || cipher[x]>'z') /*Displays error message if a letter is entered that is not between a and z*/
  125.                 {
  126.                     ++error1;
  127.                     errorchar1=cipher[x];
  128.                 }
  129.                 for(y=x+1; y<=len_cipher_key; y++) /*Checks the next letter to the above letter in the array cipher*/
  130.                 {
  131.                     if(cipher[x]==cipher[y]) /*Displays error message is there are more than 2 of the same letter*/
  132.                     {
  133.                         ++error2;
  134.                         errorchar2=cipher[x];
  135.                     }
  136.                 }
  137.             }
  138.             if(error1!=0)
  139.             {
  140.                 printf("Erroneous input for cipher key text.\nOnly lower case letters between a and z are valid.\nIncorrect letter input: %c", errorchar1);
  141.             }
  142.             else if(error2!=0)
  143.             {
  144.                 printf("Erroneous input for cipher key text.\nCannot have more than one of the same letter.\nDuplicate letter input: %c", errorchar2);
  145.             }
  146.  
  147. /*----------------------------3rd line of input and validation - length of the plain text----------------------------*/
  148.  
  149.             else
  150.             {
  151.                 scanf("%d ", &len_plain_txt); /*Gets the length of the plain text*/
  152.                 if(len_plain_txt<4) /*Displays error message if the plain text is too short*/
  153.                 {
  154.                     printf("Erroneous input for length of the plain text. Input is too small, please enter an integer between 4 and 256.\nYou inserted: %d.", len_plain_txt);
  155.                 }
  156.                 else if(len_plain_txt>256) /*Displays error message if the plain text is too long*/
  157.                 {
  158.                     printf("Erroneous input for length of the plain text. Input is too large, please enter an integer between 4 and 256.\nYou inserted: %d.", len_plain_txt);
  159.                 }
  160.  
  161.  
  162.  
  163.  
  164. /*-----------------------------------4th line of input and validation - plain text-----------------------------------*/
  165.  
  166.                 else
  167.                 {
  168.                     scanf("%[^\n]s", plaintxt); /*Gets the plain text and puts all the characters in the array plaintxt*/
  169.                     if(strlen(plaintxt)!=len_plain_txt) /*Displays error message if an incorrect number of characters is entered*/
  170.                     {
  171.                     printf("Erroneous input for plain text.\nIncorrect number of characters entered.\nLower case letters between a and z and spaces are valid.\nPlain text input should have been %d characters.", len_plain_txt);
  172.                     }
  173.                     else
  174.                     {
  175.                         for(y=0; z<len_plain_txt; z++) /*Checks each letter in the array plaintxt*/
  176.                         {
  177.                             if((plaintxt[z]<97 || plaintxt[z]>122) && plaintxt[z]!=32) /*Displays error message if a character is entered that is not between a and z and it is not a space*/
  178.                             {
  179.                                 ++error1;
  180.                                 errorchar1=plaintxt[z];
  181.                             }
  182.                         }
  183.                         if(error1!=0)
  184.                         {
  185.                             printf("Erroneous input for plain text.\nLower case letters between a and z and spaces are valid.\nIncorrect character input: %c", errorchar1);
  186.                         }
  187.  
  188. /*-----------------------------------Encryption of plain text using cipher key text----------------------------------*/
  189.  
  190.                         else
  191.                         {
  192.                             char encrypted[len_plain_txt]; /*Array to store encrypted text*/
  193.                             strcpy(encrypted, plaintxt); /*Copies the contents of the array plaintxt into the array encrypted*/
  194.                             for(a=0; a<len_cipher_key; a++) /*Check each letter in the array encrypted*/
  195.                             {
  196.                                 if(a==len_cipher_key-1) /*If looking at last position in the array cipher*/
  197.                                 {
  198.                                     swap=a-a; /*Updates variable swap to be the first letter in the array cipher*/
  199.                                 }
  200.                                 else
  201.                                 {
  202.                                     swap=a+1; /*Updates variable swap to be the next letter in the array cipher*/
  203.                                 }
  204.                                 printf("\nStep %d substitute %c with %c\n", a+1, cipher[a], cipher[swap]); /*Displays which letters are going to be swapped*/
  205.                                 for(b=0; b<=len_plain_txt; b++) /*Check each letter in the array plaintxt*/
  206.                                 {
  207.                                     if(plaintxt[b]==cipher[a]) /*If letter in the array plaintxt is equal to the letter in the array cipher*/
  208.                                     {
  209.                                         encrypted[b] = cipher[swap]; /*Update the array encrypted for that letter equal to the variable swap for that letter from the array cipher*/
  210.                                     }
  211.                                 }
  212.                                 for(c=0; c<len_plain_txt; c++) /*Displays the array encrypted*/
  213.                                 {
  214.                                     printf("%c", encrypted[c]);
  215.                                 }
  216.                             }
  217.                         }
  218.  
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.     }
  224.     return(0); /*Ends the function*/
  225. }
  226.  
  227. /*-----------------------------------------------------END PROGRAM---------------------------------------------------*/