<mosaic.cnfolio.com>
PROGRAM
  1. /*-----------------------------------------Pseudo Code----------------------------------------------------
  2.  
  3.  
  4.  
  5. Start
  6.  
  7. --------------------------------------------INPUTS -------------------------------------------------------
  8.  
  9. OBTAIN Cipher key length
  10. OBTAIN Cipher key
  11. OBTAIN length of plain text
  12. OBTAIN plain text for encrytpion
  13.  
  14. -------------------------------------------END OF INPUTS--------------------------------------------------
  15.  
  16. -------------------------------------------INPUT VALIDATION-----------------------------------------------
  17.  
  18. WHILE Cipher key length is more than or equal to 2 and less than or equal to 16
  19.  
  20. IF Cipher key length is less than 2 or more than 16
  21.    PRINT Error Cipher key is too short or too long
  22. END IF
  23.  
  24.   WHILE No capital letters within the single character string
  25.    IF any capital letters are in single character string
  26.     PRINT Error, capital letters found in Cipher key
  27.    END IF
  28.    WHILE No duplicate characters in Cipher key
  29.     IF Duplicate characters found in Cipher key
  30.      PRINT Error, there are duplicates in the Cipher key
  31.     END IF
  32.    Check next character from Cipher key
  33.    END WHILE
  34.   END WHILE
  35. END WHILE
  36.  
  37. WHILE length of plain text is more than or equal to 4 and less than or equal to 256
  38.   IF length of plain text isn't between 4 and 256, 4 and 256 inclusive
  39.    PRINT Error, plain text is too short or too long
  40.   END IF
  41.   WHILE All characters are lower case or a space
  42.    IF A capital letter found in single character string
  43.     PRINT Error, capital letter found in plain text
  44.    END IF
  45.   INPUT plain text into temporary variable for encryption
  46. END WHILE
  47. END WHILE
  48.  
  49. -----------------------------------------END OF INPUT VALIDATION-----------------------------------------
  50.  
  51. -----------------------------------------MAIN PSEUDO CODE------------------------------------------------
  52.  
  53. WHILE Position of character in plain text and number of iterations is less than Cipher key length
  54.   WHILE every single end value is not substituted
  55.     IF the character in single character string hasnt yet been substituted
  56.      Change character in single character string that matches cipher key at the same position to position + number of times loop iterated + 1
  57.       IF character in single character string was already changed
  58.        Don't change character in single character string
  59.       END IF
  60.      PRINT "step 'position indicator': character A has been replaced by character B"
  61.      Swap every A character to B character cipher
  62.     END IF
  63.   END WHILE
  64. PRINT all B characters, displaying the encrypted text
  65. END WHILE
  66.  
  67. END
  68.  
  69. -------------------------------------END OF MAIN PSEUDO CODE---------------------------------------------*/
  70.  
  71.  
  72.  
  73. /*----------------------------------------Main Code------------------------------------------------------*/
  74.  
  75. /*--------------------------------------HEADER FILES-----------------------------------------------------*/
  76.  
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79.  
  80. /*------------------------------------END OF HEADER FILES------------------------------------------------*/
  81.  
  82. /*-----------------------------------VARIABLE DECLARATIONS-----------------------------------------------*/
  83.  
  84. int cipherkeylen; /*Integer variable to hold the length of the cipher key*/
  85. char cipherkey[16]; /*String with predefined length of 16 characters to hold the cipher key itself*/
  86. int plaintextlen; /*Integer variable to store the length of the plain text to be ciphered*/
  87. char plaintext[256]; /*String with predefined length of 256 characters to hold the plain text to be ciphered */
  88. int posind = 0; /*Integer variable to store the number POSition INDication when navigating through the cipherkey, set to 0 by default to be safe */
  89. int counter = 0; /*Integer variable to store the value of a counter, used in the code to compare how many times the loops have iterated agains length of cipherlen and plaintext */
  90. char temporary[256]; /*String variable to put plaintext into without modifying it*/
  91.  
  92. /*-------------------------------END OF VARIABLE DECLARATIONS---------------------------------------------*/
  93.  
  94. /*-----------------------------------FUNCTION DECLARATIONS------------------------------------------------*/
  95.  
  96. int errchk(char check);
  97.  
  98. /*--------------------------------END OF FUNCTION DECLARATION---------------------------------------------*/
  99.  
  100. /*-----------------------------------------INPUTS---------------------------------------------------------*/
  101.  
  102. int main() /* initialising main function */
  103.  
  104. {
  105.  
  106. scanf("%d", &cipherkeylen); /*scanf reads the input and writes it to cipherkeylen as an integer*/
  107.  
  108. if(cipherkeylen <2 || cipherkeylen >16) /* this if statement is performed if chipherkeylen is less than 2 or more than 16 */
  109.  
  110.  {
  111.    printf("The cipher key is too short or too long!"); /*prints error message about cipher key length being invalid*/
  112.    return 1; /*returns 1 to stop program if error is found*/
  113.  }
  114.  
  115. for(counter = 0; counter <= cipherkeylen; counter++) /*First, the counter is set to 0, and then the loop is set to add 1 to counter if the counter is less than or equal to cipherkeylen*/
  116.  
  117.  {
  118.    scanf("%c", &cipherkey[counter]); /*this is to input the cipher text characters one at a time*/
  119.    
  120.     if(errchk(cipherkey[counter]) !=0) /*this is to check if the cipherkey input has errors*/
  121.      
  122.      {
  123.        return 1; /*returns 1 to stop program if error is found*/
  124.      }
  125.  }
  126.  
  127.  
  128.  scanf("%d" , &plaintextlen); /* input for plaintextlen scanned */
  129.  
  130.  if(plaintextlen <4 || plaintextlen >256) /* this if statement is only executed when plaintextlen is too short or too long */
  131.  
  132.   {
  133.     printf("Plain text lenght is too small or too big!"); /* prints error message about plain text length being invalid */
  134.     return 1; /*returns 1 to stop program if error is found*/
  135.   }
  136.  
  137.  for (counter = 0; counter <= plaintextlen; counter++) /*this is to input the plain text characters one at a time*/
  138.  
  139.   {
  140.     scanf("%c" , &plaintext[counter]); /*this is to input the plain text characters one at a time*/
  141.     temporary[counter] = plaintext[counter]; /*this is to hold the plain text in a temporary variable*/
  142.    
  143.     if(errchk(plaintext[counter]) != 0) /*this is to check if the plain text input has errors*/
  144.      
  145.      {
  146.        return 1; /*returns 1 to stop program if error is found*/
  147.      }
  148.   }
  149.  
  150. /*----------------------------------------END OF INPUTS--------------------------------------------------*/
  151.  
  152. /*----------------------------------------ENCRYPTION-----------------------------------------------------*/
  153.  
  154.  
  155. for(posind = 1; posind <= cipherkeylen; posind++) /*keep repeating this until cipher length has been reached*/
  156.  
  157.  {
  158.    if(posind == cipherkeylen)
  159.    
  160.     {
  161.      
  162.       for(counter = 1; counter <= plaintextlen; counter++) /*keep repeating until the text length has been reached*/
  163.        
  164.        {
  165.          
  166.          if(plaintext[counter] == cipherkey[posind]) /*when a cipher character matches plain text character perform task*/
  167.          
  168.           {
  169.             posind = 1; /*this makes sure arrays start at 1*/
  170.             temporary[counter] = cipherkey[posind]; /*this holds plain text in a temporary variable*/
  171.           }
  172.          
  173.          
  174.        }
  175.        
  176.     posind = cipherkeylen; /*this puts N (posind) equal to the length of cipher key*/
  177.     printf("\n step %d replace %c with %c\n", posind, cipherkey[posind], cipherkey[1]); /*output*/
  178.    
  179.    
  180.     for(counter = 1; counter <= plaintextlen; counter++)
  181.    
  182.      {
  183.        printf("%c", temporary[counter]); /*this is to display each encrypted process*/
  184.      }
  185.      
  186.      break; /*this is to leave the loop*/
  187.      
  188.     }
  189.    
  190.    
  191.     for(counter = 1; counter <= plaintextlen; counter++)
  192.      
  193.      {
  194.        
  195.        if(plaintext[counter] == cipherkey[posind]) /*when the plain text value is equal to cipher character perform task*/
  196.        
  197.         {
  198.          temporary[counter] = cipherkey[posind + 1]; /*replace the matching character with the next cipher key character*/
  199.         }
  200.        
  201.      }
  202.      
  203.      printf("\n step %d replace %c with %c\n" , posind, cipherkey[posind], cipherkey[posind + 1]); /*output the next encrypted steps*/
  204.      
  205.      for(counter = 1; counter <= plaintextlen; counter++) /*when the plain text length is reached perform task*/
  206.      
  207.       {
  208.        printf("%c", temporary[counter]); /*print out the encrypted text one character at a time*/
  209.       }
  210.      
  211.  }
  212.  
  213.  return 0;
  214.  
  215. }
  216.  
  217. /*----------------------------------------END OF ENCRYPTION-------------------------------------------*/
  218.  
  219. /*------------------------------------------ERROR CHECKING--------------------------------------------*/
  220.  
  221. int errchk(char checker) /*Returns 0 if there isn't any errors*/
  222.  
  223. {
  224.    
  225.     if(checker <= 'z' && checker >= 'a')
  226.      
  227.      {
  228.        /*This is purposely empty*/
  229.      }
  230.      
  231.      else if(checker <= 'Z' && checker >= 'A') /*this is used for capital letters error checking*/
  232.      
  233.       {
  234.        
  235.         printf("Error, a capital character was found in the string!"); /*prints error message about a capital character being used*/
  236.         return 1; /*returns 1 to stop program if error is found*/
  237.        
  238.       }
  239.  
  240.     switch(checker) /*this is used for number error checking*/
  241.    
  242.       {
  243.        
  244.         case '0': /*all single digits have the same error*/
  245.         case '1': /*all single digits have the same error*/
  246.         case '2': /*all single digits have the same error*/
  247.         case '3': /*all single digits have the same error*/
  248.         case '4': /*all single digits have the same error*/
  249.         case '5': /*all single digits have the same error*/
  250.         case '6': /*all single digits have the same error*/
  251.         case '7': /*all single digits have the same error*/
  252.         case '8': /*all single digits have the same error*/
  253.         case '9': /*all single digits have the same error*/
  254.        
  255.           printf("Error, a number was found in the string!");
  256.           return 1; /*returns 1 to stop program if error is found*/
  257.          
  258.           break; /*this is to leave the loop*/
  259.        
  260.        }
  261.        
  262.    return 0; /*if no errors are found, function returns 0 and therefore no errors on inputs*/
  263.    
  264. }
  265.  
  266. /*---------------------------------------END OF ERROR CHECKING----------------------------------------*/