<mosaic.cnfolio.com>
PROGRAM
  1. /*PSEUDO CODE
  2.  
  3. In this code the libraries used are stdio.h, string.h and stdlib.h
  4. as it needs to be able to efficiently handle and manipulate arrays.
  5.  
  6. The code starts by printing instructions on the inputs that are
  7. needed and the requirements such as maximum and minimum lengths of
  8. the cipher key and text message.
  9.  
  10. The inputs are then scanned and stored within the program so that
  11. they can be called upon by the code and manipulated later, as well
  12. text message is copied into a second array so that it can be altered
  13. with the ciphercode.
  14.  
  15. The code then checks to see whether the inputs meet the requirement
  16. and if not then they display an error message informing the user of
  17. the issue and terminating the program.
  18.  
  19. The code then moves onto the ciphering of the text message, it
  20. continuously goes through the cipher and checks to see whether any of
  21. the characters match the cipher code and if they do then they swap the
  22. characters. The program continues through this process until the entire
  23. cipher has been checked and the message is fully ciphered. Once the
  24. message has been fully ciphered the program then terminates.
  25.  
  26. */
  27.  
  28.  
  29. #include<stdio.h>
  30. #include<string.h>
  31. #include <stdlib.h>
  32.  
  33. int main () {
  34.  
  35. int i;                                                             /*All of the arrays and integers are initialised*/
  36. char text[256];
  37. char cipher[256];
  38. char encryptedtext[256];
  39. char ch;
  40. int textlength;
  41. int x = 0;
  42. int y = 0;
  43. int z = 0;
  44. int c = 0;
  45.  
  46. printf("input length of cipher:\n");                              /*prints messages to prompt inputs and inform on their requirements*/
  47. printf("enter cipher key between 2 & 16 characters:\n");
  48. printf("enter length of plain text including spaces:\n");
  49. printf("enter plain text:\n");
  50.  
  51. scanf("%d",&y);                                                   /*scans and stores the inputs*/
  52. scanf("%s", cipher);
  53. scanf("%d",&z);
  54. scanf(" %[^\n]s",text);
  55.  
  56.  
  57.  
  58.  
  59. strcpy(encryptedtext, text);                                      /*copies the text message into a second array so it can be manipulated*/
  60.  
  61. if(2 > y){
  62. printf("\ncipher is too small");                                  /*error message if cipher is too small*/
  63. return 0;}
  64. else{
  65. if(16 < y){
  66. printf("\ncipher is too big");                                    /*error message if cipher is too large*/
  67. return 0;}
  68. else{
  69.  
  70. if(4 > z){
  71. printf("\nmessage is too small");                                 /*error text message is too small*/
  72. return 0;}
  73. else{
  74. if(256 < z){
  75. printf("\nmessage is too big");                                   /*error text message is too large*/
  76. return 0;}
  77. else{
  78.  
  79.  
  80.          
  81.         printf("\nstep 1 substitute ");                          /*prints the characters to be changed in the cipher*/
  82.         printf("%c", cipher[0]);
  83.         printf(" with ");
  84.         printf("%c\n", cipher[1]);
  85.         for( x = 0; x < z; x = x+1) {   
  86.         if(text[x] == cipher[0]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  87.         encryptedtext[x] = cipher[1];
  88.           }}
  89.         printf("%s\n", encryptedtext);                           /*prints the encrypted text*/
  90.                    
  91.                
  92.         if (y == 2){
  93.         printf("\nstep 2 substitute ");                          /*prints the characters to be changed in the cipher*/
  94.         printf("%c", cipher[1]);
  95.         printf(" with ");
  96.         printf("%c\n", cipher[0]);
  97.         for( x = 0; x < z; x = x+1) {
  98.         if(text[x] == cipher[1]){
  99.         encryptedtext[x] = cipher[0];}}}                         /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  100.         if (y == 2){
  101.         goto finish;}
  102.         else
  103.         printf("\nstep 2 substitute ");                          /*prints the characters to be changed in the cipher*/
  104.         printf("%c", cipher[1]);
  105.         printf(" with ");
  106.         printf("%c\n", cipher[2]);
  107.         for( x = 0; x < z; x = x+1) {
  108.         if(text[x] == cipher[1]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  109.         encryptedtext[x] = cipher[2];}
  110.            }
  111.         printf("%s\n", encryptedtext);                           /*prints the encrypted text*/   
  112.        
  113.         if (y == 3){
  114.         printf("\nstep 3 substitute ");                          /*prints the characters to be changed in the cipher*/
  115.         printf("%c", cipher[2]);
  116.         printf(" with ");
  117.         printf("%c\n", cipher[0]);
  118.         for( x = 0; x < z; x = x+1) {
  119.         if(text[x] == cipher[2]){
  120.         encryptedtext[x] = cipher[0];}}}                         /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  121.         if (y == 3){
  122.         goto finish;}
  123.         else
  124.         printf("\nstep 3 substitute ");                          /*prints the characters to be changed in the cipher*/
  125.         printf("%c", cipher[2]);
  126.         printf(" with ");
  127.         printf("%c\n", cipher[3]);
  128.         for( x = 0; x < z; x = x+1) {
  129.         if(text[x] == cipher[2]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  130.         encryptedtext[x] = cipher[3];}
  131.         }
  132.        
  133.         printf("%s\n", encryptedtext);                           /*prints the encrypted text*/
  134.              
  135.        
  136.         if (y == 4){
  137.         printf("\nstep 4 substitute ");                          /*prints the characters to be changed in the cipher*/
  138.         printf("%c", cipher[3]);
  139.         printf(" with ");
  140.         printf("%c\n", cipher[0]);
  141.         for( x = 0; x < z; x = x+1) {
  142.         if(text[x] == cipher[3]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  143.         encryptedtext[x] = cipher[0];}}}
  144.         if (y == 4){
  145.         goto finish;}
  146.         else
  147.         printf("\nstep 4 substitute ");                          /*prints the characters to be changed in the cipher*/
  148.         printf("%c", cipher[3]);
  149.         printf(" with ");
  150.         printf("%c\n", cipher[4]);
  151.         for( x = 0; x < z; x = x+1) {
  152.         if(text[x] == cipher[3]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  153.         encryptedtext[x] = cipher[4];}
  154.         }
  155.         printf("%s\n", encryptedtext);                           /*prints the encrypted text*/
  156.            
  157.            
  158.        
  159.         if (y == 5){
  160.         printf("\nstep 5 substitute ");                           /*prints the characters to be changed in the cipher*/
  161.         printf("%c", cipher[4]);
  162.         printf(" with ");
  163.         printf("%c\n", cipher[0]);
  164.         for( x = 0; x < z; x = x+1) {
  165.         if(text[x] == cipher[4]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  166.         encryptedtext[x] = cipher[0];}}}
  167.         if (y == 5){
  168.         goto finish;}
  169.         else
  170.         printf("\nstep 5 substitute ");                          /*prints the characters to be changed in the cipher*/
  171.         printf("%c", cipher[4]);
  172.         printf(" with ");
  173.         printf("%c\n", cipher[5]);
  174.         for( x = 0; x < z; x = x+1) {
  175.         if(text[x] == cipher[4]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  176.         encryptedtext[x] = cipher[5];}
  177.         }
  178.         printf("%s\n", encryptedtext);                           /*prints the encrypted text*/
  179.          
  180.              
  181.        
  182.         if (y == 6){
  183.         printf("\nstep 6 substitute ");                          /*prints the characters to be changed in the cipher*/
  184.         printf("%c", cipher[5]);
  185.         printf(" with ");
  186.         printf("%c\n", cipher[0]);
  187.         for( x = 0; x < z; x = x+1) {
  188.         if(text[x] == cipher[5]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  189.         encryptedtext[x] = cipher[0];}}}
  190.         if (y == 6){
  191.         goto finish;}
  192.         else
  193.         printf("\nstep 6 substitute ");                          /*prints the characters to be changed in the cipher*/
  194.         printf("%c", cipher[5]);
  195.         printf(" with ");
  196.         printf("%c\n", cipher[6]);
  197.         for( x = 0; x < z; x = x+1) {
  198.         if(text[x] == cipher[5]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  199.         encryptedtext[x] = cipher[6];}
  200.         }
  201.         printf("%s\n", encryptedtext);                           /*prints the encrypted text*/
  202.            
  203.        
  204.                    
  205.         if (y == 7){
  206.         printf("\nstep 7 substitute ");                          /*prints the characters to be changed in the cipher*/
  207.         printf("%c", cipher[6]);
  208.         printf(" with ");
  209.         printf("%c\n", cipher[0]);
  210.         for( x = 0; x < z; x = x+1) {
  211.         if(text[x] == cipher[6]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  212.         encryptedtext[x] = cipher[0];}}}
  213.         if (y == 7){
  214.         goto finish;}
  215.         else
  216.         printf("\nstep 7 substitute ");                         /*prints the characters to be changed in the cipher*/
  217.         printf("%c", cipher[6]);
  218.         printf(" with ");
  219.         printf("%c\n", cipher[7]);
  220.         for( x = 0; x < z; x = x+1) {
  221.         if(text[x] == cipher[6]){                               /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  222.         encryptedtext[x] = cipher[7];}
  223.         }
  224.         printf("%s\n", encryptedtext);                          /*prints the encrypted text*/
  225.          
  226.        
  227.                    
  228.         if (y == 8){
  229.         printf("\nstep 8 substitute ");                         /*prints the characters to be changed in the cipher*/
  230.         printf("%c", cipher[7]);
  231.         printf(" with ");
  232.         printf("%c\n", cipher[0]);
  233.         for( x = 0; x < z; x = x+1) {
  234.         if(text[x] == cipher[7]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  235.         encryptedtext[x] = cipher[0];}}}
  236.         if (y == 8){
  237.         goto finish;}
  238.         else
  239.         printf("\nstep 8 substitute ");                        /*prints the characters to be changed in the cipher*/
  240.         printf("%c", cipher[7]);
  241.         printf(" with ");
  242.         printf("%c\n", cipher[8]);
  243.         for( x = 0; x < z; x = x+1) {
  244.         if(text[x] == cipher[7]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  245.         encryptedtext[x] = cipher[8];}
  246.         }
  247.         printf("%s\n", encryptedtext);                         /*prints the encrypted text*/
  248.          
  249.        
  250.                    
  251.         if (y == 9){
  252.         printf("\nstep 9 substitute ");                        /*prints the characters to be changed in the cipher*/
  253.         printf("%c", cipher[8]);
  254.         printf(" with ");
  255.         printf("%c\n", cipher[0]);
  256.         for( x = 0; x < z; x = x+1) {
  257.         if(text[x] == cipher[8]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  258.         encryptedtext[x] = cipher[0];}}}
  259.         if (y == 9){
  260.         goto finish;}
  261.         else
  262.         printf("\nstep 9 substitute ");                        /*prints the characters to be changed in the cipher*/
  263.         printf("%c", cipher[8]);
  264.         printf(" with ");
  265.         printf("%c\n", cipher[9]);
  266.         for( x = 0; x < z; x = x+1) {
  267.         if(text[x] == cipher[8]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  268.         encryptedtext[x] = cipher[9];}
  269.         }
  270.         printf("%s\n", encryptedtext);                         /*prints the encrypted text*/
  271.          
  272.        
  273.                    
  274.         if (y == 10){
  275.         printf("\nstep 10 substitute ");                       /*prints the characters to be changed in the cipher*/
  276.         printf("%c", cipher[9]);
  277.         printf(" with ");
  278.         printf("%c\n", cipher[0]);
  279.         for( x = 0; x < z; x = x+1) {
  280.         if(text[x] == cipher[9]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  281.         encryptedtext[x] = cipher[0];}}}
  282.         if (y == 10){
  283.         goto finish;}
  284.         else
  285.         printf("\nstep 10 substitute ");                      /*prints the characters to be changed in the cipher*/
  286.         printf("%c", cipher[9]);
  287.         printf(" with ");
  288.         printf("%c\n", cipher[10]);
  289.         for( x = 0; x < z; x = x+1) {
  290.         if(text[x] == cipher[9]){                             /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  291.         encryptedtext[x] = cipher[10];}
  292.         }
  293.         printf("%s\n", encryptedtext);                        /*prints the encrypted text*/
  294.          
  295.        
  296.              
  297.                    
  298.         if (y == 11){
  299.         printf("\nstep 11 substitute ");                      /*prints the characters to be changed in the cipher*/
  300.         printf("%c", cipher[10]);
  301.         printf(" with ");
  302.         printf("%c\n", cipher[0]);
  303.         for( x = 0; x < z; x = x+1) {
  304.         if(text[x] == cipher[10]){                            /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  305.         encryptedtext[x] = cipher[0];}}}
  306.         if (y == 11){
  307.         goto finish;}
  308.         else
  309.         printf("\nstep 11 substitute ");                      /*prints the characters to be changed in the cipher*/
  310.         printf("%c", cipher[10]);
  311.         printf(" with ");
  312.         printf("%c\n", cipher[11]);
  313.         for( x = 0; x < z; x = x+1) {
  314.         if(text[x] == cipher[10]){                            /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  315.         encryptedtext[x] = cipher[11];}
  316.         }
  317.         printf("%s\n", encryptedtext);                        /*prints the encrypted text*/
  318.          
  319.        
  320.                    
  321.         if (y == 12){
  322.         printf("\nstep 12 substitute ");                       /*prints the characters to be changed in the cipher*/
  323.         printf("%c", cipher[11]);
  324.         printf(" with ");
  325.         printf("%c\n", cipher[0]);
  326.         for( x = 0; x < z; x = x+1) {
  327.         if(text[x] == cipher[11]){                             /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  328.         encryptedtext[x] = cipher[0];}}}
  329.         if (y == 12){
  330.         goto finish;}
  331.         else
  332.         printf("\nstep 12 substitute ");                       /*prints the characters to be changed in the cipher*/
  333.         printf("%c", cipher[11]);
  334.         printf(" with ");
  335.         printf("%c\n", cipher[12]);
  336.         for( x = 0; x < z; x = x+1) {
  337.         if(text[x] == cipher[11]){                             /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  338.         encryptedtext[x] = cipher[12];}
  339.         }
  340.         printf("%s\n", encryptedtext);                         /*prints the encrypted text*/
  341.          
  342.        
  343.                    
  344.         if (y == 13){
  345.         printf("\nstep 13 substitute ");                        /*prints the characters to be changed in the cipher*/
  346.         printf("%c", cipher[12]);
  347.         printf(" with ");
  348.         printf("%c\n", cipher[0]);
  349.         for( x = 0; x < z; x = x+1) {
  350.         if(text[x] == cipher[12]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  351.         encryptedtext[x] = cipher[0];}}}
  352.         if (y == 13){
  353.         goto finish;}
  354.         else
  355.         printf("\nstep 13 substitute ");                        /*prints the characters to be changed in the cipher*/
  356.         printf("%c", cipher[12]);
  357.         printf(" with ");
  358.         printf("%c\n", cipher[13]);
  359.         for( x = 0; x < z; x = x+1) {
  360.         if(text[x] == cipher[12]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  361.         encryptedtext[x] = cipher[13];}
  362.         }
  363.         printf("%s\n", encryptedtext);                          /*prints the encrypted text*/
  364.          
  365.        
  366.                    
  367.         if (y == 14){
  368.         printf("\nstep 14 substitute ");                         /*prints the characters to be changed in the cipher*/
  369.         printf("%c", cipher[13]);
  370.         printf(" with ");
  371.         printf("%c\n", cipher[0]);
  372.         for( x = 0; x < z; x = x+1) {
  373.         if(text[x] == cipher[13]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  374.         encryptedtext[x] = cipher[0];}}}
  375.         if (y == 14){
  376.         goto finish;}
  377.         else
  378.         printf("\nstep 14 substitute ");                        /*prints the characters to be changed in the cipher*/
  379.         printf("%c", cipher[13]);
  380.         printf(" with ");
  381.         printf("%c\n", cipher[14]);
  382.         for( x = 0; x < z; x = x+1) {
  383.         if(text[x] == cipher[13]){                              /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  384.         encryptedtext[x] = cipher[14];}
  385.         }
  386.         printf("%s\n", encryptedtext);                          /*prints the encrypted text*/
  387.          
  388.        
  389.                    
  390.         if (y == 15){
  391.         printf("\nstep 15 substitute ");                         /*prints the characters to be changed in the cipher*/
  392.         printf("%c", cipher[14]);
  393.         printf(" with ");
  394.         printf("%c\n", cipher[0]);
  395.         for( x = 0; x < z; x = x+1) {
  396.         if(text[x] == cipher[14]){                               /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  397.         encryptedtext[x] = cipher[0];}}}
  398.         if (y == 15){
  399.         goto finish;}
  400.         else
  401.         printf("\nstep 15 substitute ");                          /*prints the characters to be changed in the cipher*/
  402.         printf("%c", cipher[14]);
  403.         printf(" with ");
  404.         printf("%c\n", cipher[15]);
  405.         for( x = 0; x < z; x = x+1) {
  406.         if(text[x] == cipher[14]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  407.         encryptedtext[x] = cipher[15];}
  408.         }
  409.         printf("%s\n", encryptedtext);                            /*prints the encrypted text*/
  410.          
  411.        
  412.        
  413.         if (y == 16){
  414.         printf("\nstep 16 substitute ");                          /*prints the characters to be changed in the cipher*/
  415.         printf("%c", cipher[15]);
  416.         printf(" with ");
  417.         printf("%c\n", cipher[0]);
  418.         for( x = 0; x < z; x = x+1) {
  419.         if(text[x] == cipher[15]){                                /*tests to see if any of the text characters match the cipher and swapping them if they match*/
  420.         encryptedtext[x] = cipher[0];}
  421.         }}}}}}
  422.         finish: printf("%s\n", encryptedtext);                    /*prints the encrypted text*/
  423.                
  424.         return 0;                                                 /*ends the program*/
  425.         }