<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2.    >>>> ---- ---- ---- ---- ---- ---- ---- ---- ---- - ---- ---- ---- ---- ---- ---- ---- ---- ---- <<<<
  3.    >>>> ---- ---- ---- ---- ---- ---- ----      Pseudo Code      ---- ---- ---- ---- ---- ---- ---- <<<<
  4.    >>>> ---- ---- ---- ---- ---- ---- ---- ---- ---- - ---- ---- ---- ---- ---- ---- ---- ---- ---- <<<<
  5.  
  6.    START
  7.    
  8.       Get Length of Cipher Key
  9.       Get Cypher Key Text
  10.       Get Length of Plain Text
  11.       Get Plain Text
  12.      
  13.       Check Length of Cipher Key is valid
  14.       Check Cypher Key Text is valid
  15.       Check Length of Plain Text is valid
  16.       Check Plain Text is valid
  17.      
  18.       FOR (Length of Cipher Key + 1)
  19.        FOR Length of Plain Text
  20.             Check Plain Text for First Character in Cipher Key
  21.                IF N Character is Found THEN replace with (N + 1) Character in Cypher Key
  22.                ELSE IF Character is Not found THEN Check for next character
  23.          LOOP
  24.       LOOP
  25.      
  26.       Output Encrypted String
  27.  
  28.    END
  29.  
  30. */
  31.  
  32. /* >>>> ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- <<<< */
  33. /* >>>> ---- ---- ---- ---- ---- ---- ----     MAIN PROGRAM    ---- ---- ---- ---- ---- ---- ---- <<<< */
  34. /* >>>> ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- <<<< */
  35.  
  36. #include <stdio.h>
  37.  
  38. int main() {
  39.  
  40. /* >>>> ---- ---- ---- ---- ---- ---- ---- ---- VARIABLES ---- ---- ---- ---- ---- ---- ---- ---- <<<< */
  41.  
  42.    int i = 0, j = 0, k = 0; /* Temporary variables for loops and counters */
  43.    int errors = 0; /* Used to flag if there are any errors with the input*/
  44.    int PlainTextCheckNull = 0, CipherKeyCheckNull = 0; /* Used to check for errors in entered plain text and cipher key */
  45.    int CipherKeyLength = 0, PlainTextLength = 0; /* Use integers to store Length of Cipher Key and Plain text as cant have fraction of a character. */
  46.    char CipherKey[16] = {'\0'}; /* Initialise all values as NULL characters. Array is one bigger than max size to allow first character to be copied to end of string during encryption.*/
  47.    char PlainText[255] = {'\0'}; /* Used to store original text. OK to use char as max length of text is 256. Char is 1 byte which can do 256 unique values. */
  48.    char EncryptedText[255] = {'\0'}; /* Used to store a duplicate of the plain text that gets manipulated during encryption stage, */
  49.    
  50. /* >>>> ---- ---- ---- ---- ---- ---- ---- ----   INPUT   ---- ---- ---- ---- ---- ---- ---- ---- <<<<  */
  51.  
  52.    scanf("%d", &CipherKeyLength);
  53.    
  54.    for (i = 0; i < CipherKeyLength; i++) {   /* Stores each character of the input text in a seperate place in the character array. */
  55.       scanf(" %c", &CipherKey[i])/* Space before %c makes the scanf() ignore whitespace */
  56.    }
  57.    
  58.    scanf("%d", &PlainTextLength); /* Gets the length of the Plain Text that is to be encrypted */
  59.    
  60.    for (i = 0; i < PlainTextLength; i++) {   /* Stores each character of the input text in a seperate place in the character array. */
  61.       scanf(" %[^\n]c", &PlainText[i]);      /* Space before %c makes the scanf() ignore whitespace. [^\n] ignores newlines */
  62.    }
  63.    
  64.    printf("\n---- ---- Substitution Cipher ---- ---- \n"); /* Pretty Output Formatting */
  65.    
  66. /* How program handles spaces needs improvement. Need to ignore integers in the string. */
  67.  
  68. /* >>>> ---- ---- ---- ---- ---- ---- ----   INPUT VALIDATION   ---- ---- ---- ---- ---- ---- ---- <<<<  */
  69.  
  70.    /* Checks Cipher Key Length entered is an integer */
  71.    if(CipherKeyLength == 0) {
  72.       printf("\nERROR: The Cipher Key Length entered is Invalid. It must be of an Integer length between 2 and 16 characters.");
  73.       errors++;   /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  74.    }
  75.    
  76.    /* Checks Plain Text Length is an integer*/
  77.    if(PlainTextLength == 0) {
  78.       printf("\nERROR: The Plain Text Length entered is invalid. It must be of an Integer length between 4 and 256 characters.");
  79.       errors++;   /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  80.    }
  81.    
  82.    /* Checks Cipher Key Length is between 2 and 16 Characters long */
  83.    if (CipherKeyLength < 2 && CipherKeyLength != 0) {
  84.       printf("\nERROR: The Cipher Key is too short. It must be of an Integer length between 2 and 16 characters.");
  85.       errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  86.    } else if (CipherKeyLength > 16 && CipherKeyLength != 0) {
  87.       printf("\nERROR: The Cipher Key is too long. It must be of an Integer length between 2 and 16 characters.");
  88.       errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  89.    }
  90.    
  91.    /* Checks Plain Text Length is between 4 and 256 Characters long */
  92.    if (PlainTextLength < 4 && PlainTextLength != 0) {
  93.       printf("\nERROR: The Plain Text is too short. It must be of an Integer length between 4 and 256 characters.");
  94.       errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  95.    } else if (PlainTextLength > 256 && PlainTextLength != 0) {
  96.       printf("\nERROR: The Plain Text is too long. It must be of an Integer length between 4 and 256 characters.");
  97.       errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  98.    }
  99.    
  100.    /* Checks Cipher Key Entered matches declared length */
  101.   for(i = 0; i < CipherKeyLength; i++) {
  102.       if(CipherKey[i] == '\0') {
  103.          CipherKeyCheckNull++; /* If entries within the bounds of array of the length declared by the user are still null (i.e. not updated since initialisation) this is incremented by 1 as means a mismatch between declared length and actual length. */
  104.       }
  105.    }
  106.    
  107.    if (CipherKey[CipherKeyLength] != '\0') {
  108.       CipherKeyCheckNull++;   /* If entries outside the bounds of array of the length declared by the user are not still null (i.e. updated since initialisation) this is incremented by 1 as means a mismatch between declared length and actual length. */
  109.    }
  110.    
  111.    if(CipherKeyCheckNull != 0) {
  112.       printf("\nERROR: Declared Length of Cipher Key does not equal entered Plain text Length.");
  113.     errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  114.    }
  115.    
  116.    /* Checks Plain Text Entered matches declared length */
  117.    for(i = 0; i < PlainTextLength; i++) {
  118.       if(PlainText[i] == '\0') {
  119.          PlainTextCheckNull++; /* If entries within the bounds of array of the length declared by the user are still null (i.e. not updated since initialisation) this is incremented by 1 as means a mismatch between declared length and actual length. */
  120.       }
  121.    }
  122.    
  123.    if (PlainText[PlainTextLength] != '\0') {
  124.       PlainTextCheckNull++;   /* If entries outside the bounds of array of the length declared by the user are not still null (i.e. updated since initialisation) this is incremented by 1 as means a mismatch between declared length and actual length. */
  125.    }
  126.    
  127.    if(PlainTextCheckNull != 0) {
  128.       printf("\nERROR: Declared Length of Plain Text does not equal entered Plain text Length.");
  129.     errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  130.    }
  131.    
  132.    /* Checks Cipher Key is all lowercase characters a-z and no spaces*/
  133.    for (i = 0; i < CipherKeyLength; i++) {
  134.       if (CipherKey[i] < 'a' || CipherKey[i] > 'z') {
  135.          printf("\nERROR: Cipher Key must only be lowercase characters a-z.");
  136.          errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  137.       } else if (CipherKey[i] == ' '){
  138.          printf("\nERROR: Space Detected. Cipher Key must only be lowercase characters a-z.");
  139.          errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  140.       }
  141.    }
  142.    
  143.    /* Checks Plain Text is all lowercase characters a-z (Spaces are valid) */
  144.    for (i = 0; i < PlainTextLength; i++) {
  145.       if ((PlainText[i] < 'a' || PlainText[i] > 'z') && (PlainText[i] != ' ') && (PlainText[i] != '\0')) {
  146.          printf("\nERROR: Plain Text must only be lowercase characters a-z.");
  147.          errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  148.       }
  149.    }
  150.  
  151.    /* Checks Cipher Key has no duplicate characters */
  152.    for (i = 0; i < CipherKeyLength; i++) {
  153.       for (j = 1; j < CipherKeyLength; j++) {
  154.          if (CipherKey[i] == CipherKey[j] && i != j){
  155.             printf("\nERROR: Cipher Key contains duplicate characters.");
  156.             errors++; /* Increments the Error Flag by 1 to show that an error has occoured so that no attempt is made to process bad input. */
  157.             i = CipherKeyLength; /* Ends the loop as soon as first duplicate is found */
  158.          }
  159.       }
  160.    }
  161.    
  162. /* >>>> ---- ---- ---- ---- ---- ---- ----  ENCRYPTION & OUTPUT  ---- ---- ---- ---- ---- ---- ---- <<<<  */   
  163.    
  164.    /* Makes a copy of the original string and stores it in the Encrypted text Array */
  165.    for(i = 0; i < PlainTextLength; i++) {
  166.       EncryptedText[i] = PlainText[i];
  167.    }
  168.    
  169.    CipherKey[CipherKeyLength] = CipherKey[0]; /* Copies first character in Cipher String and adds it to the end of the array. */
  170.    
  171.    /* Takes the original string to make comparisons on with cipher keys. But makes changes to the duplicate string. So that previous alterations dont get overwritten. */
  172.    /* Only runs if no errors have ben flagged */
  173.    if(errors == 0) {
  174.       for(i = 0; i < CipherKeyLength; i++) {
  175.          printf("\nStep %d. ", i + 1); /* Prints Current Step in Encryption Process */
  176.          printf("Substitute %c with %c", CipherKey[i], CipherKey[i+1])/* Prints current substitution comparison ongoing */
  177.          for(j = 0; j < PlainTextLength; j++) {
  178.             if((PlainText[j] == CipherKey[i]) && (i != CipherKeyLength)) {
  179.                EncryptedText[j] = CipherKey[i+1];
  180.             }
  181.          }
  182.          printf("\n");
  183.          for (k = 0; k < PlainTextLength; k++) { /* Prints encrypted string */
  184.             printf("%c", EncryptedText[k]);
  185.          }
  186.       }
  187.    }
  188.    
  189. }