<mosaic.cnfolio.com>
PROGRAM
  1. /* ------------------------------------------*****PSEUDOCODE*****---------------------------------------------
  2.  
  3. DECLARE Variables int i, j, n, count, TextLength, CipherLength
  4. DELCARE Variables int subbed[255]
  5. DECLARE Variables char CipherKey[15], PlainText[255]
  6.  
  7. ==============================================================================================================
  8.  
  9. INPUT CipherLength
  10. INPUT CipherKey
  11. INPUT TextLength
  12. INPUT PlainText
  13.  
  14. =============================================================================================================
  15.  
  16. IF CipherLength NOT == StringLength(CipherKey)
  17.    THEN Display error message
  18.  
  19. ELSE IF NOT CipherLength( >=2 AND <= 16 )
  20.    THEN Display error message
  21. END
  22.  
  23. FOR LOOP
  24.    count = 0
  25.    FOR LOOP
  26.        IF CipherKey[Array1] == CipherKey[Array2]
  27.             THEN count = count + 1
  28.    END FOR LOOP
  29.    IF count >=2
  30.        THEN Display error message
  31.    ELSE IF NOT CipherKey[Array1] >= 'a' AND CipherKey[Array1] <= 'z'
  32.        THEN Display error message
  33.    END
  34. END FOR LOOP
  35.  
  36. ===========================================================================================================
  37.  
  38. IF TextLength NOT == StringLength(PlainText)
  39.    THEN Display error message
  40.  
  41. ELSE IF NOT TextLength( >=4 AND <= 256 )
  42.    THEN Display error message
  43. END
  44.  
  45. FOR LOOP
  46.    IF NOT PlainText[Array] == ' ' OR (PlainText[Array] >= 'a' AND PlainText[Array] <= 'z')
  47.         THEN Display error message
  48.    END
  49. END FOR LOOP
  50.  
  51. ==========================================================================================================
  52.  
  53. FOR LOOP
  54.    subbed[Array] = 0
  55. END FOR LOOP
  56.  
  57. FOR LOOP
  58.     FOR LOOP
  59.         IF PlainText[Array2] == CipherKey[Array1] AND subbed[Array2] == 0 AND NOT Array1 == CipherLength
  60.             PlainText[Array2] = CipherKey[Array1 + 1]
  61.             subbed[Array2] = 1
  62.         ELSE IF PlainText[Array2] == CipherKey[Array1] AND subbed[Array2] == 0 AND Array1 == CipherLength
  63.             PlainText[Array2] = CipherKey[0]
  64.             subbed[Array2] = 1
  65.         END
  66.     END FOR LOOP
  67.    
  68.     IF Array1 == CipherLength
  69.         OUTPUT "Step (Array1 + 1) substitute (CipherKey[Array1]) with (CipherKey[0])"
  70.         OUTPUT PlainText
  71.     ELSE
  72.         OUTPUT "Step (Array1 + 1) substitute (CipherKey[Array1]) with (CipherKey[Array2 + 1])"
  73.         OUTPUT PlainText
  74.     END
  75. END FOR LOOP
  76.  
  77.  
  78.  
  79. ENG PROGRAM
  80. */       
  81.        
  82.  
  83.  
  84.  
  85. #include <stdio.h>
  86. #include <string.h>
  87.  
  88. int main(void) {
  89.  
  90. //------------------------------------------------------*****DECLARING VARIABLES*****------------------------------------------------------------------------
  91.  
  92.    
  93.     int cipherLen, len1, txtLen, len2, i, j, count, n;
  94.     int subbed[255];
  95.     char cipherKey[15], Plaintxt[255];
  96.    
  97.  
  98. //---------------------------------------------------*****READING INPUT FROM THE USER*****-------------------------------------------------------------------
  99.  
  100.  
  101.     scanf("%d\n", & cipherLen);        // Inputs the length of the cipher key
  102.  
  103.     scanf("%s\n", & cipherKey);        // Inputs the actual cipher key that will be used to encrypt the plain text
  104.  
  105.     scanf("%d\n", & txtLen);           // Inputs length of the plain text
  106.  
  107.     scanf("%[^\n]%*c", Plaintxt);      // Inputs a string of text to be encrypted
  108.  
  109.  
  110. //---------------------------------------------------*****CIPHER KEY ERROR DETECTION*****--------------------------------------------------------------------
  111.  
  112.  
  113.  
  114.     len1 = strlen(cipherKey);          // Works out the actual length of the cipher key string
  115.     len2 = strlen(Plaintxt);           // Works out the actual length of the plain text string
  116.    
  117.  
  118.     if (!(len1 == cipherLen)) {        // Checks if the length of the cipher key matches with the number that was entered by the user.
  119.         printf("CIPHER KEY INPUT ERROR. Your ciphey key text length is '%d' and does not match the number that was entered.", len1);            //displays appropriate informative error message, addressing where the user went wrong.
  120.         return 0;                      // Exits the program as an error was detected.
  121.     } else if (!(cipherLen >= 2 && cipherLen <= 16)) {     // Checks if the cipher key length is within the limits.
  122.         printf("CIPHER KEY INPUT ERROR. The cipher key must be 2 to 16 characters in length. Your cipher key length was '%d'.", cipherLen);     //displays appropriate informative error message, addressing where the user went wrong.
  123.         return 0;                      // Exits the program as an error was detected.
  124.     }
  125.  
  126.  
  127.     for(i=0; i<len1; ++i) {            // Nested For loop to compare each element with every other elements inside the array for any character duplicates
  128.         count = 0;                     // Initializing count to 0 for every loop so that it is reset and prevents errors.
  129.         for(j=0; j<len1; ++j) {
  130.             if(cipherKey[i]==cipherKey[j]) {     // Compares the characters in different positions inside the array and checks if there is a duplicate.
  131.                 count++;               // If repetition is found then the count variable is incremented.
  132.             }
  133.         }
  134.         if((count >= 2) && (cipherKey[i] >= 'a' && cipherKey[i] <= 'z')) {        // The count for every character in the array would be 1. But if there is multiple duplicates then the count would be 2 or more. This statement catches the character that has been repeated.
  135.             printf("CIPHER KEY INPUT ERROR. The letter '%c' is repeated! The cipher key must not contain any duplicates.", cipherKey[i]);   //displays appropriate informative error message, addressing where the user went wrong.
  136.             return 0;                  // Exits the program as an error was detected.
  137.         } else if (!(cipherKey[i] >= 'a' && cipherKey[i] <= 'z')) {               // Checks if the character is not a lowercase alphabet.
  138.             printf("CIPHER KEY INPUT ERROR. '%c' is not valid! Please enter lowercase alphabets only.", cipherKey[i]);                      //displays appropriate informative error message, addressing where the user went wrong.
  139.             return 0;                  // Exits the program as an error was detected.
  140.         }
  141.     }
  142.  
  143.  
  144.  
  145. //---------------------------------------------------*****PLAIN TEXT ERROR DETECTION*****--------------------------------------------------------------------
  146.  
  147.  
  148.  
  149.     if (!(len2 == txtLen)) {           // Checks if the length of the plain text matches with the number that was entered by the user.
  150.         printf("PLAIN TEXT INPUT ERROR. Your plain text length is '%d' and does not match the number that was entered.", len2);             //displays appropriate informative error message, addressing where the user went wrong.
  151.         return 0;                      // Exits the program as an error was detected.
  152.     } else if (!(txtLen >= 4 && txtLen <= 256)) {   // Checks to see if the length of the plain text is within the limits.
  153.         printf("PLAIN TEXT INPUT ERROR. The plain text must be 4 to 256 characters in length. Your plain text length was '%d'.", txtLen);   //displays appropriate informative error message, addressing where the user went wrong.
  154.         return 0;                      // Exits the program as an error was detected.
  155.     }
  156.  
  157.  
  158.     for(i=0; i<len2; ++i) {            // Going through every elements inside the array.
  159.         if(!((Plaintxt[i]== ' ') || (Plaintxt[i] >= 'a' && Plaintxt[i] <= 'z'))){            // checking if the character in the array position is either lowercase alphabet or a space.
  160.             printf("PLAIN TEXT INPUT ERROR. '%c' is not valid. Please enter lower case alphabets only.", Plaintxt[i]);                      //displays appropriate informative error message, addressing where the user went wrong.
  161.             return 0;                  // Exits the program as an error was detected.
  162.         }
  163.     }
  164.  
  165.  
  166.  
  167. //----------------------------------------------------------*****ENCRYPTION*****----------------------------------------------------------------------------
  168.  
  169.  
  170.  
  171.         for (i= 0; i < txtLen; ++i) {         // Going through the elements inside the array to give it the value of 0. 0 meaning that the position is unsubstituted and 1 if the position has already been substituted.
  172.             subbed[i] = 0;                    // Initializing the elements inside the array to 0.
  173.         }
  174.  
  175.         for (n = 0; n < cipherLen; ++n) {     // Going through every character inside the cipher key array.
  176.  
  177.             for (i = 0; i < txtLen; ++i) {    // Going through every character inside the plain text array.
  178.  
  179.                 if ((Plaintxt[i] == cipherKey[n]) && (!(n == cipherLen - 1)) && (subbed[i] == 0)) {       // Checks if target character is inside the plain txt array and if that array position has already been subbed or not.
  180.                     Plaintxt[i] = cipherKey[n + 1];   // Substituting the character in the plain text's array position with the cipher key character.
  181.                     subbed[i] = 1;            // Initializing that array position's subbed value to 1 to make sure the character in this array position doesn't get substituted again
  182.  
  183.                 } else if ((Plaintxt[i] == cipherKey[n]) && (n == cipherLen - 1) && (subbed[i] == 0)) {   //  Checks if the character in the end of the array matches the target and if the array has already been subbed or not.
  184.                     Plaintxt[i] = cipherKey[0];       // Substitutes the character in the plain text's array position with the first letter in cipher key array.
  185.                     subbed[i] = 1;            // Initializing that array position's subbed value to 1 to make sure the character in this array position doesn't get substituted again.
  186.                 }
  187.  
  188.             }
  189.             if (n == cipherLen - 1) {         // Once the search has reached the end of the plain text array
  190.                 printf("Step %d substitute %c with %c\n", (n+1), cipherKey[n], cipherKey[0]);       // Outputs the last step number. Also substitutes the last character in the cipher key array with the first character in the same array.
  191.                 printf("%s\n", Plaintxt);     // Outputs the encrypted message.
  192.             } else {
  193.                 printf("Step %d substitute %c with %c\n", (n+1), cipherKey[n], cipherKey[n + 1]);   // Outputs the current step number and declares the current character it is searching for and what character it is substituting it with. (character in 'n' substitute with character in 'n+1')
  194.                 printf("%s\n", Plaintxt);      // Outputs the encrypted message.
  195.             }
  196.         }
  197.         return 0;   //End program.
  198.     }