<mosaic.cnfolio.com>
PROGRAM
  1. /*START
  2. Obtain cipher key length
  3. Obtain cipher key
  4. Obtain length of plain text
  5. Obtain plain text ready to be ciphered
  6. N is position 1 of the cipher key
  7.  
  8. WHILE the cipher key length is not between 2 and 16
  9.    IF the cipher key length is not between 2 and 16
  10.     reobtain the cipher key length
  11.    END IF
  12. END WHILE
  13.  
  14. WHILE the plain text length is not between 4 and 256
  15.    IF the plain text length is not between 4 and 256
  16.     reobtain the plain text length
  17.    END IF
  18. END WHILE
  19.  
  20. WHILE the cipher key length between 2 and 16
  21.  
  22.    
  23.    WHILE the cipher key has any uppercase letters and duplicates
  24.     Reobtain the cipher key
  25.    
  26.     IF the cipher key contains uppercase letters
  27.       Display there are uppercase letters in the cipher key which cannot be used
  28.     END IF
  29.      
  30.       IF there are any duplicates
  31.         Display the cipher key should not have any duplicates
  32.       END IF
  33.  
  34.   END WHILE
  35.  
  36.    
  37.    IF the cipher key is below 2
  38.     Display too few keys to work with
  39.   END IF
  40.  
  41.   IF the cipher key is above 16
  42.     Display too many keys in the cipher key
  43.   END IF
  44.  
  45. END WHILE
  46.  
  47. WHILE the plain text length is between 4 and 256
  48.  
  49.   WHILE the plain text contains upper case characters
  50.     Reobtain the plain text
  51.    
  52.     IF there are any uppercase letters
  53.       Display please enter in the plain text with lowercase letters only
  54.     END IF
  55.  
  56.   END WHILE
  57.  
  58.   IF the plain text length is below 4
  59.     Display plain text length too short
  60.   END IF
  61.  
  62.   IF plain text length is above 256
  63.     Display plain text is too long
  64.  
  65.   END IF
  66.  
  67. END WHILE
  68.  
  69. WHILE N+x<cipher key length where x is the amount of times the loop has reiterated
  70.  
  71.   WHILE every character hasn’t been checked
  72.   Change every character that is the same as N+x into N+x+1
  73.  
  74.   IF the character at that point has already been changed
  75.     Don’t change it
  76.   END IF
  77.  
  78.   END WHILE
  79.  
  80.   Display step N+x with the substitution values and the new text
  81.  
  82. END WHILE
  83. END*/
  84.  
  85. #include <stdio.h>
  86.  
  87. int errorchecking(char test)
  88. {
  89.   int error = 0;
  90.     if(test<='z'&&test>='a') //checks if the test character sent is a valid character and does nothing
  91.       {
  92.        
  93.       }
  94.      else if(test<='Z'&&test>='A')
  95.      {
  96.        printf("Capital letters have been entered please try again\n");
  97.        error=-1;
  98.      }
  99.     switch(test) // checks if there has been an integer value added into the character arrays and gets rid of them while asking for a new char value
  100.        {
  101.          case '0':
  102.          case '1':
  103.          case '2':
  104.          case '3':
  105.          case '4':
  106.          case '5':
  107.          case '6':
  108.          case '7':
  109.          case '8':
  110.          case '9':
  111.               error = -1; //returns a -1 as a return value, so that the when obtaining the char values it returns back to the previous space
  112.            printf("You have entered an integer value \n");
  113.            break;
  114.          default:
  115.            break;
  116.        }
  117.      /*if(test>='z'|| test<='a'|| test>='Z'|| test<='A')   //Supposed to take any non char values, or integer values and cause them to be overwritten
  118.      {
  119.        printf("Non english alphabet characters have been entered please try again\n");
  120.        error=-1;
  121.      }*/
  122.   return error;
  123. }
  124.  
  125. int main()
  126. {
  127.  
  128.   //======================Initialising variables=====================================
  129.    char cipherkey[16];
  130.    char plaintext[256];
  131.    char ciphertext[256];
  132.    int cipherkeylength=0;
  133.    int plaintextlength=0;
  134.    int count;
  135.  
  136.  
  137.   //=======================Obtaining cipher key length=============================== 
  138.    do
  139.    {
  140.       scanf("%d", &cipherkeylength);
  141.       if(cipherkeylength>16) //returns error when cipherkey length is too long
  142.       {
  143.         printf("Cipher key length is too long please shorten it.\n");
  144.       }
  145.       else if(cipherkeylength<2)//returns error when cipherkey length is too short
  146.       {
  147.         printf("Cipher key length is too short please.\n");
  148.       }     
  149.    } while(cipherkeylength>16 || cipherkeylength<2); //after obtaining the value for cipherkeylength it will stay within the loop
  150.    
  151.  
  152.   //==================Obtaining cipher key to be used=============================== 
  153.    for(count=0; count<=cipherkeylength; count++)
  154.    {
  155.      scanf("%c", &cipherkey[count]);
  156.      for(int count2 = 0; count2<count; count2++)//stops the program when a duplicate character is found in the cipher key
  157.      {
  158.        if(cipherkey[count2]==cipherkey[count])
  159.          {
  160.             printf("You have entered a duplicate character");
  161.             return;
  162.          }
  163.      }
  164.      count+=errorchecking(cipherkey[count]);     
  165.    } 
  166.   //========================Obtaining the plain text length to be used============== 
  167.    do
  168.    {
  169.      scanf("%d", &plaintextlength);
  170.      if(plaintextlength>4 && plaintextlength<256)//Stops the while loop if a valid value has been entered
  171.      {
  172.        break;
  173.      }
  174.      printf("You have entered a value that is not between 4 and 256. \n");
  175.    }while(plaintextlength<4 || plaintextlength>256);
  176.    
  177.  
  178.  
  179.   //=============Obtaining plain text to be and inserting plaintext into cipher text   
  180.    for(count=0; count<=plaintextlength; count++)
  181.    {
  182.      scanf("%c", &plaintext[count]);
  183.    
  184.      ciphertext[count]=plaintext[count]; //cipher text is the output, while plain text is the input this is to differentiate between the two in the code
  185.      
  186.      count+=errorchecking(plaintext[count]); //if there is an error code will go back and ask for a correct value
  187.      
  188.    }   
  189. //==Using the cipherkey to change the plain text into cipher text==================
  190.    for(int N=1; N<=cipherkeylength;N++)
  191.    {     
  192. //=======Replacing last character in cipher key with first character===============
  193.       if(N==cipherkeylength)
  194.       {
  195.         for(count=1; count<=plaintextlength;count++)
  196.          {
  197.           if(cipherkey[N]==plaintext[count])   //checks for the last character in the cipher key in the plain text     
  198.            {           
  199.                 N=1;           
  200.               ciphertext[count]=cipherkey[N]; //replaces the that characterwith the first character in the cipher key     
  201.            }
  202.          }         
  203. //=================Showing very last step==========================================
  204.        
  205.         N = cipherkeylength; //demonstrates the final step count       
  206.         printf("Step %d substitute %c with %c \n", N, cipherkey[N], cipherkey[1]);     
  207.         //This is to show the cipher text at that stage
  208.         for(count=1; count<=plaintextlength; count++)     
  209.         {           
  210.            printf("%c", ciphertext[count])//Prints the cipher text at the very end   
  211.         }       
  212.         break;     
  213.       }         
  214. //=========Regularly replacing cipherkey into plaintext============================       
  215.      for(count=1;count<=plaintextlength; count++)   
  216.       {       
  217.          if(cipherkey[N]==plaintext[count]) //Checks if the character in the plain text is the same as the current position being checked in the key       
  218.          {           
  219.             ciphertext[count]=cipherkey[N+1]; //Replaces the current character in the plain text with the next character in the key       
  220.          }     
  221.       }         
  222. //==============Displaying the steps and cipher text at each stage=================     
  223.       printf("Step %d substitute %c with %c \n", N, cipherkey[N], cipherkey[N+1] );     
  224.       //This is to show the cipher text at that stage
  225.      
  226.      for(count=1; count<=plaintextlength; count++)     
  227.       {           
  228.          printf("%c", ciphertext[count]); //prints the cipher text at each stage   
  229.       }
  230.      printf("\n");
  231.    }
  232.   return 0;
  233. }