<mosaic.cnfolio.com>
PROGRAM
  1. /* Pseudocode
  2.  
  3. START
  4.  
  5. -----------------------Input Values--------------------------------------------------------
  6.  
  7. Input Lenght of Cipher
  8. Get Value of Cipher in order to "Search"
  9.    If the Cipher lenght has to be between 2 to 16 characters long
  10. Input Cipher Key
  11. Get Cipher Key in order to "Search"
  12.    If the Cipher Key has any duplicates
  13.    If the Cipher Key has any Upper Case letters
  14.    If the Cipher Key has any non alphabetical letters
  15. Input Lenght of PlainText
  16. Get Lenght of Main Text in order to "Search"
  17.    If the PlainText is between 4-256 characters long
  18.    If the PlainText contain any Upper Case letters
  19. Input PlainText
  20. Get Length of Plain Text in order to "Search"
  21.    If the Plain Text has any non alphabetical letters
  22.    If the Plain Text matches the lenght of the Length
  23.    
  24. Copy the input of the PlainText into a seperate string
  25. If any validation rules are broken!
  26. End Program
  27.  
  28. -----------------------Testing----------------------------------------------------------------
  29.  
  30. Set "Search" and "PlainText" to positions
  31. Set "Substitute" to the second position
  32. If PlainText= Search
  33. Then change "PlainText" to "Subsititute"
  34. Display the conversion between Plain Text & Substitute
  35. Set Search to final position = move 1 position
  36. Display final conversion
  37.  
  38.  
  39. End*/
  40.  
  41. #include <stdio.h>
  42. #include <string.h>
  43.  
  44. int main(void)
  45.  
  46. {
  47.  
  48. int LengthOfCipher;
  49. int LengthOfPlainText;
  50. char CipherKey[16];
  51. char Substitute[16];
  52. char PlainText[256]
  53. int CipherKeyPosition=0;
  54. int SubstitutePosition=0;
  55. int PlainTextPosition=0;
  56. int Counter2=0;
  57. int ChangedPosition[256];
  58. int OpenPosition=0;
  59. int Test;
  60. int counter;
  61.  
  62. /* ---------- input ---------- */
  63.  
  64. scanf ("%d", &LengthOfCipher); /*Gets the input for the length of the Cipher Key*/
  65. scanf ("%s", &CipherKey); /* Gets the input for the Cipher Key*/
  66. scanf ("%d", &LengthOfPlainText); /*Gets the input for lenght of the Plain Text */
  67. scanf (" %[^\n]256", &PlainText); /*Gets the input for the Plain Text */
  68.  
  69. /* ---------- Error Checks ---------- */
  70.  
  71.  
  72.     if(LengthOfCipher<2 || LengthOfCipher >16)
  73.     {
  74.       printf("Error! The length of cipher has to be between 2 to 16 characters long ,please try again. \n");
  75.        return 0;
  76.       }
  77.   if(strlen(CipherKey)!=LengthOfCipher) /*checks if the length of cipher matches the actual length of Cipher Key */
  78.     {
  79.       printf("Error! The length of the Cipher Key doesn't match  the length of the cipher key, please try again. \n");
  80.        return 0;
  81.       }
  82.    for(Test=0; Test<LengthOfCipher; Test++)
  83.     {
  84.       for(counter=Test+1; counter<LengthOfCipher+1; counter++)
  85.         {
  86.          if(CipherKey[Test]==CipherKey[counter])/*Checks if there is any duplicates*/
  87.             {
  88.             printf("Error! A duplicate was found in the Cipher Key, please try again. \n");
  89.             return 0;}
  90.          }
  91.       }
  92.    for(CipherKeyPosition=0; CipherKeyPosition<LengthOfCipher; CipherKeyPosition++)
  93.     {
  94.      if (CipherKey[CipherKeyPosition] >= 'A' && CipherKey[CipherKeyPosition] <= 'Z')/*Cipher Key can only contain lowercase letters*/
  95.           {
  96.             printf("Error! Capital letters where found in the Cipher Key, please try again. \n");
  97.             return 0; }
  98.       }     
  99.  
  100.  
  101.     if(LengthOfPlainText<4 || LengthOfPlainText>256)/*Length of Plain Text can only be between 4-256 characters long*/
  102.     {
  103.       printf("Error! The length of the plain text can only be between 4 to 256 characters long, please try again. \n");
  104.       return 0;
  105.       }
  106.  
  107.    for(PlainTextPosition=0; PlainTextPosition<LengthOfPlainText; PlainTextPosition++)
  108.     {
  109.      if (PlainText[PlainTextPosition] >= 'A' && PlainText[PlainTextPosition] <= 'Z')/*Plain Text can only contain lowercase characters*/
  110.           {
  111.             printf("Error! Capital leters where found in the plain text, please try again. \n");
  112.             return 0; }
  113.         }
  114.       for(PlainTextPosition=0; PlainTextPosition<LengthOfPlainText; PlainTextPosition++)
  115.     {
  116.      if (PlainText[PlainTextPosition] >= '0' && PlainText[PlainTextPosition] <= '9')/*Plain Text can only contain letters*/
  117.           {
  118.             printf("Error! Please enter letters only. \n");
  119.             return 0; }
  120.             }
  121.      
  122.       for(CipherKeyPosition=0; CipherKeyPosition<LengthOfCipher; CipherKeyPosition++)
  123.     {
  124.      if (CipherKey[CipherKeyPosition] >= '0' && CipherKey[CipherKeyPosition] <= '9')/*Cipher Key can only contain letters*/
  125.           {
  126.             printf("Error! Please enter letters only. \n");
  127.             return 0; }       
  128.            
  129.       }     
  130.  
  131. strncpy (Substitute,CipherKey, sizeof(Substitute)); /*copies the string into a seperate string for encryption */
  132.      
  133. /* Encryption*/ 
  134. SubstitutePosition=0;
  135. CipherKeyPosition=0;
  136. Counter2=0; /* Sets the variables to previous position in order for encryption*/
  137. OpenPosition=0;
  138.  
  139. for(CipherKeyPosition=0; CipherKeyPosition<LengthOfCipher-1; CipherKeyPosition++)
  140.     {
  141.     SubstitutePosition++;
  142.       Counter2++;
  143.      
  144.       for(PlainTextPosition=0; PlainTextPosition<LengthOfPlainText+1; PlainTextPosition++)
  145.                                                                                            
  146.         {
  147.          if(PlainText[PlainTextPosition]==CipherKey[CipherKeyPosition])
  148.             {
  149.              Test=0;
  150.              counter=0;
  151.              for(Test=0; Test<OpenPosition; Test++)
  152.                 { if (PlainTextPosition==ChangedPosition[Test])
  153.                         {
  154.                     counter++;}
  155.                }     
  156.              if(counter==0)
  157.                 {
  158.                      PlainText[PlainTextPosition]=Substitute[SubstitutePosition];
  159.                      ChangedPosition[OpenPosition]=PlainTextPosition;
  160.                      OpenPosition++; }
  161.                }
  162.          }
  163.          printf("Step %d replace %c with %c \n", Counter2, CipherKey[CipherKeyPosition], Substitute[SubstitutePosition]);/*First line of the output*/
  164.          printf("%s \n", PlainText);
  165.    }
  166.  
  167. SubstitutePosition=0;
  168. CipherKeyPosition=LengthOfCipher-1;
  169. PlainTextPosition=0;
  170. Counter2=LengthOfCipher;
  171.  
  172.  
  173.  
  174.  
  175. for(PlainTextPosition=0; PlainTextPosition<LengthOfPlainText+1; PlainTextPosition++)
  176.     { if(PlainText[PlainTextPosition]==CipherKey[CipherKeyPosition])
  177.     { Test=0;
  178.              counter=0;
  179.              for(Test=0; Test<OpenPosition; Test++)
  180.                 { if (PlainTextPosition==ChangedPosition[Test])
  181.                         {
  182.                     counter++;}
  183.                }     
  184.              if(counter==0)
  185.                 {
  186.                      PlainText[PlainTextPosition]=Substitute[SubstitutePosition];
  187.                      ChangedPosition[OpenPosition]=PlainTextPosition;
  188.                      OpenPosition++; }
  189.       }
  190.       }
  191. /*final Output*/
  192. printf("Step %d replace %c with %c \n", Counter2, CipherKey[CipherKeyPosition], Substitute[SubstitutePosition]);
  193. printf("%s \n", PlainText)
  194.      
  195. return 0;
  196. }