<mosaic.cnfolio.com>
PROGRAM
1. /*PSEUDOCODE
2.
3. START
4. -------------------------------------1st line input LENGTH OF CIPHER KEY------------------------------------------
5.
6.
8. IF number input is not a number
9.    THEN give appropriate error message then quit program
10. ELSE IF number is not between 2 and 16 characters
11.    THEN give appropriate error message then quit program
12. ELSE store the number for the length of cipher key and move onto next section
13. ENDIF
14.
15.
16. -------------------------------------2nd line input CIPHER KEY TEXT-----------------------------------------------
17.
18.
20. IF input is number
21.    THEN give appropriate error message then quit program
22. ELSEIF text contains uppercase letters
23.    THEN give appropriate error message then quit the program
24. ELSE store the text and move onto next section
25. ENDIF
26.
27. -------------------------------------3rd line input LENGTH OF PLAIN TEXT------------------------------------------
28.
30. IF number input is not a number
31.    THEN give appropriate error message then quit program
32. ELSE store the number for the length of plain text and move onto next section
33. ENDIF
34.
35. ----------------------------------4th line input PLAIN TEXT FOR ENCRYPTION----------------------------------------
37. IF input is number
38.    THEN give appropriate error message then quit program
39. ELSE IF when number of characters when counted (including spacing) is more than length of plain text value
40.    THEN give appropriate error message then quit program
41. ELSE IF when number of characters when counted (including spacing) is less than length of plain text value
42.    THEN give appropriate error message then quit program
43. ELSE store the text  and move onto next section
44. ENDIF
45.
46. ----------------------------------------------GIVING OUTPUT-------------------------------------------------------
47.
48. Make N=0
49. WHILE N DOES NOT equal length of cipher key
50.    Make P=0
51.       WHILE P DOES NOT equal length of plain text
52.       IF value at position N is equal to value at position P
53.         THEN E equal to P
54.         THEN value at E in encrypted is equal to value at N+1 in cipher key
55.       ENDIF
56.       WRITE "Step (N+1) substitute (value at position N in cipher key) with (value at position N+1 in cipher key)"
57.       WRITE "(new plain text that has been encrypted)"
58.       INCREMENT P
59.    ENDWHILE
60.    INCREMENT N
61. ENDWHILE
62.
63. Make P=0
64. WHILE P DOES NOT equal length of plain text
65.    IF value at position N is equal to value at position P
66.       THEN E equal to P
67.       THEN value at E in encrypted is equal to value at position 0 in cipher key
68.    ENDIF
69.    WRITE "Step (length of cipher key) substitute (value at position N in cipher key) with (value at position N=0 in cipher key)"
70.    WRITE "(new plain text that has been encrypted)"
71.    INCREMENT P
72. END WHILE
73.
74. ------------------------------------------------------------------------------------------------------------------
75. END
76. */
77.
78.
79. #include <stdio.h>
80. #include <string.h>
81. #include <ctype.h>
82. #define PLAINMAX 256
83. #define CIPHERMAX 16
84.
85. int step, N;
86. int lencipher, lenplain;
87. int boolerror=0;            //using as boolean value for error checking, 0=false 1=true
88. char duplicate, temp;
89. char cipher[CIPHERMAX];
90. char ciphersort[CIPHERMAX];
91. char plaintxt[PLAINMAX];
92. char encryption[PLAINMAX];
93.
94.
95. int main (void){
96.
97. /*-------------------------------------1st line input LENGTH OF CIPHER KEY------------------------------------------*/
98. scanf("%d", &lencipher);
99. if (lencipher<2||lencipher>16){
100.    printf("Please enter a number between 2 and 16 for the Length of the Cipher Key.");
101. }
102. else
103. {
104. /*-------------------------------------2nd line input CIPHER KEY TEXT-----------------------------------------------*/
105.   scanf("%s", &cipher);
106.   for(int i=0; i<strlen(cipher); i++)
107.   {
108.         if(32 == cipher[i] || (97 <= cipher[i] && 122 >= cipher[i]))    //this checks that the characters used are within
109.         {                                                               //the ASCII values for the lower case alphabet
110.         }
111.         else
112.         {
113.         boolerror=1;
114.         }
115.   }
116.   if (boolerror==1)
117.   {
118.      printf("Please only use either alpabetic characters or spaces in the Cipher.");
119.   }
120.   else
121.   {
122.   if (strlen(cipher)>lencipher||strlen(cipher)<lencipher)   //checking whether length of cipher value
123.    {                                      //and the cipher are the same
124.       boolerror=1;
125.    }
126.    if (boolerror==1)
127.    {
128.       printf("Please ensure your Cipher has the specified amount of characters you gave in the Length of Cipher Key");
129.    }
130.    else
131.    {
132.    /*----------------------------------------CHECKING FOR DUPLICATE LETTERS---------------------------------------------*/
133.
134.     strcpy(ciphersort, cipher);
135.     for (int q=0; q<(strlen(ciphersort)-1); q++)
136.     {
137.       for (int r=0; r<(strlen(ciphersort)-q-1); r++)
138.       {
139.         if (ciphersort[r] < ciphersort[r+1])
140.         {
141.           temp=ciphersort[r];
142.           ciphersort[r]=ciphersort[r+1];
143.           ciphersort[r+1]=temp;
144.         }
145.       }
146.     }
147.
148.
149.
150.     for (int a=0; a<strlen(ciphersort); a++)
151.     {
152.        if (ciphersort[a]==ciphersort[a+1])
153.        {
154.           boolerror=1;
155.           duplicate=(ciphersort[a+1])//use this value to tell user what letter is duplicated in error output message
156.        }
157.     }
158.
159.     if (boolerror==1)
160.     {
161.        printf("Please do not use duplicate letters in the Cipher. The first occurunce of the letter duplication is %c", duplicate);
162.     }
163.     else{
164.
165. /*-------------------------------------3rd line input LENGTH OF PLAIN TEXT------------------------------------------*/
166.       scanf("%d", &lenplain);
167.       if (lenplain<4||lenplain>256){
168.          printf("Please enter a number between 4 and 256 for the Length of the Plain Text.");
169.       }
170.       else
171.         {
172. /*----------------------------------4th line input PLAIN TEXT FOR ENCRYPTION----------------------------------------*/
173.         scanf("\n%[^\n]",&plaintxt); //this is to copy the whole string including spacing
174.         for(int j=0; j<strlen(plaintxt); j++)
175.         {
176.           if(32 == plaintxt[j] || (97 <= plaintxt[j] && 122 >= plaintxt[j])) //again checking for only lower
177.             {                                              //case alphabetic ASCII values
178.             }
179.             else
180.             {
181.               boolerror=1;
182.             }
183.         }
184.         if (boolerror==1)
185.         {
186.            printf("Please only use either lower case alpabetic characters or spaces in the Plain Text.");
187.         }
188.         else
189.         {
190.          if (strlen(plaintxt)>lenplain||strlen(plaintxt)<lenplain) //comparing length of plain text value and
191.          {                                        //plain text so they are the same length
192.             boolerror=1;
193.          }
194.          if (boolerror==1)
195.          {
196.             printf("Please ensure your Plain Text has the same number of characters specified in your Length of Plain Text");
197.          }
198.          else
199.          {
200. /*----------------------------------------------GIVING OUTPUT-------------------------------------------------------*/
201.           strcpy(encryption, plaintxt);         //copying the contents of the plaintxt array to the encryption arry
202.           cipher[lencipher]=cipher[0];
203.
204.           for (int y=0; y<lencipher; y++)
205.           {
206.              for (N=0; N<lenplain; N++)
207.              {
208.                 if (plaintxt[N]==cipher[y])
209.                 {
210.                    encryption[N]=cipher[y+1];   //swapping the desired letter in the array with the next letter in the cipher
211.                 }
212.              }
213.              step=y+1;    //used for step number in output
214.              printf("\nStep %d: Substitute %c with %c\n", step, cipher[y], cipher[y+1]);
215.              printf("%s", encryption);
216.           }
217.          }
218.         }
219.        }
220.       }
221.      }
222.
223.   }
224.
225.          }
226.
227.
228.
229. return(0);
230.
231. }