<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.    Display greeting message
  4.    Get inputs for length of cipher key, cipher key text, length of plain text, plain text
  5.    IF no errors found in inputs
  6.       THEN
  7.          Proceed with step 1
  8.             Step 1 is to replace every instance of the first character in the cipher key with the following character in the cipher key
  9.          Proceed with steps until length of cipher key input is satisfied
  10.             Every step after step 1 is to replace every instance of the next character in the cipher key with the following character in the cipher key, until all of the cipher key has been read through the plain text
  11.       ELSE
  12.          Display relevant error message
  13.    END IF
  14. END
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. int main( void ){
  21.  int NumberOfSteps;
  22.  char CipherKey[ 16 ] = { '\0' };
  23.  int TextLength;
  24.  char PlainText[ 256 ] = { '\0' };
  25.  /*These have to be defined at the start of the program,
  26. as they are needed later on after the error checking 'block'
  27. and would not be usuable otherwise.*/
  28.  printf( "Welcome to the Plain Text Encryptor. Please input first, the length of the cipher key, then the cipher key text,\nfollowed by the length of the plain text to be encrypted, and finally the plain text to be encrypted.\n" );
  29.  
  30.  int ErrorFlag, ErrorCounter;
  31.  /*ErrorFlag is used to make sure that when the program does run fully, there are no errors being processed through the code,
  32. because it does not run through the actual code if the ErrorFlag is set active by an incorrect input.*/
  33.   for( ErrorCounter = 0; ErrorCounter < 1; ErrorCounter++ ){
  34.   /*This makes sure that the error checking segments only run once,
  35.   but can be changed to run multiple times for more thorough error checking.*/
  36.    if( scanf( "%d", &NumberOfSteps ) == 1 ){
  37.       if( NumberOfSteps < 2 ){
  38.          printf( "The number for the length of the cipher key is too low, and has to be greater than 1 but less than 17.\n" );
  39.          ErrorFlag = 1;
  40.       }
  41.       if( NumberOfSteps > 16 ){
  42.          printf( "The number for the length of the cipher key is too high, and has to be greater than 1 but less than 17.\n" );
  43.          ErrorFlag = 1;
  44.       }
  45.    }
  46.       else{
  47.          printf( "Please input data for the length of the cipher key.\n" );
  48.          ErrorFlag = 1;
  49.       }
  50.    
  51.    if( scanf( "%s", CipherKey ) == 1 ){
  52.       if( strlen( CipherKey ) != NumberOfSteps ){
  53.          printf( "The length of the cipher key is not equal to the number provided, %d.\n", NumberOfSteps );
  54.          ErrorFlag = 1;
  55.       }
  56.       int CheckingCharacter, ReadingCharacter;
  57.       /*CheckingCharacter is used to identify a specific character in the cipher key,
  58.       and ReadingCharacter is used to check every other character against the CheckingCharacter to make sure there are no duplicates.*/
  59.       for( CheckingCharacter = 0; CheckingCharacter < NumberOfSteps; CheckingCharacter++ ){
  60.          for( ReadingCharacter = CheckingCharacter+1; ReadingCharacter < NumberOfSteps; ReadingCharacter++ ){
  61.             if( CipherKey[CheckingCharacter] == CipherKey[ReadingCharacter] ){
  62.                printf( "No duplicate characters are allowed in the cipher key.\n" );
  63.                ErrorFlag = 1;
  64.             }
  65.          }
  66.          if( isupper( CipherKey[CheckingCharacter] ) ){
  67.             printf( "Uppercase characters are not allowed in the cipher key.\n" );
  68.             ErrorFlag = 1;
  69.          }
  70.       }
  71.    }
  72.       else{
  73.          printf( "Please input data for the cipher key text.\n" );
  74.          ErrorFlag = 1;
  75.       }
  76.    
  77.    if( scanf( "%d", &TextLength ) == 1 ){
  78.       if( TextLength < 4 ){
  79.          printf( "The number for the length of the plain text is too low, and has to be greater than 3 but less than 257.\n" );
  80.          ErrorFlag = 1;
  81.       }
  82.       if( TextLength > 256 ){
  83.          printf( "The number for the length of the plain text is too high, and has to be greater than 3 but less than 257.\n" );
  84.          ErrorFlag = 1;
  85.       }
  86.    }
  87.       else{
  88.          printf( "Please input data for the length of the plain text.\n" );
  89.          ErrorFlag = 1;
  90.       }
  91.    
  92.    if( scanf( "%256c", PlainText ) == 1 ){
  93.       if( strlen(PlainText)-1 != TextLength ){
  94.       /*Strings read end of file as a character, so "-1" is needed
  95.       after "strlen(PlainText)" to give the actual length value.*/
  96.          printf( "The length of the plain text is not equal to the number provided, %d.\n", TextLength );
  97.          ErrorFlag = 1;
  98.       }
  99.       int TextCharacter;
  100.       for( TextCharacter = 0; TextCharacter <= TextLength; TextCharacter++ ){
  101.          if( isupper( PlainText[TextCharacter] ) ){
  102.             printf( "Uppercase characters are not allowed in the plain text.\n" );
  103.             ErrorFlag = 1;
  104.          }
  105.       }
  106.    }
  107.       else{
  108.          printf( "Please input data for the plain text.\n" );
  109.          ErrorFlag = 1;
  110.       }
  111.   }
  112.    
  113.    if( ErrorFlag == 0 ){
  114.       int N, M, S;
  115.       /*Note: N is the current position in the cipher key,
  116.       M is the current position in the plain text,
  117.       S is the substitution value between the two.*/
  118.       char OutputText[ 256 ] = {'\0'};
  119.       /*This is a copy of PlainText that gets edited for the output,
  120.       so that the replaced letters do not get changed at the end as the cipher key loops around.*/
  121.       strcpy( OutputText, PlainText );
  122.       for( N = 0; N < NumberOfSteps; N++ ){
  123.          if( N == NumberOfSteps-1 ) S = 0;
  124.          else S = N+1;
  125.             printf( "\nStep %d substitute %c with %c", N+1, CipherKey[N], CipherKey[S] );
  126.                for( M = 0; M <= TextLength; M++ ){
  127.                   if( PlainText[M] == CipherKey[N] ) OutputText[M] = CipherKey[S];
  128.                }
  129.          printf( "%s\n", OutputText );
  130.       }
  131.    }
  132.    else printf( "Please amend error(s) in inputs before retrying the program.\n" );
  133. }