<mosaic.cnfolio.com>
PROGRAM
  1. /* Pseudocode
  2.  
  3.    START
  4.    
  5.  
  6.    ---------------------------------------------------------------------------------------------------------
  7.                SELECTING CIPHER
  8.    
  9.    WHILE cipher key has to be 2-16 characters in length
  10.       IF Only lower case letters in Cipher
  11.          IF No Duplicates
  12.          IF Only alpha values
  13.          
  14.             
  15.          
  16.      
  17.      
  18.      ----------------------------------------------------------------------------------------------------
  19.               INPUTTING Text
  20.      
  21.       Input text (Plain Text)
  22.          IF Text must be 4-256 characters in length
  23.             IF Only lower case characters and spaces
  24.             IF Only alpha values
  25.          
  26.          
  27.      
  28.      -------------------------------------------------------------------------------------------------
  29.             INDICATION OF CURRENT POSITION IN CIPHER KEY
  30.          
  31.       IF indicate starting position (first character) = N+0 or N
  32.      
  33.          
  34.  
  35.    ---------------------------------------------------------------------------------------------------
  36.       
  37.             PERFORMING ENCRYPTION
  38.      
  39.      IF Character found at Position N is target value
  40.        IF Character found at Position N+1 is substituion value
  41.             IF N is last Character in Cipher Key, Substitution value is first character of Cipher Key
  42.               IF In the plain text, replace the target value with the subsitution value
  43.            IF Increment N by one
  44.                         IF Repeat all the steps
  45.      
  46.    
  47.    
  48.    --------------------------------------------------------------------------------------------------------
  49.    WHILE END
  50.  
  51.   END
  52.  
  53.  
  54.  
  55.  
  56.   -----------------------------------------------------------------------------------------------------------
  57.   */
  58.  
  59.   #include <stdio.h>
  60.   #include <string.h> // I used this string library since i used some some commands which are under this library
  61.  
  62.   // Variables
  63.  
  64.   int main (void)
  65.   {
  66.  
  67.   char encoderKey[16]; // The key for the encoder (Cipher)
  68.   int lengthEncoder=0; // The length of the encoder (Cipher)
  69.   int lengthText; // The length of the inputted text
  70.   char mainText[256];// How many characters that can be used
  71.   char changedText[256];// This stores and tracks the mainText when it is changed once it has been maniupulated
  72.   char substitute[16]; // Holds the encoderKey when the actual substitutions occurs
  73.   int stepCount=0; // Counts the number of steps the program has done in order for the output to be printed
  74.   int pointer=0// Used to store and track the positions of the characters from the mainText
  75.   int subPosition=0; // substitution checker
  76.   int encoderChecker = 0; // This pointer is for the Cipher key to mark which position it is on the main text
  77.   int textmainChecker = 0; // This pointer is for the mainText to see where the subsitution is occurring during the transfer on the characters within mainText
  78.   int tester; // This value is used for the Manipulation of the changedText
  79.   int marker; // Marks what goes on
  80.  
  81.  
  82.   //----------------------------------------------------------------------------------------------------------
  83.  
  84.   // Inputs
  85.  
  86.   scanf("%d",&lengthEncoder);
  87.    // Scanning the length of the cipher key
  88.  
  89.   scanf("%s",&encoderKey);
  90.     // Scanning the cipher key
  91.  
  92.   scanf("%d",&lengthText);
  93.     // Scanning the Length of the Plain text
  94.  
  95.  
  96.   scanf(" %[^\n]256",&mainText); // This line scans for the mainText that is entered by the user, it also scans any character the user has inputted within the 256 character range
  97.  
  98.   strncpy (substitute, encoderKey, sizeof (substitute)); // Copies the text that the user inputs
  99.  
  100.   //------------------------------------------------------------------------------------------------------------
  101.     // Checking for Errors
  102.      
  103.  //encoderKey error checking
  104.  
  105.    if(strlen(encoderKey)!=lengthEncoder)
  106.    {
  107.      printf("\n The length of your encoder key does not match the length of the encoder key that you have entered!");
  108.      return 0;
  109.    
  110.    } //This checks if the encoder key matches the length of the encoder key
  111.      
  112.     if(lengthEncoder > 16 || lengthEncoder < 2 ) {
  113.    printf("\n You have inputted insufficient or more than enough values. Please make sure that your Cipher key is at least 2 numbers long!");
  114.     return 0;
  115.        
  116.     }
  117.     // This checks whether the user has inputted the correct amount of characters for the Cipher key
  118.    
  119.     for(tester=0; tester<lengthEncoder; tester++) {
  120.    
  121.    
  122.     for(marker=tester+1; marker<lengthEncoder+1; marker++)
  123.     {
  124.         if(encoderKey[tester]==encoderKey[marker]) // Checks if the inputted the length of the encoder key actually matchces the length of encoder key
  125.             {
  126.               printf("\n There is a duplicate character in your encoder key, please change this character! ");
  127.               return 0;
  128.               }
  129.             }
  130.      
  131.     }
  132.    
  133.     for(encoderChecker=0; encoderChecker<lengthEncoder; encoderChecker++)
  134.       {
  135.          if (encoderKey[encoderChecker] >= '0' && encoderKey[encoderChecker] <= '9')
  136.             {
  137.            
  138.                printf("\n There are numeric values in your encoder key, please change to alpha values! "); // This part checks if they are any numerical numbers in the encoder.
  139.                   return 0;
  140.                  
  141.                   
  142.                   }
  143.                   }
  144.                  
  145.                  
  146.       for(encoderChecker=0; encoderChecker<lengthEncoder; encoderChecker++)
  147.      
  148.          {
  149.          
  150.             if (encoderKey[encoderChecker] >= 'A' && encoderKey[encoderChecker] <= 'Z') //This checks for Capital letters in the encoder key
  151.                
  152.                {
  153.                  printf("\n There are Capital letters in your encoder key, please change to lower case letters!");
  154.                  return 0;
  155.                
  156.                }
  157.           }
  158.    
  159.  
  160.  //------------------------------------------------------------------------------------------------------------------------------------------------------------------   
  161.  
  162.  //mainText error checking
  163.  
  164.    if(lengthText > 256 || lengthText < 4 ) {
  165.    printf("\n You have inputted insufficient or more than enough values. Please make sure that your plain text is between 4 and 256 characters!");
  166.    return 0;
  167.    } // This checks whether the user has inputted the correct amount of characters for the plain text
  168.    
  169.    for(textmainChecker = 0; textmainChecker < lengthText; textmainChecker++)
  170.   {
  171.  
  172.   if (mainText[textmainChecker] >= 'A' && mainText[textmainChecker] <= 'Z') //Checks if there are any Capital letters in the text string
  173.  
  174.   {
  175.     printf("\n There are capitals in your text! Please change to lowercase letters!");
  176.     return 0;
  177.       }
  178.   }
  179.    
  180.    for(textmainChecker=0; textmainChecker<lengthText; textmainChecker++)
  181.    {
  182.       if (mainText[textmainChecker] >= '0' && mainText[textmainChecker] <= '9') // Checks for numbers in the mainText
  183.      
  184.       {
  185.         printf("\n There numerical values in your text, please change to alpha characters!");
  186.         return 0;
  187.      
  188.       }
  189.    
  190.   }
  191.    
  192.    
  193.      
  194.      
  195.      
  196.   //-------------------------------------------------------------------------------------------------------------
  197.      
  198.       // Cipher Manipulation
  199.      
  200.      
  201.       subPosition = 0; //sets the position to 0 in order for the substitution to work properly
  202.       encoderChecker = 0; //sets the position to 0 in order for the substitution to work properly
  203.       stepCount = 0; //sets the position to 0 in order for the substitution to work properly
  204.       pointer = 0; //sets the position to 0 in order for the substitution to work properly
  205.      
  206.      
  207.      
  208.      
  209.   for(encoderChecker = 0; encoderChecker < lengthEncoder - 1; encoderChecker++) //The loop here starts checking each character in encoderKey
  210.   {
  211.      subPosition++; // each repeat of the code increases the variable (n+1)
  212.       stepCount++; // each repeat of the code increases the variable (n+1)
  213.  
  214.     for(textmainChecker = 0; textmainChecker < lengthText + 1; textmainChecker ++) // The loop here starts checking each character in the mainText
  215.       {
  216.             
  217.             if(mainText [textmainChecker] == encoderKey [encoderChecker]) // Checks here if the text is the same as the encoderKey and if so it performs another loop underneath
  218.              {
  219.                tester = 0; //Resets the variables to the position it needs to be in
  220.                marker = 0; //Resets the variables to the position it needs to be in
  221.                            for (tester = 0; tester < pointer; tester++) // New loop starts since there is an updated array
  222.                            
  223.                            {
  224.                               if (textmainChecker==changedText[tester]) // Checks if the text is actually stored in the array
  225.                                  {
  226.                                     marker++; // adds one more value to the marker if there is a duplicate
  227.                                     }
  228.                                   }
  229.                                     if (marker==0) // If there are no duplicates it doesn't add a value to the marker
  230.                                   {
  231.                                   mainText[textmainChecker] = substitute[subPosition]; // Updates the position
  232.                                   changedText[pointer] = textmainChecker; // Updates the position
  233.                                   pointer++; // the pointer is then moved into the next part
  234.                                  
  235.                                   }
  236.                                 }
  237.                                
  238.                               }
  239.                      
  240.                       printf ("\n Step %d Replace %c With %c ", stepCount,encoderKey[encoderChecker], substitute[subPosition]); //First part of the output
  241.                       printf ("\n %s", mainText); //Second part of the output
  242.                      
  243.                       }
  244.                      
  245.                 subPosition = 0; //sets the position to 0 in order for the substitution to work properly
  246.                 encoderChecker = lengthEncoder - 1;
  247.                 textmainChecker = 0; //sets the position to 0 in order for the substitution to work properly
  248.                 stepCount = lengthEncoder;
  249.                
  250.                 for (textmainChecker = 0; textmainChecker < lengthText + 1; textmainChecker++)
  251.                 {
  252.                
  253.                   if (mainText[textmainChecker] == encoderKey[encoderChecker])
  254.                      {
  255.                         tester=0;
  256.                         marker=0;
  257.                         for (tester = 0; tester < pointer; tester++)
  258.                       {
  259.                         if(textmainChecker==changedText[tester])
  260.                         {
  261.                            marker++;
  262.                            }
  263.                       }
  264.                
  265.                   if(marker==0)
  266.                      {
  267.                         mainText[textmainChecker]=substitute[subPosition];
  268.                         changedText[pointer]=textmainChecker;
  269.                         pointer++;
  270.                        
  271.                         }
  272.                }
  273.                
  274.               }
  275.              
  276.             printf ("\n Step %d Replace %c With %c ", stepCount,encoderKey[encoderChecker], substitute[subPosition]);
  277.             printf ("\n %s", mainText);
  278.             return 0;
  279.                    } 
  280.                              
  281.                            
  282.                       
  283.                        
  284.  //--------------------------------------------------------------------------------------