<mosaic.cnfolio.com>
PROGRAM
  1. /*START
  2. Take IN the length of the cipher key
  3. Take IN the cipher key text
  4. Take IN the length of the plain text
  5. Take IN the plain text
  6. IF the length of cipher key is less than two or more than 16
  7.    Print “Error: Cipher key MUST be 2-16 characters long “
  8.    Terminate program
  9. Else
  10.    IF the length of the plain text is less than 4 or more than 256
  11.       Print “Error: Text MUST be 4-256 characters long “
  12.       Terminate program
  13.    Endif
  14. Endif
  15. Copy PLAIN TEXT to another string called PLAIN TEXT REF
  16. Create a string called FINAL TEXT
  17. Initialize WHILE COUNTER to zero
  18. */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. //#include <ctype.h>
  24.  
  25. int main(){
  26.  
  27.    int cipherkeylength;
  28.    char key[16];
  29.    int textlength;
  30.    char text[256];
  31.    char target_character[1];
  32.    char substitution_character[1];
  33.    int outer_loop, inner_loop;
  34.    char text_final[256];
  35.    
  36.    
  37.    
  38.    scanf("%d", &cipherkeylength);         // Take in  the length of cipher key
  39.  
  40.    scanf("%s", &key);         // Take in the cipher key
  41.    
  42.    scanf("%d", &textlength);      // Take in  the length of text
  43.  
  44.    scanf("%[^/n]%*c", &text);   // Take in the text
  45.    
  46.    
  47.    strcpy(text_final, text);      // copying text into text_final for later use
  48.    
  49.    
  50.    if ( 1<cipherkeylength && cipherkeylength<17 ) {         // checking if key length is within limits
  51.      
  52.       if ( cipherkeylength==strlen(key) ){         // checking if inputted length is the same as acctual length
  53.          
  54.          if ( 3<textlength && textlength<257 ){   // checking if text length is within limits
  55.            
  56.             if ( textlength==strlen(text)-1 ){      // checking if inputted length is the same as acctual length
  57.                
  58.                
  59.                for ( outer_loop=0; outer_loop<cipherkeylength; outer_loop++){   // outer loop controls target and character values
  60.                   
  61.                   target_character[1] = key[ (cipherkeylength - (cipherkeylength -0)) + outer_loop];
  62.                   substitution_character[1] = key[ (cipherkeylength - (cipherkeylength -1)) + outer_loop];
  63.                   if ( ((cipherkeylength - (cipherkeylength -2)) + outer_loop) > cipherkeylength ){
  64.                      substitution_character[1] = key[ (cipherkeylength - (cipherkeylength -1)) -1 ];
  65.  
  66.                   }
  67.                     printf("\nstep %d: substitute %c with %c", outer_loop+1, target_character[1], substitution_character[1]);
  68.                    
  69.                  
  70.                   for ( inner_loop=0; inner_loop<=(textlength); inner_loop++ ){      // inner loop controls encrypting
  71.                      
  72.                      if ( text[inner_loop] == text_final[inner_loop] && text[inner_loop] == target_character[1]){   // checkes if the character being looked at has not been changed befor && checkes if it is the same as the target value
  73.                      
  74.                         text_final[inner_loop] = substitution_character[1];         // only if those two conditions are true it replaces the character
  75.                        
  76.                      }
  77.                      
  78.                   }
  79.                   printf("%s", text_final);
  80.                }
  81.                
  82.                
  83.                
  84.                
  85.             }
  86.             else{
  87.                printf("\nacctual text length does NOT equal inputed text length!!");
  88.             }
  89.            
  90.          }
  91.          else{
  92.             printf("\nText Must be between 4-256 characters long!!");
  93.          }
  94.       }
  95.       else{
  96.          printf("\nacctual Key length does NOT equal inputed key length!!");
  97.       }
  98.    }
  99.    else {
  100.       printf("\nkey length MUST be between 2-16 characters long!!");
  101.    }
  102.    
  103.    
  104.    
  105.    
  106.    
  107.    
  108. }