<mosaic.cnfolio.com>
PROGRAM
  1. /* >>>-----------------------------------------Pseudocode-------------------------------------------------<<<
  2. /*
  3. Pseudocode
  4. Declare variables and initialise
  5. Read in data:
  6.    READ in length of keycode and keycode
  7.    READ in length of Plain message and Plain message loop until all message is read
  8.    
  9. Validate inputted keycode data:
  10.    IF KeyCode length not same as inputted length THEN
  11.       PRINT "error message -- wrong keycode length"
  12.       SET invalid_flag to True
  13.    
  14.    IF KeyCode is not betweeen 2 and 16 characters THEN
  15.       PRINT "error message -- keycode must be between 2 and 16 characters"
  16.       SET  invalid_flag to True
  17.       
  18.    DO UNTIL each element of KeyCode have been compared to those ahead
  19.       LOOP through comparing keycode to all those positioned ahead
  20.       IF duplicate letters found THEN
  21.          PRINT "error message -- Duplicate letters not allowed in keycode"
  22.          SET invalid_flag to True
  23.          
  24.    DO UNTIL each element of keyCode have been compared to allowed characters without a space
  25.         IF Invalid character found THEN
  26.          PRINT "error message -- Only alphabetic letters and no spaces allowed in keyCode"
  27.          SET invalid_flag to True   
  28.          
  29. Validate inputted Plain message data: 
  30.    IF Plain Message length not same as inputted length THEN
  31.       PRINT "error message -- message length value not same as length of message"
  32.       SET invalid_flag to True
  33.    
  34.    IF Plain Message is not betweeen 4 and 256 characters THEN
  35.       PRINT "error message  -- Message must be between 4 and 256 characters"
  36.       SET invalid_flag to True
  37.    
  38.    DO UNTIL each element of Plain Message have been compared to allowed characters including space
  39.         IF Invalid character found THEN
  40.          PRINT "error message -- Only alphabetic letters and spaces allowed in message"
  41.          SET invalid_flag to True   
  42.  
  43.          
  44. Checking if all input data is valid:
  45.  
  46.    IF invalid_flag is equal to False -- data has no errors THEN
  47.       can proceed to cipher Plain Message:
  48.    
  49.       COPY Plain Message to Encrypted Message so encrypted message has starting point
  50.  
  51.       DO UNTIL all characters in KeyCode have been incremented one step at a time
  52.          LOOP through Plain Message by incrementing one character at a time
  53.             compare each character of plain message against keycode
  54.             IF a match found THEN
  55.                IF keycode character is last value THEN
  56.                   SET: cipher character to first in keycode
  57.                ELSE
  58.                   SET: cipher character to next keycode character
  59.                ENDIF
  60.             
  61.             SET character at this position in encrypted message with cipher character
  62.             PRINT "substitition has taken place at step N - character P replaced with X"
  63.          Exit loop on Plain message (ie when no more next character positions)
  64.       Exit Until Loop (ie when last keycode has been used)
  65.       
  66.    When loop completed:
  67.       PRINT "Plain Message has been Encrypted to <ciphertext>"
  68.  
  69. (Exit program)
  70. * >>>--------------------------------------- End of pseudocode --------------------------------------------<<<*/
  71.  
  72. #include <stdio.h>
  73. #include <string.h>
  74.  
  75. int main( void )
  76. {
  77. /*
  78. //-------------------------Declare and initalise variables ------------------------------------------------//
  79. note:
  80. keylen holds length of keyCode
  81. keyCode holds the cipher key
  82. msglen holds message length
  83. msgPlain holds the input plain text message
  84. msgEncrypt will hold the ciphered text
  85. msg is a temporary holding array to enable input of all words in plain text
  86. j,n are used for loop counting
  87. vaild_data set to 1 to represent true and get changed to 0 if invaild data
  88. nNext will hold position of substitute letter in keyCode
  89. allowed initially holds upper and lower case alphabet letters then will have a space concatenated to it
  90.  
  91. */
  92. char keyCode[15], msgPlain[256], msgEncrypt[256], allowed [] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  93. char msg[256]="\0";
  94. int keylen, msglen, j, n, nNext, limitChecked;
  95. int valid_data = 1; //set flag to True
  96. /*
  97. //-------------------------get input------------------------------------------------//
  98. */
  99. scanf ("%d",&keylen); // Get key length //
  100. scanf ("%s",&keyCode); // Get keyCode //
  101. scanf ("%d",&msglen); // Get Message length //
  102. scanf ("%s",&msgPlain); // Get first word of message //
  103. while (strlen(msgPlain) < msglen) //loop until all message read //
  104. {
  105.    scanf ("%s",&msg); // read next word //
  106.    strcat (msgPlain, " "); // append a space //
  107.    strcat (msgPlain, msg); // append next word to message //
  108. } // end loop when all message words are read //
  109.  
  110. /*
  111. //-------------------------validate the input data------------------------------------------------//
  112. */
  113.  
  114. /*
  115. //-------------------------validating keyCode inputs---------------------------------------------------//
  116. */
  117.  
  118. if (keylen < 2 || keylen > 16) // testing for valid length between 2 and 16 //
  119. {
  120. printf ("Length of keycode must be between 2 and 16- you entered %d", keylen); // print error //
  121. valid_data = 0; // set flag value to 0 for invalid data //
  122. }
  123.  
  124. if (keylen != strlen(keyCode)) // testing the keyCode length to see if it meets the keylen input value //
  125. {
  126.    printf ("Length of keycode: %s is not length you said it was going to be: %d", keyCode,keylen); // print error //
  127. valid_data = 0; // set flag value to 0 to indictate invalid data //
  128.  
  129. }
  130.  
  131. for (n = 0; n < keylen ; n++) // looping to check they are no duplicate values in keyCode //
  132. {
  133.    limitChecked = n + 1;
  134.    for (j = limitChecked; j < keylen; j++)
  135.    {
  136.       if (keyCode[n] == keyCode[j])
  137.       {
  138.          printf ("\n duplicate letters not allowed in keycode - %c = %c", keyCode[n], keyCode[j]); // output error //
  139.          valid_data = 0; // set flag value to 0 to indictate invalid inputting data //
  140.       }
  141.    } //next j
  142.  
  143. } //next n
  144.  
  145. for (n = 0; n < keylen; n++) // nested loops to ensure letters only and no spaces in keyCode //
  146. {
  147.    
  148.    for (j = 0; j < strlen(allowed); j++)
  149.    {
  150.       if (keyCode[n] == allowed[j])
  151.       { j=strlen(allowed);
  152.      
  153.       } else {
  154.       if (j > strlen(allowed)) {
  155.          printf ("\n invalid letter %c found in keyCode - found at position %d", keyCode[n], n);
  156.          valid_data = 0; // set flag value to 0 to indictate invalid inputting data //
  157.          }
  158.       }
  159.    } //next j
  160.  
  161. } //next n
  162. /*
  163. //---------------------------validating Message inputs-------------------------------------------------------//
  164. */
  165. if (msglen < 4 || msglen > 256) // checking message length between 4 and 256 long //
  166. {
  167. printf ("Length of message must be between 4 and 256 - you entered %d", keylen);
  168. valid_data = 0; // set flag value to 0 to indictate invalid inputting data //
  169. }
  170. if (msglen != strlen(msgPlain)) // checking if message input length is not the same as length message //
  171. {
  172.    printf ("Length of message: %s is not length you said it was going to be: %d", msgPlain,msglen);
  173. valid_data = 0; // set flag value to 0 to indictate invalid inputting data //
  174.  
  175. }
  176. strcat (allowed," "); //appending space to allowed letters in plain text //
  177. for (n = 0; n < msglen; n++) // nested loops comparing each message letter against allowed letters + spaces //
  178. {
  179.    
  180.    for (j = 0; j < strlen(allowed); j++)
  181.    {
  182.       if (msgPlain[n] == allowed[j])
  183.       { j=strlen(allowed);
  184.      
  185.       } else {
  186.       if (j > strlen(allowed)) {
  187.          printf ("\n invalid letter %c found in message - found at position %d", msgPlain[n], n);
  188.          valid_data = 0; // set flag value to 0 to indictate invalid inputting data //
  189.          }
  190.       }
  191.    } //next j
  192.  
  193. } //next n
  194. /*
  195. //---------------------------------------end of validation testing-------------------------------------------//
  196. */
  197.  
  198. if (valid_data == 0) // if any data not vaild output error message end program //
  199. {
  200. printf ("\n Failed validation - Please correct data and retry.");
  201.  
  202.  
  203. } else {
  204. /*
  205. //---------------------------------------validation was successful so now encrypt-----------------------------//
  206. */
  207. printf ("\n \n Validated Plain message is:   %s", msgPlain);
  208. printf ("\n Applying cipher now ... \n");
  209. strcpy (msgEncrypt, msgPlain); // copy plain text into cipher text array //
  210.  
  211. for (n=0; n < keylen; n++) // loop through each keyCode letter //
  212. {
  213.       nNext=n+1; // this is the position of the substitute letter //
  214.       if (n == keylen -1) nNext =0; // if last letter roll back to first letter in keyCode for substitute //
  215.      
  216.       for (j=0; j< msglen; j++) // loop through each letter in plain text //
  217.       {
  218.          if (msgPlain [j] == keyCode [n]) // if match found then cipher //
  219.          {
  220.             msgEncrypt [j] = keyCode [nNext]; // replace letter in message with keyCode //
  221.             printf ("\n Step %d sub %c with %c", n, msgPlain [j], keyCode [nNext]);
  222.             printf ("\n Cipher applied so far:   %s", msgEncrypt);
  223.          
  224.          //} else { // not now required as have use strcpy () above //
  225.             //if ( n == 0) msgEncrypt [j] = msgPlain [j]; //first time round fill encrypt 
  226.          
  227.          }
  228.      
  229.       }// Next j
  230.      
  231.  
  232. }// next n
  233.  
  234. printf ("\n \n Cipher applied - Fully Encrypted Message is:   %s", msgEncrypt); // finally output ciphered text
  235.  
  236. }
  237.  
  238. /*
  239. //-------------------------------end program main---------------------------------------//
  240. */
  241.  
  242. }