<mosaic.cnfolio.com>
PROGRAM
  1. /* Write a C program to implement a substitution cipher to encrypt plain text:
  2. • Select a string of characters to use as the cipher key, which must be 2 to 16 characters in
  3. length and contains only lower case characters of the alphabet (a-z) without any duplicates
  4. • Select a string of plain text to encrypt, which must be 4 to 256 characters in length and
  5. contains only lower case characters of the alphabet (a-z) and spaces
  6. • Use N to indicate the current position in the cipher key, where the starting position (first
  7. character) is indicated by N equal to 1
  8. • Perform the encryption by implementing these steps from the start of the cipher key:
  9. 1) The character found at position N in the cipher key is the target value
  10. 2) The character found at position N+1 in the cipher key is the substitution value; in the
  11. situation that N is the last character in the cipher key, then, the substitution value is
  12. the first character of the cipher key
  13. 3) In the plain text, replace all instances of the target value with the substitution value
  14. 4) Increase N by one and repeat all steps until the end of the cipher key
  15. The program must read the following input:
  16. • 1
  17. st line of input is a number indicating the length of the cipher key
  18. • 2
  19. nd line of input is the cipher key text
  20. • 3
  21. rd line of input is a number indicating the length of the plain text
  22. • 4
  23. th line of input is the plain text to be encrypted
  24. The program must display the following output:
  25. • The number of each step, e.g. if the length of the cipher key is 4, then, the output must
  26. display substitution steps 1, 2, 3 and 4
  27. • The target value and the substitution value at each step
  28. • The encrypted text at each substitution step
  29.  
  30. PSEUDOCODE
  31.  
  32. OPERATION_STATUS SimpleSubstitutionCipher_Code(char* originalAlphabet,
  33.                                                char* codedAlphabet,
  34.                                                char* originalMessage,
  35.                                                char* encodedMessage)                                               
  36. {
  37. unsigned short alphabetLength = strlen(originalAlphabet);
  38. unsigned int messageLength    = strlen(originalMessage);
  39. unsigned int i = 0U;
  40. char* pointer = NULL;
  41. unsigned int position = 0U;
  42. if( alphabetLength != strlen(codedAlphabet) )
  43. {
  44.   //The lengths of the alphabets do not match
  45.   return OPERATION_FAILED;
  46. }
  47. for(i=0; i<messageLength; i++)
  48. {
  49.   pointer  = strchr(originalAlphabet, originalMessage[i]);
  50.   if(pointer==NULL)
  51.   {
  52. //A character in the message was not found in the
  53.    //original alphabet
  54.    return OPERATION_FAILED;
  55.   }
  56. else
  57.   {
  58.    position = pointer - originalAlphabet;
  59.    encodedMessage[i] = codedAlphabet[position];
  60.   }
  61. }
  62. encodedMessage[messageLength] = '\0';
  63. return OPERATION_SUCCESS;
  64. }
  65.  
  66.  
  67.  
  68. */
  69.  
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72.  
  73. #define MAX_CIPHER_SIZE 16 /* defines the maximum size of the cipher */
  74. #define MAX_TEXT_SIZE 256 /* defines the maximum size of the text to be encrypted by the cipher */
  75.  
  76. int main()
  77. {
  78.     char text[MAX_TEXT_SIZE+1]={0}, cipher[MAX_CIPHER_SIZE+1]={0};
  79.     int ciphercount, textcount,i=0,j=0;
  80.     printf("Enter size of Cipher!\n");
  81.     scanf(" %d", &ciphercount);
  82.    
  83.     if(ciphercount>=2){
  84.     printf("Enter the Cipher!\n");
  85.     /*loop through and add*/
  86.     scanf("%s", cipher); /* scans the string */
  87.     cipher[ciphercount]='\0';
  88.     }else{
  89.     printf("Start again / Cipher size should be greater or equal to 2\n");
  90.     main();
  91.     }
  92.     printf("Enter the size of the plain text\n"); /* ask user to enter number of characters for the plain text */
  93.     scanf("%d", &textcount);
  94.     if(textcount>=4){
  95.     printf("Enter the Text!\n");
  96.      scanf("%s", text);
  97.      text[textcount]='\0';
  98.      }else{
  99.      printf("Start again / Text should be greater or equal to 4\n");
  100.      main();
  101.      }
  102.      //loop and replace
  103.      for(i=0;cipher[i]!='\0';i++){
  104.      for(j=0;text[j]!='\0';j++){
  105.      if((cipher[i] == text[j]))
  106.      {
  107.                     //replace
  108.                     if(cipher[++i] == '\0'){
  109.                     //passed
  110.                     text[j] = cipher[0];
  111.                     }else{
  112.                      text[j] = cipher[i++];
  113.                      }
  114.                    
  115.             }
  116.         }
  117.     }
  118.     printf("Our ciphered text is: %s\n",text);
  119.     return 0;
  120. }