<mosaic.cnfolio.com>
PROGRAM
  1. /* START
  2.  
  3.   Select a string of characters to be used as a cipher key
  4.   Cipher key must be between 2 and 16 characters long
  5.   IF cipher key is lower than 2 or higher than 16
  6.     THEN
  7.          display error message "Cipher key must be between 2 and 16 characters long"
  8.   END IF
  9.   Cipher key must be comprised of lower case letters from a-z only without duplicates
  10.   IF cipher key includes numbers, symbols, upper case letters, or duplicates
  11.       THEN
  12.          display error message "Cipher must contain only lower case characters of the alphabet (a-z)without any duplicates
  13.   END IF
  14.  
  15.   Select a string of characters to be used as plain text
  16.   Plain text must be between 4 and 256 characters long
  17.     IF plain text is lower than 4 or higher than 256
  18.          THEN
  19.             display error message "Plain text must be between 4 and 256 characters long"
  20.       END IF
  21.   Plain text must be comprised of lower case letters from a-z only without duplicates
  22.   IF plain text includes numbers, symbols, upper case letters, or duplicates
  23.       THEN
  24.          display error message "Plain text must contain only lower case characters of the alphabet (a-z)without any duplicates
  25.   END IF
  26.    
  27.   Use N to indicate the current position in the cipher key
  28.   The starting position (first character) is indicated by N
  29.  
  30.   Find character in cipher key at location N=1
  31.   Set character in N=1 position from cipher text as the target
  32.   Find character in cipher key at location N+1
  33.   Set character in N+1 position from cipher text as the substitution value
  34.       IF The target character is in the last position of cipher key
  35.          THEN
  36.             set the character in position N=1 of cipher text as the substitution value
  37.       END IF
  38.      
  39.   Go to plain text
  40.   Swap all target value characters with substitution value
  41.   Display substitution step, target value, and substitution value
  42.   Display encrypted text
  43.   Increase N by 1 and repeat until end of cipher key
  44.  
  45.   END*/
  46.  
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50.  
  51. int cipherlen;
  52. char cipher [16] = {'\0'};
  53. int plaintextlen;
  54. char plaintext [256] = {'\0'};
  55. void SwappingData (char cipher[], char plaintext[]);
  56. int error=1;
  57. int i=3;
  58. int temp;
  59. int p;
  60. int c;
  61. int n;
  62.  
  63.  
  64. void main (void)
  65. {
  66. scanf("%d", &cipherlen); // Select Cipher Key & Plain text
  67. scanf("%s", &cipher);
  68. scanf("%d", &plaintextlen);
  69. scanf("%256c", plaintext);
  70.  
  71. while (error && i == 3)// Check 3 times
  72.    {error = 0; // Set error to 0
  73.  
  74.  
  75. if (strlen (cipher)<2 || strlen (cipher)>16)
  76.    {error=1;} // If cipher key is not between 2 and 16 characters, display error message
  77.    
  78. if(cipherlen!= strlen(cipher))
  79.    {error=1;} // If cipher length is not equal to the length of cipher key, display error message
  80.    
  81. if (plaintextlen<4 || plaintextlen>257 )
  82.    {error=1;} // If plain text is not between 4 and 256 characters, display error message
  83.    
  84. if (plaintextlen!= strlen(plaintext))
  85.    {error=1;} // If plain text length is not equal to the length of plain text, display error message
  86.      
  87.    for (p=0; p<plaintextlen+1; p++){ // Scan plaintext
  88. if( isupper(plaintext[p]) )
  89.    {error=1;} // If plain text contains capital letters, display error message
  90. if ( isblank(plaintext[p]))
  91. {error=0;} // If plain text does not contain a space, display error message
  92. if ( isdigit(plaintext[p]))
  93. {error=1;} //  If plain text contains a number, display error message
  94. }
  95.  
  96.     for (c=0; c<cipherlen; c++){ // Scan cipher key
  97. if( isupper(cipher[c]) )
  98.    {error=1;} // If cipher key contains capital letters, display error message
  99. if(isdigit(cipher[c]))
  100.    {error=1;}}   // If cipher key contains a number, display error message
  101.    
  102.    
  103. if (error!=1) // If there are no errors, continue to swapping data
  104.    {SwappingData (cipher, plaintext);}
  105. if (error>0)
  106.    {printf("Cipher key must be between 2 and 16 characters long\n");
  107.    printf("Plain text must be between 4 and 256 characters long\n");
  108.    printf("Cipher must contain only lower case characters of the alphabet (a-z)without any duplicates\n");
  109.    printf("Plain text must contain only lower case characters of the alphabet (a-z)without any duplicates\n");
  110.    printf("Ensure Cipher length and Plain text lengths are  correct\n");
  111.    printf("Ensure there is a space in the plain text\n");}
  112. i--;}
  113.    
  114. }
  115.  
  116.  
  117.  
  118. void SwappingData (char cipher[], char plaintext[])
  119. {
  120. n=0;
  121. while (n<cipherlen-1){ // Scan through cipher length
  122.  
  123. for (p=0; p<plaintextlen;p++)
  124.  
  125. {if (plaintext[p] == cipher[n]) // Find the character at location 0 of cipher key to plaintext
  126. plaintext[p]=cipher[n+1]; // Swap for character at next location of cipher key
  127.  
  128. if (p==plaintextlen-1) // Display output as often as necessary
  129. {
  130. printf("Step %d substitute %c with %c", n+1, cipher[n], cipher[n+1]);
  131. printf("%s\n",plaintext);}}
  132. n++; // Increase loaction by 1
  133. }
  134. for (p=0; p<=plaintextlen;p++){
  135. if (plaintext[p] == cipher[n])
  136. plaintext[p]=cipher[0]; // Swap for character at location 0   
  137. if (p==plaintextlen-1)
  138. {
  139. printf("Step %d substitute %c with %c", n+1, cipher[n], cipher[0]);
  140. printf("%s\n",plaintext);}}}