<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.  
  4.     VARIABLE textlength, cipherlength, i, n, step, x
  5.     VARIABLE cipher[array], plaintext[array], targetvalue[array]
  6.  
  7.     //INPUT SECTION
  8.  
  9.     Input a value for cipherlength between 2 and 16
  10.     Display value of cipherlength entered
  11.     IF
  12.     Value of cipherlength is less than 2 or greater than 16
  13.     THEN
  14.     display error message
  15.     END IF
  16.    
  17.     Input characters for cipher[array] between 4 and 256
  18.     display the entered characters  of the cipher array
  19.     IF
  20.     the length of the cipher[array] is not equals to cipherlength
  21.     THEN
  22.     display error message
  23.     END IF
  24.    
  25.     Input a value for textlength between 2 and 16
  26.     display value of plaintext entered
  27.    
  28.     input characters for plaintext[array] between 4 and 256
  29.     display the entered characters of plaintext[array]
  30.     IF
  31.     length of plaintext[array] is not equals to textlength
  32.     THEN
  33.     display error message
  34.     END IF
  35.    
  36.     copy string from targetvalue to plaintext
  37.     make i equal to the length of cipher[array]
  38.    
  39.     //Substitution
  40.    
  41.     WHILE
  42.     n is less than the length of plaintext[array] add 1 to the value of n
  43.     IF
  44.     Target value is equals to cipher
  45.     THEN
  46.     make plaintext equal to the next character in the cipher array
  47.     END IF
  48.     END WHILE
  49.    
  50.     display the step number and subtitue the first character in the cipher array with the one at the next position
  51.    
  52.     WHILE
  53.     step is less than the value of i minus 1 increment step by 1
  54.     WHILE n is less than the length of plaintext increment n by 1
  55.     IF
  56.     cipher is equal to targetvalue
  57.     THEN
  58.     make plaintext equal to the cipher [array]
  59.     END IF
  60.     END WHILE
  61.     END WHILE
  62.    
  63.     display the step number and substitute the second character in the array with the next one
  64.    
  65.     WHILE
  66.     n is less than the length of plaintext increment n by 1
  67.     IF
  68.     cipher is equal to target value
  69.     THEN
  70.     make plaintext = to cipher[array]   
  71.     END IF
  72.     END WHILE
  73.    
  74.     display the step number and substitute the next character in the array the one at position n+1
  75.     cyle through until the end of the cipher array
  76.    
  77.     END
  78.  
  79. */
  80. #include<stdio.h>
  81. #include<string.h> //header file for the standard string library functions
  82. #include<stdlib.h>
  83.  
  84. int main(void)
  85. {
  86.  
  87. int textlength,cipherlength,i, n,step,x;
  88. char cipher[17], plaintext[257], targetValue[257];
  89.  
  90. //enter the length of the cipher key
  91. scanf("%d", &cipherlength);
  92. printf("Length of the cipher key: %d",cipherlength);
  93.  
  94. //Error message 1
  95. if(cipherlength<2 || cipherlength>16){
  96. printf("\nInvalid range, cipherlength must be within 2 and 16");
  97. return 0;
  98. }
  99.  
  100. //Cipher key text
  101. scanf(" %[^\n]s", cipher);
  102. printf("\nCipher Key: %s",cipher);
  103.  
  104.  
  105. //Length of plain text
  106. scanf("%d", &textlength);
  107. printf("\nLength of plaintext: %d",textlength);
  108.  
  109. //Enter plainText to be encrypted
  110. scanf(" %[^\n]s", plaintext);
  111. printf("\nPlain text to encrypt: %s",plaintext);
  112.  
  113. //Error message - display when plaintextlength > strlen plaintext
  114. if(strlen(plaintext) != textlength) {
  115.   printf("\nERROR! The characters in plaintext exceed the set amount");
  116.  
  117. return 0;
  118.  
  119. }
  120.  
  121. strcpy(targetValue,plaintext);
  122. i=strlen(cipher);
  123.  
  124. //Begin encryption
  125.  
  126. for(n=0; n<strnlen(plaintext); n++){
  127. if(targetValue[0] == cipher[n]){
  128. plaintext[n] = cipher[n+1];
  129. }
  130. }
  131.  
  132. printf("\n\nStep 1: Substitute %c with %c", cipher[0], cipher[1]);
  133. printf("\n%s",plaintext);
  134.  
  135.  
  136. for(step=1; step<i-1; step++){
  137. for(n=0; n<strlen(plaintext); n++){
  138. if(cipher[step] == targetValue[n]){
  139.    plaintext[n] = cipher[step+1];
  140. }
  141. }
  142.  
  143. printf("\nStep %d: Substitute %c with %c", step+1, cipher[step],cipher[step+1]);
  144. printf("\n%s",plaintext);
  145. }
  146.  
  147.      
  148. for(n=0; n<strlen(plaintext); n++){   
  149. if(cipher[i-1] == targetValue[n]){
  150.    plaintext[n] = cipher[0];
  151. }
  152. }
  153.        
  154. printf("\nStep %d: Substitute %c with %c",i,cipher[i-1],cipher[0]);
  155. printf("\n%s",plaintext);
  156.  
  157. }