<mosaic.cnfolio.com>
PROGRAM
  1. /*START
  2.  
  3. Obtain cipher key length
  4. Obtain cipher key
  5. Obtain length of plain text
  6. Obtain plain text ready to be ciphered
  7.  
  8. WHILE character is between 2 and 16 including 2 and 16
  9.   IF character is not between 2 and 16 excluding 2 and 16
  10.       Display Error! You have gone above or below the limits of characters
  11.   END IF
  12.     WHILE No capital letters in the string
  13.       IF capital letters are present in the string
  14.         Display Error! You have an uppercase character in your input
  15.       END IF
  16.       WHILE no duplicates characters
  17.          IF there are duplicate characters in the input
  18.           Display Error! You have duplicates in your string
  19.          END IF
  20.          Input next character for the cipher key
  21.       END WHILE
  22.     END WHILE
  23.   END WHILE
  24.  
  25. WHILE character length is between 4 to 256 including 4 and 256
  26.   IF character is not between 4 and 256 excluding 2 and 256
  27.       Display Error! You have gone above or below the limits of characters
  28.   END IF
  29.   WHILE   No capital letters in string and space is including
  30.     IF capital letters are present in the string
  31.       Display Error! You have an uppercase character in your input
  32.     END IF
  33.    Input plain text to encrypt
  34.   END WHILE
  35. END WHILE
  36.  
  37. WHILE N plus the amount of times loop has reiterated is less than cipher key length
  38.   WHILE all target values are not substitution values
  39.         IF the character has not been substituted once before
  40.           Change every character that is the same as N+x into N+x+1
  41.                 IF the character at that point has already been changed
  42.               Do not change thecharacter and leave it as it is
  43.             END IF
  44.           Display step n: character A has been replaced by character B
  45.           Substitute all A characters into B characters cipher
  46.         END IF
  47.   END WHILE
  48. Output the new string with the cipher B key character
  49. END WHILE
  50. END
  51. =====================================================================================================================================*/
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54.  
  55. /*___Global Variables___*/
  56.  
  57.     int    ciph_key_length = 0;    /*The number of characters the cipher key has*/
  58.     char   ciph_key[16];           /*The cipher Key used to encrypt the text*/
  59.     int    text_length;            /*The number of characters the text_length has*/
  60.     char   text[256];              /*The input text that is to be encrypted*/
  61.     int    N = 0;                  /*Position of Cipher key*/
  62.     int    count;                  /*To count the number of charaacter swap*/
  63.     char   temp[256];              /*Used to store and hold the temporary encryption in process*/
  64.                                    /*This means the plain text is just copied and changed in temporary variable*/
  65.  
  66. char check_error(char detect)              /*If the error function doesn't return a 0, its an error*/
  67. {                              /*Parameter Detect used to check any variable for error*/
  68.    
  69.    if(detect <= 'z' && detect >= 'a')      /*Lowercase values included to avoid errors with special characters*/
  70.    {
  71.        /*Does nothing when it is lowercase values*/
  72.    }
  73.    
  74. /*___Capital Letter(s) error detection__*/
  75.  
  76.    else if(detect <= 'Z' && detect >= 'A')   /*Error checking for Capital Letters*/
  77.    {
  78.        printf("Error! You have Capital Letter(s) in your input...\n");            /*Error Message*/      
  79.        return 0xFF;                  /*Returning a non-zero value to represent error*/
  80.    }
  81.    
  82. /*___Number(s) error detection___*/
  83.  
  84.    else if(detect >= '0' && detect <= '9')   /*Error checking for Numbers in character inputs*/
  85.    {
  86.        printf("Error! You have Number(s) present in your cipher or plain text\n")/*Error Message*/
  87.        return 0xFF;                  /*Returning a non-zero value to represent error*/
  88.    }
  89.                                        /*When there is an error, the value 0xFF will be returned*/
  90.    return 0;                           /*Returning a 0 will represent that there are no errors*/
  91. }
  92.  
  93. int main(void)
  94. {
  95. /*___Inputs___*/
  96.  
  97.     scanf("%d", &ciph_key_length);        /*Take the value of the cipher key length for loop*/
  98.     if(ciph_key_length < 2 || ciph_key_length > 16)   /*Error check for the cipher key length*/
  99.     {
  100.       printf("Error! You are not in the limit of cipher length...\n")/*Error Message*/
  101.       return 0xFF;                        /*Return value to stop program once error is found*/
  102.     }
  103.    
  104.     for(count = 0; count <= ciph_key_length; count++) /*Setting limit for the program to stop*/     
  105.     {
  106.       scanf("%c", &ciph_key[count]);      /*Take each character of the cipher key one by one*/ 
  107.    
  108.         if(check_error(ciph_key[count]) != 0)   /*Checking for error in cipher text*/
  109.          {
  110.            return 0xFF;      /*Return value to stop program once error is found*/
  111.          }
  112.     }
  113.    
  114.     scanf("%d", &text_length);            /*Take the value of the text key length for loop*/
  115.     if(text_length < 4 ||text_length > 256)        /*Error check for text length*/
  116.     {
  117.       printf("Error! You are not in the limit of text length..\n")/*Error Message*/
  118.       return 0xFF;                        /*Return value to stop program once error is found*/
  119.     }
  120.    
  121.     for(count = 0; count <= text_length; count++)   
  122.     {
  123.       scanf("%c", &text[count]);          /*Take each character of the plain text one by one*/
  124.       temp[count] = text[count];          /*Hold the characters in a Temporary variable*/
  125.      
  126.         if(check_error(text[count]) != 0)        /*Checking for error in plain text*/
  127.          {
  128.           return 0xFF;      /*Return value to stop program once error is found*/
  129.          }
  130.     }
  131.    
  132. /*___Encryption___*/
  133.  
  134.   for(N = 1; N <= ciph_key_length; N++)      /*Used per character to meet the required length of cipher length*/
  135.   {
  136.      if(N == ciph_key_length)                /*Once the right no of elements of the cipher are indicated perform task*/
  137.       {
  138.         for(count = 1; count <= text_length; count++)
  139.          {
  140.             if(text[count] == ciph_key[N])
  141.             {
  142.               N=1;                       /*Used because arrays start at 1*/
  143.               temp[count] = ciph_key[N];     /*The original text is copied so only temporary copy is changed in encryption*/
  144.             }
  145.          }
  146.  
  147. /*___Output___*/
  148.  
  149.          N = ciph_key_length;          /*Setting indicator to the length of the cipher*/
  150.          printf("\nStep %d replace %c with %c\n", N, ciph_key[N], ciph_key[1])/*Output for each step of encryption process*/
  151.            
  152.          for(count = 1; count <= text_length; count++)/*Repeat until all the characters of plain text is met*/
  153.          {
  154.            printf("%c", temp[count])/*Printing out the encrypted copy from the original text*/
  155.          }
  156.          break;
  157.       }
  158.      
  159.       for(count = 1; count <= text_length; count++)   /*Repeat until all the characters in the plain text has been checked*/
  160.       {
  161.      
  162.         if(text[count] == ciph_key[N])       /*Perform when a character of cipher key and plain text are the same*/
  163.          {
  164.            temp[count] = ciph_key[N + 1];    /*Hold the encrypted step into the temporary variable*/
  165.          }
  166.       }
  167.      
  168.       printf("\nstep %d replace %c with %c\n", N, ciph_key[N], ciph_key[N + 1])/*Repetition of encryption steps label*/
  169.       for(count = 1; count <= text_length; count++)
  170.       {
  171.           printf("%c", temp[count]);        /*Show each encrypted text until the final encryption text*/
  172.       }
  173.      
  174.    }
  175.    
  176.   return 0;           /*End*/
  177. }