<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3. DECLARE cipherkeylength
  4. DECLARE cipherkeytext
  5. DECLARE plaintext
  6. DECLARE plaintextcopy
  7. DECLARE N
  8. GET cipher length
  9.   IF cipher length not between 2 to 16
  10.     THEN
  11.         Enable ERROR
  12. END IF       
  13. GET cipher text
  14.   IF number of characters in cipher text not between 2 to 16
  15.     THEN
  16.         Enable ERROR
  17. END IF
  18. IF capital letter found in cipher text
  19.     THEN
  20.         Enable ERROR
  21. END IF
  22. IF any duplicates found in cipher text
  23.     THEN
  24.         Enable ERROR
  25. END IF       
  26. GET length of plain text
  27. IF characters in plain text length not between 4 to 256
  28.     THEN
  29.         Enable ERROR
  30. END IF
  31. GET plain text to be encrypted
  32. IF plain text characters not betwwen 4 to 256
  33.      THEN
  34.          Enable ERROR
  35. END IF
  36. IF ERROR is enabled
  37.      THEN
  38.          SHOW ERROR MESSAGE
  39. END IF         
  40.  
  41. IF no ERROR
  42.      THEN
  43.         The first character found in the cipher key as the target value
  44.         The second character found in the cipher key as the substitution value
  45. END IF
  46.      THEN
  47.          Get the first character which is the target value in the cipher key text and compare with the characters the plain text
  48. END IF         
  49. IF the target value in the cipher key text found in the plain text
  50.      THEN
  51.          Replace the target value found in plain text with the substitution value in the cipher key text
  52.          Display the step number and the target value with the replaced substition value
  53.          Display the finished encrypted text
  54.          Repeat steps until the end of the cipher key
  55. END IF 
  56. END 
  57.   */
  58.  
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <ctype.h>
  63.  
  64. void main(void)
  65.  
  66. {
  67. int i,a;
  68. int N=0;
  69. int error=0;
  70. int cipherkeylength;
  71. char cipherkeytext[16];
  72. int plaintextlength;
  73. char plaintext[256];
  74. char plaintextcopy[256];
  75. int e;
  76.  
  77. /*GET ALL INPUTS FOR CIPHER WITH ERRORS */
  78.  
  79. /*--------------------INPUTS FOR CIPHER LENGTH---------------------------*/
  80.  
  81. scanf("%d", &cipherkeylength);                 //get input for length of cipher key
  82.  
  83.  
  84.     if((cipherkeylength < 2) || (cipherkeylength > 16)){  // checks inputs should be in between 2 to 16
  85.         printf("Error, length of the  cipher key must be between 2 to 16\n"); // show error message
  86.         error=1;
  87.                                                        }
  88.                                                    
  89.  
  90. /*----------------------INPUT FOR CIPHER TEXT-----------------------------------------------------------------------------*/
  91.  
  92. scanf("%s", &cipherkeytext);     // get input for cipher key text
  93.     for(a = 0; a < cipherkeylength; a++) {  // for loop for checking for letters
  94.         if (isdigit(cipherkeytext[a])) {   // check inputs to see if it is decimal digit.
  95.             printf("Error, cipher key text should only be LETTERS\n"); // shows error message if condition true
  96.             error=1;
  97.                                        }
  98.                                          
  99.                                          
  100.         if (isupper(cipherkeytext[a])) {  // check if inputs is in uppercase.
  101.             printf("Error, cipher key text should only be in LOWER CASE\n")// shows error message if condition true
  102.              error =1;
  103.                                        }
  104.                                            }
  105.          if ( cipherkeylength != strlen(cipherkeytext)){        // check if cipher key and cipher key text not equal
  106.             printf("Error, cipher key must be equal to cipher text\n");
  107.             error=1;                                   }                                     
  108.        
  109. /*----------------------INPUT FOR PLAIN TEXT LENGTH--------------------------------------------------------------------------------- */
  110.  
  111. scanf("%d", &plaintextlength);
  112.  
  113.        
  114.        if((plaintextlength < 4) || (plaintextlength > 256)){  // checks inputs should be in between 4 to 256
  115.         printf("Error, length of the  cipher key must be between 4 to 256\n"); // show error message if condition true.
  116.         error=1;
  117.                                                            }                                       
  118.  
  119.  
  120. /*-----------------------INPUT FOR PLAINTEXT---------------------------------------------------------------*/
  121. scanf("\n%[^\n]",&plaintext)// get input for plain text
  122.  
  123.         for(a = 0; a < plaintextlength; a++) { // for loop for checking for alphabets
  124.         if (isdigit(plaintext[a])) {   // // check inputs to see if it is decimal digit.
  125.             printf("Error, plain text should be LETTERS\n")// Show error message when condition true.
  126.             error=1;
  127.                                    }
  128.         if (isupper(plaintext[a])) {    // check if inputs is in uppercase.
  129.             printf("Error,plain text should be LOWERCASE\n")// show error message when condition true.
  130.             error=1;
  131.                                    }
  132.                                              }
  133.         if ( plaintextlength != strlen(plaintext)) {error=1;    // checks if plaintext length equal plaintext letters
  134.             printf("ERROR, plain text length should be equal to number of plain text characters\n");}                                   
  135.  
  136. strncpy(plaintextcopy, plaintext, 256);    // Copy characters from  plaintext string.
  137.  
  138. if(error==1){ return 0;}  // Terminate program if error
  139.  
  140.  
  141. /* -----------------------ENCRYPTION/SUBSTITUTION PROCESS------------------------------ */
  142.  
  143.       for(N=0; N < cipherkeylength; N++){  //  for loop to range less than cipher key length
  144.  
  145.         for(i=0; i < plaintextlength; i++){  // for loop in the range less than plain text length
  146.  
  147.  
  148.             if(plaintext[i] == cipherkeytext[N]){ // checking for target value in plain text
  149.  
  150.  
  151.                 plaintextcopy[i] = cipherkeytext[N + 1]; // replace target value with substitution value in plaintext copy
  152.                                                 }
  153.                                            }
  154.         e= N + 1;                               // substitution steps
  155.  
  156. /*------------------------DISPLAY OUTPUT---------------------------------------------------- */
  157.  
  158.         cipherkeytext[cipherkeylength] =cipherkeytext[0];
  159.         printf("\nStep %d substitute %c with %c", e, cipherkeytext[N], cipherkeytext[N + 1]);
  160.         printf("\n%s", plaintextcopy);       //   Show final encrypted text for plaintextcopy
  161.                                           }
  162.    
  163.   }