<mosaic.cnfolio.com>
PROGRAM
  1. /* Pseudo Code
  2. START
  3.  
  4. Input number for length of cipher key
  5. IF length is not between 2 and 16
  6.    THEN print "Error on input line 1. Please enter a number between 2 and 16 as your length."
  7.    End program
  8. END IF
  9.  
  10. Input text to be used as cipher key
  11. Create array of this text
  12. Initialize A to 0
  13. WHILE A is less than text length + 1
  14.    Assign the Ath value to array at position A
  15.    Increase A by 1
  16. END WHILE
  17. Initialize a to 0
  18. WHILE a is less than cipher length + 1
  19.    Initialize b to 0
  20.    WHILE b is less than array length
  21.       IF value a is equal to value b
  22.          THEN
  23.             Output "Error on input line 2. Please do not use duplicate letters."
  24.             End Program
  25.       END IF
  26.       Increase b by 1
  27.    END WHILE
  28.    Increase a by 1
  29. END WHILE
  30.  
  31. Input number for length of text
  32. IF length is not between 4 and 256
  33.    THEN print "Error on input line 3. Please enter a number between 4 and 256 as your length."
  34.    End program
  35. END IF
  36.  
  37. Input text to be encrypted
  38. Create array of this text
  39. Initialize A to 0
  40. WHILE A is less than text length + 1
  41.    Assign the Ath value to array at position A
  42. END WHILE
  43. Create identical array 'newtext' which will be changed during encryption, while the previous text array remains unchanged
  44. Find size of this array, add 1 and assign this new value to variable 'destination_size'
  45. Send output of original array to new array in order to fill both identically
  46.  
  47. Declare variable 'N' to indicate current position in the cipher key
  48. Declare variable for target value in text
  49. Declare variable for substitution value from cipher key
  50. Initialize N to 1
  51. WHILE N is less than key length + 1
  52.    Set target value to the value at position N in the key
  53.    Set substitution value to the value at position N + 1 in the key
  54.    Set null value at end of the key to the value at position 1
  55.    Output "Step number N: Substitute targetvalue with subvalue
  56.    Intialize a to 0
  57.    WHILE a is less than text length + 1
  58.       IF value at position a in the text is the same as the target value
  59.          THEN
  60.             Make value at position a of the new text the same as the substitution value
  61.       END IF
  62.       Output value at position a of new text
  63.       Increase a by 1
  64.    END WHILE
  65.    Increase N by 1
  66. END WHILE
  67.  
  68. END*/
  69.  
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72.  
  73. int main(void) {
  74.  
  75.   int keylength; /*This will be the length of the cipher key*/
  76.   scanf("%d", &keylength);
  77.   if(keylength < 2 || keylength > 16){ /*Error message to be printed if an incorrect number is entered*/
  78.       printf("Error on line 1. Please enter a number between 2 and 16.");
  79.       exit(0);
  80.     }
  81.    
  82.   char key[keylength]; /*This will be the cipher key*/
  83.   int a;
  84.   for(a = 0; a < keylength + 1; a++){
  85.      scanf("%c", &key[a]);
  86.   }
  87.  
  88.   int textlength; /*This will be the length of the text*/
  89.   scanf("%d", &textlength);
  90.   if(textlength < 4 || textlength > 256){ /*Error message to be printed if an incorrect number is entered*/
  91.     printf("Error on line 3. Please enter a number between 4 and 256.");
  92.     exit(0);
  93.   }
  94.  
  95.   char text[textlength]; /*This will be the text array that will not be changed*/
  96.   for(a = 0; a < textlength + 1; a++){
  97.      scanf("%c", &text[a]);
  98.   }
  99.  
  100.   char newtext[textlength + 1]; /*This is a new text array which will be changed*/
  101.   int arraysize = sizeof (newtext) + 1;
  102.   snprintf(newtext, arraysize, "%s", text); /*Copies the contents of the first text array to the new text array */
  103.   /* One array must not be changed because otherwise the encryption will swap letters that have already been swapped,
  104.   filling the output text with mostly the same letter and making decryption impossible.*/
  105.  
  106.   int N;
  107.   char targetvalue;
  108.   char subvalue;
  109.   for(N = 1; N < keylength + 1; N++){
  110.     targetvalue = key[N];
  111.     subvalue = key[N + 1];
  112.     key[keylength + 1] = key[1]; /*When N is the last value of the key, the substitution value will be the first value.*/
  113.     printf("Step %d: Substitute %c with %c. ", N, targetvalue, subvalue);
  114.     for(a = 0; a < textlength + 1; a++){
  115.       if(text[a] == targetvalue)/* When the target value is found in the text, that character will be swapped with subvalue.*/
  116.          newtext[a] = subvalue;
  117.          printf("%c", newtext[a]); /*Prints the encrypted text letter by letter.*/
  118.       }
  119.     printf("\n");
  120.     }
  121.    
  122.   return 0;
  123. }