<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2.  
  3.    /*
  4.    
  5.    Pseudo Code
  6.       
  7.    START
  8.  
  9.       INPUT cipher key lenth
  10.       INPUT cipher key
  11.       INPUT lenth of plain text to be encrypted
  12.       INPUT plain text to be encrypted
  13.      
  14.       WHILE errors are detected in the imputs
  15.          IF the cipher key lenth is less than 2 or grater than 16 characters long
  16.          IF the cipher key lenth contains any non numerical characters
  17.          IF the cipher key contains any upper case characters
  18.          IF the cipher key contains any spaces
  19.          IF the cipher key contains repeat characters
  20.       END WHILE
  21.      
  22.       WHILE errors are detected in the imputs
  23.          IF the plain text lenth is less that 4 or grate than 256 characters long
  24.          IF the plain text contains any uper case characters
  25.          IF the plain text contains any numerical characters
  26.       END WHILE
  27.      
  28.       WHILE N=0, N indicating the starting position and first character in the string
  29.      IF position N character in the cipher key is the target character
  30.          IF position N+1 character in the cipher key is the subsitution character
  31.          IF N is the last character in the cipher key the substitution character becomes the first cipher key character 
  32.             
  33.             FOR the plain text replace all appearances of the target character with the substitution value
  34.                FOR every time a set of terget characters is substituted incroment N by one are repeat all steps until the end of the cipher key
  35.                END FOR
  36.             END FOR
  37.                
  38.       END WHILE
  39.            
  40.    END
  41.    
  42.    */
  43.    
  44.  int main ( void )
  45.  
  46.    {
  47.  
  48.  /* Input veriables. */
  49.  
  50.  /*Initialises the four veriables used for storing and manipulating the users four inputs, setting the intergers to 0 and filling the arrays with null values.*/
  51.  
  52.     int CipherKeyLength=0;
  53.     char CipherKey[16]={'\0'};
  54.     int LengthofPlainText=0;
  55.     char PlainText[256]={'\0'};
  56.  
  57.  /* Error check veriables. */
  58.  
  59.  /*Initialises the four veriables used in error checking the users imputs, the first three used In loops for verious error checks and the last being used to count the number of errors encounterd.*/
  60.  
  61.     int ErrorLoop1=0;
  62.     int ErrorLoop2=0;
  63.     int ErrorLoop3=0;
  64.     int ErrorCount=0;
  65.  
  66.  /* Substitution veriables. */
  67.  
  68.  /*Initialises the four veriables used in validating and performing the substitution of characters in the plain text.*/
  69.  
  70.     int CipherKeyValidator=0;
  71.     char PlainTextSubstitution[256];
  72.     int PlainTextValidator=0;
  73.     int loopCount=0;
  74.  
  75.  /* Inputs. */
  76.  
  77.  /*Scans the values for all of the users inputs, also useing strcpy to copy the contence of the plain text into another aray for substitution.*/
  78.  
  79.     scanf("%d", &CipherKeyLength);
  80.     scanf("%s", &CipherKey);
  81.     scanf("%d", &LengthofPlainText);
  82.     scanf(" %[^\n]s", &PlainText);
  83.     strcpy(PlainTextSubstitution, PlainText);
  84.  
  85.  /* Error checking. */
  86.  
  87.  /*Checks wether the value of the input cipher key length is within the alowed paramaters.*/
  88.  
  89.     if(CipherKeyLength < 2 || CipherKeyLength > 16)
  90.        {
  91.          printf("This Cipher key length is invalid.\nThe input length of the cipher Key must be greater than 2 and no more than 16.\nThe input length of the cipher Key must also contain no non numerical characters.\n\n");
  92.          ErrorCount++; /*This veriable is incramented when ever a error is detected and is used to count the number of errors.*/
  93.        }
  94.        
  95.  /*Checks that no uper case or speshal charicters as well as spaces are included in the cipher key length input.*/
  96.  
  97.     for(ErrorLoop1 = 0; ErrorLoop1 < CipherKeyLength; ErrorLoop1++)
  98.         {
  99.           if(CipherKey[ErrorLoop1] < 'a' || CipherKey[ErrorLoop1] > 'z')
  100.                {
  101.                   printf("This Cipher key is invaid.\nThe input Cipher Key must not contain any capitalised characters, spaces or speshal characters.\n\n");
  102.                   ErrorCount++;
  103.                }
  104.         }
  105.        
  106.  /*Checks that the cipher key length contains no duplicate characters.*/
  107.  
  108.     for(ErrorLoop1 = 0; ErrorLoop1 < CipherKeyLength; ErrorLoop1++)
  109.         {
  110.     for(ErrorLoop2 = 1; ErrorLoop2 < CipherKeyLength; ErrorLoop2++)
  111.         {
  112.           if(CipherKey[ErrorLoop1] == CipherKey[ErrorLoop2] && ErrorLoop1 != ErrorLoop2)
  113.                {
  114.                   printf("This Cipher key is invaid\nThe input Cipher key must not contain any duplicate characters.\n\n");
  115.                   ErrorCount++;
  116.                   ErrorLoop1 = CipherKeyLength;
  117.                }
  118.         }
  119.         }
  120.        
  121.  /*Checks wether the value of the input plain text length is within the alowed paramaters.*/
  122.  
  123.     if(LengthofPlainText < 4 || LengthofPlainText > 256)
  124.        {
  125.          printf("The length of the input plain text is invalid.\nThe input length of the plain text must be greater than 4 and no more that 256 characters long.\nThe number of characters includes spaces.\n\n");
  126.          ErrorCount++;
  127.        }
  128.        
  129.  /*Checks wether the value of the plain text length matches the number of characters in the plain text.*/
  130.  
  131.     for(ErrorLoop1 = 0; ErrorLoop1 < LengthofPlainText; ErrorLoop1++)
  132.         {
  133.           if(PlainText[ErrorLoop1] == '\0')
  134.                {
  135.                   ErrorLoop3++;
  136.                }
  137.         }
  138.  
  139.     if(PlainText[LengthofPlainText] != '\0')
  140.        {
  141.          ErrorLoop3++;
  142.        }
  143.  
  144.     if(ErrorLoop3 != 0)
  145.        {
  146.           printf("This plain text is invaid.\nThe number of characters in the input Plain Text dose not match the number stated in the Plain Text Length\n\n");
  147.         ErrorCount++;
  148.        }
  149.        
  150.  /*Checks wether the plain text contains any capitalised or special characters*/
  151.  
  152.     for(ErrorLoop1 = 0; ErrorLoop1 < LengthofPlainText; ErrorLoop1++)
  153.         {
  154.           if((PlainText[ErrorLoop1] < 'a' || PlainText[ErrorLoop1] > 'z') && (PlainText[ErrorLoop1] != ' ') && (PlainText[ErrorLoop1] != '\0'))
  155.                {
  156.                   printf("This plain text is invaid.\nThe input plain text must not contain any capitalised or special characters.\n\n");
  157.                   ErrorCount++;
  158.                }
  159.         }
  160.        
  161.  /*Checks wether the ErrorCount is anything other than 0, and ends the program befor it performs the character substitution if any errors were detected.*/
  162.  
  163.     if(ErrorCount >= 1)
  164.        {
  165.          printf("The Program has encountered 1 or more errors and will not execute.\nThe causes of these errors are printed above.");
  166.          return 0;
  167.        }
  168.  
  169.  /* scenario validation */
  170.  
  171.  /*Checks */
  172.  
  173.     for(CipherKeyValidator = 0; CipherKeyValidator < CipherKeyLength; CipherKeyValidator++)
  174.         {
  175.             loopCount++;
  176.             for(PlainTextValidator = 0; PlainTextValidator < LengthofPlainText; PlainTextValidator++)
  177.                 {
  178.                   if(PlainText[PlainTextValidator] == CipherKey[CipherKeyValidator])
  179.                      {
  180.                         PlainTextSubstitution[PlainTextValidator] = CipherKey[CipherKeyValidator + 1];
  181.                            }
  182.                      }
  183.  
  184.  /* character substitution */
  185.  
  186.              if(CipherKeyValidator == CipherKeyLength -1)
  187.                      {
  188.                         for(PlainTextValidator =0; PlainTextValidator < LengthofPlainText; PlainTextValidator++)
  189.                             {
  190.                              if(PlainText[PlainTextValidator] == CipherKey[CipherKeyValidator])
  191.                             {
  192.                               PlainTextSubstitution[PlainTextValidator] = CipherKey [0];
  193.  
  194.  /* Result printing */
  195.  
  196.                           printf("Step %d replace %c with %c\n", loopCount, CipherKey[CipherKeyValidator], CipherKey[0]);
  197.                               printf("%s\n", PlainTextSubstitution);
  198.                               return 0;
  199.                             }
  200.                             }
  201.                      }
  202.  
  203.                   printf("Step %d replace %c with %c\n", loopCount, CipherKey[CipherKeyValidator], CipherKey[CipherKeyValidator + 1]);
  204.                   printf("%s\n", PlainTextSubstitution);
  205.  
  206.         }
  207.  
  208.    }