<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2. #include <ctype.h>   /* this header file is used to define different character types */
  3.  
  4. /*
  5. START
  6.    WRITE Enter length of cipher key;
  7.    READ  keylength;
  8.    
  9.       IF keylength < 2 THEN PRINT "Error" and READ keylength;
  10.       IF keylength > 16 THEN PRINT "Error" and READ keylength;
  11.      
  12.    WRITE Enter Cipher key;
  13.    
  14.       Intialize counter to 0;
  15.       WHILE counter less than or equal to keylength;
  16.          READ Cipher key;
  17.          Cipher key is equal to cipher array [ counter];
  18.             IF key = [A-Z] THEN WRITE "Error" and READ key;
  19.             IF plaintext = [0-9] THEN WRITE "Error" and READ key;
  20.          increase counter by one;
  21.          
  22.             intialize counter2 to 0;
  23.             WHILE counter less than or equal to keylength;
  24.                IF key == keycipherarray [counter2] THEN WRITE "Error" and READ key;
  25.             increase counter2 by one;
  26.             END WHILE
  27.       END WHILE
  28.      
  29.      WRITE "please enter the length of your plain text";
  30.      READ   lengthofplaintext;
  31.      
  32.      IF lengthofplaintext < 4 THEN WRITE "Error" and READ lengthofplaintext;
  33.      IF lengthofplaintext > 256 THEN WRITE "Error" and READ lengthofplaintext;
  34.            
  35.      WRITE "Enter your text to be encrypted";
  36.        
  37.          intialize counter3 to 0;
  38.          WHILE plain text >= 4;
  39.          WHILE plain text <= 256;
  40.             READ plain text;
  41.             plain text = textarray[ counter3 ];
  42.                IF plaintext = [A-Z] THEN WRITE "Error" and READ plain text;
  43.                IF plaintext = [0-9] THEN WRITE "Error" and READ plain text;
  44.        END WHILE
  45.          
  46.        intialize counter4 to 0;
  47.          intialize N to 0;
  48.          WHILE N less than or equal to length of the cipher key;
  49.             cipher array [counter4] = target value;
  50.             cipher array [counter4 + 1] = substitution value;
  51.                IF N = length of the cipher key THEN substitution value = cipher array[0];
  52.                IF plain text characters = cipher array [counter4] THEN plain text characters = cipher array [counter4 + 1];
  53.             WRITE "step N substitute the target value with the substitution value";
  54.             WRITE plain text;
  55.          increase N by 1;
  56.          increase counter4 by 1;
  57. END
  58. */
  59.  
  60. int main()
  61. {
  62.    int keylength = 0, x = 0, y = 0, z = 0, lengthofplaintext = 0, N = 0, error = 0, g = 0;
  63.    int step = 1,i = 0, j = 0, k = 0, f = 0, A = 0;
  64.    char key, cipherkey[16], plaintext, textarray[256], targetval, subvalue;
  65.    
  66.    
  67. /**************************************** keylength input **************************************/
  68.  
  69.       //printf("enter your key length \n");
  70.       scanf("%d",&keylength)// keylength input
  71.      
  72.          if (keylength < 2)   // if function, preventing the input from being less than 2
  73.          {
  74.             printf("Error, incorrect input, key length cannot be less than 2. \n");
  75.             error++;
  76.          }
  77.          
  78.          if (keylength > 16)  // if function, preventing the input from being greater than 16
  79.          {
  80.             printf("Error, incorrect input, key length cannot be greater than 16.\n");
  81.             error++;
  82.          }
  83.          
  84. /******************************************** key input ****************************************/
  85.  
  86.     //printf("enter your key \n");
  87.          for ( x = 0; x <= keylength; x++)
  88.          {
  89.             scanf("%c",&cipherkey[x])//input for the cipher key
  90.            
  91.            
  92.                if (isupper(cipherkey[x])) //checks if the input is a upper case letter
  93.                {
  94.                     printf("Error, upper case letter, please try again. \n");
  95.                     error++;
  96.                }
  97.                
  98.                if (isdigit (cipherkey[x])) //checks if the input is a lower case letter
  99.                {
  100.                 printf("Error, incorrect input, cipher key cannot have numbers. \n");
  101.                   error++;
  102.                }
  103.                
  104.                   
  105.                if (cipherkey[x] == cipherkey[x-1])  //checks if there are any duplicate key values
  106.                {
  107.                   printf("Error, incorrect input, you cannot have duplicate key values. \n");
  108.                   error++;
  109.                }
  110.                  
  111.              //key = cipherkey[x];  // assigns the input a location in the key array
  112.          }
  113.          
  114. /************************************ length of plain text input *******************************/ 
  115.  
  116.      //printf("enter the length of the plain text \n");   
  117.      scanf("%d",&lengthofplaintext); // input for the length of the plain text
  118.       
  119.        if (lengthofplaintext < 4)  // checks if the length is  less than 4
  120.          {
  121.             printf("Error, incorrect input, length of plain text can't be less than 4. \n");
  122.             error++;
  123.          }
  124.          
  125.          if (lengthofplaintext > 256)  // checks if the length is  greater than 256
  126.          {
  127.             printf("Error, incorrect input, length of plain text can't be greater than 256. \n");
  128.             error++;
  129.          }
  130.          
  131. /******************************************* plain text ****************************************/ 
  132.  
  133.      //printf("enter your plain text \n");   
  134.        for (z= 0; z <= lengthofplaintext; z++)
  135.          {
  136.             scanf("%c",&textarray[z])// plain text input
  137.            
  138.                
  139.             if (isupper( textarray[z])) // checks if there are any upper case letters in the input
  140.             {
  141.                 printf("Error, upper case letter, please try again. \n");
  142.                 error++;
  143.             }
  144.              
  145.             if ( isdigit( textarray[z]))  // checks if the input is a number
  146.             {
  147.                printf("Error, incorrect input, plain text cannot have numbers. \n");
  148.                error++;
  149.             }
  150.              
  151.              //plaintext = textarray[z]; // assigns the inputted text into the array
  152.          }
  153.          
  154. /************************************ testing variables ***************************************/
  155. /*
  156.     printf("%d \n", keylength);  //prints keylength (for testing purposes)
  157.    
  158.     for ( j = 0; j <= keylength; j++)    // prints the cipher key array
  159.     {
  160.         printf("%c",cipherkey[j]);
  161.     }
  162.     printf("\n");
  163.    
  164.     printf("%d \n",lengthofplaintext); //prints length of plain text
  165.    
  166.     for (i = 0; i <= lengthofplaintext; i++) //prints the plain text array
  167.     {
  168.         printf("%c", textarray[i]);
  169.     }
  170. */
  171.          
  172. /************************************* substitution *******************************************/
  173.  if (error < 1)   //only carries out the substitution if there is no input errors
  174.  {
  175.       for (N = 1; N <= keylength; N++)
  176.          { 
  177.             targetval = cipherkey[N]; // char at position n in the key is the target value
  178.            
  179.             if ( N == keylength )   // if N reaches the last char in the key
  180.             {
  181.                subvalue  = cipherkey[0];   // the first char becomes the sub value
  182.                
  183.             }
  184.             else
  185.             {
  186.                subvalue = cipherkey[N+1]// if N isn't at the last char in the key the char in the next position becomes the key
  187.             }
  188.            
  189.             for (A = 0; A <= lengthofplaintext - 1; A++)
  190.             {
  191.                if (textarray[A] == targetval)   // checking if the target value is equal to char in the text array
  192.                {
  193.                   textarray[A] = subvalue;   // swaping the target value in the text with the key
  194.                   printf("Step %d substitute %c with %c \n", step, targetval, subvalue)// shows the steps of the substituion
  195.                
  196.                      for ( g = 0; g <= lengthofplaintext; g++)
  197.            {
  198.                         printf("%C", textarray[g]); // printing the encrypted text
  199.                      } 
  200.                  
  201.                 printf("\n");
  202.                 step++; // increases the step
  203.                }
  204.                
  205.             }
  206.           }   
  207.  }
  208. }