<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. ***********************************************PSUEDO CODE************************************************
  3.  
  4. START
  5.  
  6. SCAN the number of letters in the cipher key
  7. IF it is greater than 16
  8. PRINT error
  9. IF it is less than 2
  10. PRINT error
  11.  
  12. SCAN the letters in the cipher key
  13. IF number of characters does not equal the previous input
  14. PRINT error
  15. IF upper case letter is used
  16. PRINT error
  17.  
  18. SCAN the text that will be encrypted (ARRAY)
  19. IF input is greater than 256
  20. PRINT error
  21. IF input is less than four
  22. PRINT error
  23.  
  24. INT arraypointer
  25. FOR the location the array pointer is at is to be changed according to the first letter in the cipher key make the array char = cipher key char
  26. PRINT the result after the first pass through
  27.  
  28. arraypointer=0
  29. FOR the location the array pointer is at is to be changed according to the 2nd letter in the cipher key make the array char = cipher key char
  30. PRINT the result of the second pass
  31.  
  32. IF the cipher key letter is swapping the last value make it swap with the first char in the cipher key
  33. PRINT the final encrypted text
  34.  
  35. END
  36. */
  37.  
  38. #include <stdio.h>
  39. #include <ctype.h>
  40.  
  41. int
  42. main (void)
  43. {
  44.  
  45.  
  46. char cipherkey[16], target, swap, swapping;
  47.  
  48.  
  49. int lengthcipherkey, arrayinputcount, cipherkeycount, arraylength,
  50.    
  51. lengthofplaintext, i, x, arraypointercipher,
  52. arraypointertext,error;
  53.  
  54.  
  55. char textarray[256];
  56.  
  57.  error=0;
  58.  
  59. arraylength = 256;
  60.  /*scanning in the inputs and producing the error messages for the inputs*/
  61. scanf ("%d", &lengthcipherkey);
  62.  
  63. if (lengthcipherkey < 2)
  64.  
  65.     {
  66. printf ("CIPHER KEY TOO SHORT");
  67.  error++;   /*this adds a number to the error variable and when it is greater than 1 then the swapping code is not executed*/
  68.  
  69. }
  70.  
  71. if (lengthcipherkey > 16)
  72.  
  73.     {
  74.      
  75. printf ("CIPHER KEY TOO LONG");
  76.    error++;
  77.  
  78. }
  79. for (i = 0; i <= lengthcipherkey; i++)
  80.  
  81.     {
  82.  
  83. scanf ("%c", &cipherkey[i]);
  84.  /*if(cipherkey[i]==cipherkey[i++])
  85. {
  86. printf("DUPLICATE PRESENT TRY AGAIN");
  87. error++;
  88. }
  89. this was an attempt at producing an error message for duplicates but it intefered with the rest of the code  */
  90.  
  91. if (isupper (cipherkey[i])) /*this command executes the code within the curly braces when there is a capital letter present*/
  92.  
  93.    {
  94.  
  95. printf ("UPPER CASE LETTERS PRESENT IN CIPHER KEY TRY AGAIN\n");
  96. error++  ;
  97.  
  98. }
  99.  
  100. }
  101.  
  102. scanf ("%d", &lengthofplaintext);
  103.  
  104.  if(lengthofplaintext<4)
  105.  {
  106.      printf("PLAIN TEXT TOO SHORT\n");
  107.      error++;
  108.  }
  109.  if(lengthofplaintext>256)
  110.  {
  111.      printf("PLAIN TEXT TOO LONG\n");
  112.      error++;
  113.  }
  114.  /*here i am using a for loop to scan a string into an array*/
  115. for (arrayinputcount = 0; arrayinputcount <= lengthofplaintext;
  116.      
  117. arrayinputcount++)
  118.    
  119.     {
  120.      
  121. scanf ("%c", &textarray[arrayinputcount]);
  122.    
  123.  if (isupper (textarray[arrayinputcount]))
  124.    {
  125.        printf ("UPPER CASE LETTERS PRESENT IN PLAIN TEXT TRY AGAIN\n");
  126.         error++   ;
  127.  
  128. }
  129.  
  130. }
  131.  
  132. for (i = 0; i <= lengthcipherkey; i++)
  133.  
  134.     {
  135.      
  136. scanf ("%c", &cipherkey[i]);
  137.    
  138. }
  139.   if(error<1)
  140.   {
  141. /*setting the variables to 0 ready for the while loop*/
  142. i = 0;
  143.  
  144. arraypointercipher = 0;
  145.  
  146. arraypointertext = 0;
  147.  
  148. x = 1;
  149.   /*below is where the actual swapping is taking place that makes the cipher work*/
  150. while (arraypointertext <= lengthofplaintext)
  151.    
  152.     {
  153.      
  154. arraypointercipher = 1;
  155.      
  156. arraypointertext++;
  157.      
  158. target = textarray[arraypointertext];
  159.      
  160.  
  161.  
  162. while (arraypointercipher <= lengthcipherkey)
  163.    
  164.    {
  165.    
  166.  
  167. if (swapping == target)
  168.      
  169.        {
  170.     /*i am using swap and swapping values to keep the place in the cipher key i am up to and to facilitate the swap*/     
  171. textarray[arraypointertext] = swap;/*this is where the actual swap takes place*/
  172.        
  173. printf ("\nstep %d substitute %c with %c\n", x, swapping,swap);
  174. /*here i am printing the different steps that the code is performing
  175. to substitute the values*/
  176.              
  177.        
  178. x++;
  179.    
  180. for (i = 1; i <= lengthofplaintext; i++)
  181.      
  182.       {
  183.        
  184.  
  185. printf ("%c", textarray[i]);
  186.      
  187.  
  188. }
  189.      
  190.  
  191. }
  192.    
  193. swapping = cipherkey[arraypointercipher];
  194.    
  195. swap = cipherkey[(arraypointercipher + 1)];
  196.    
  197. arraypointercipher++;
  198.    
  199. }
  200.      
  201. swapping = cipherkey[lengthcipherkey]; /*the cipher key loops back round to the beggining*/
  202.      
  203. swap = cipherkey[1];
  204.      
  205.  
  206. if (target = swapping)
  207.    
  208.    {
  209.    
  210.  
  211. target = swap;
  212.    
  213.  
  214. }
  215.      
  216.  
  217. if (lengthcipherkey == 2)
  218.    
  219.    {
  220.    
  221. if (textarray[arraypointertext] == cipherkey[1])
  222.      
  223.        {
  224.        
  225. swap = cipherkey[2];
  226.        
  227. swapping = cipherkey[1];
  228.        
  229. textarray[arraypointertext] = cipherkey[2];
  230.      
  231. }
  232.    
  233. if (textarray[arraypointertext] == cipherkey[2])
  234.      
  235.        {
  236.        
  237. swap = cipherkey[1];
  238.        
  239. swapping = cipherkey[2];
  240.        
  241. textarray[arraypointertext] == cipherkey[1];
  242.      
  243. }
  244.    
  245.  
  246. }
  247.    
  248.  
  249. }
  250.  
  251.  
  252. }
  253.  
  254. }