<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3. Define the length of cipher key
  4. Define the length of plain text
  5. Define the position of letters in cipher key
  6. Define the position of letters in plain text before substitution
  7. Define the position of letters in plain text after substitution
  8. Counter to indicate current position of cipher key
  9. Counter a, x, y
  10. Insert input line 1 for length of cipher key
  11. IF length of cipher key is out of range from 2 to 16
  12.    THEN print error
  13. END IF
  14. Insert input line 2 for the cipher key text
  15. WHILE the character of ciper key is checked one by one   
  16.    IF ciper key is number or in upper case character
  17.       THEN print error
  18.    END IF
  19. END WHILE
  20. Insert input line 3 for length of plain text
  21. IF length of plain text is out of range from 4 to 256
  22.    THEN print error
  23. END IF
  24. Insert input line 4 for plain text to be encrypted
  25. WHILE the character of plain text is checked one by one
  26.    IF plain text is number or in upper case character
  27.       THEN print error
  28.    END IF
  29. END WHILE
  30. Copy character from plain text before substitution to plain text after substitution
  31. WHILE the position of cipher key is changing
  32.    WHILE the position of plain text is changing
  33.       IF the character of current position of cipher key is same as current position of plain text before substitution
  34.          THEN the character of plain text is replaced with character of the next position of cipher key
  35.       END IF
  36.    END WHILE
  37. END WHILE
  38. Print the output of steps which determine the alphabet of character in plain text to be substituted
  39. Print the plain text after substitution
  40. END
  41. */
  42.  
  43.  
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47.  
  48. int cipherkeylength;
  49. int plaintextlength;
  50. char cipherkey[16];
  51. char plaintext1[256];
  52. char plaintext2[256];
  53. int N;
  54. int a, x, y;
  55.  
  56. int main(void)
  57. {
  58.  
  59. // Insert 4 inputs
  60.  
  61.    scanf("%d", &cipherkeylength)
  62.    if ( (cipherkeylength < 2) || (cipherkeylength > 16) )
  63.    {
  64.       printf("\nError, cipher key must be 2 to 16 characters in length\n");
  65.       return 0;
  66.    }
  67.    
  68.    scanf("%s", &cipherkey)
  69.    for (a = 0; a < cipherkeylength; a++)
  70.    {
  71.       if( isdigit(cipherkey[a]) || isupper(cipherkey[a]) )
  72.       {
  73.          printf("\nError, cipher key must be only lower case characters of the alphabet\n");
  74.          return 0;
  75.       }
  76.    }
  77.    
  78.    scanf("%d", &plaintextlength);
  79.    if ( (plaintextlength < 4) || (plaintextlength > 256) )
  80.    {
  81.       printf("\nError, plain text must be 4 to 256 in length\n");
  82.       return 0;
  83.    }
  84.    
  85.    scanf("\n%[^\n]", &plaintext1);
  86.    for (a = 0; a < cipherkeylength; a++)
  87.    {
  88.       if ( isdigit(plaintext1[a]) || isupper(plaintext1[a]) )
  89.       {
  90.          printf("\nError, plain text must be only lower case characters of the alphabet\n");
  91.          return 0;
  92.       }
  93.    }
  94.    
  95.    strncpy(plaintext2, plaintext1, 256);
  96.    
  97.    for (N = 0; N < cipherkeylength; N++)
  98.    {
  99.       for (x = 0; x < plaintextlength; x++)
  100.       {
  101.          if (plaintext1[x] == cipherkey[N])
  102.          {
  103.             plaintext2[x] = cipherkey[N+1];
  104.          }
  105.       }
  106.      
  107.       y = N + 1;
  108.       cipherkey[cipherkeylength] = cipherkey[0];
  109.      
  110.  // Output
  111.  
  112.       printf("\nStep %d substitude %c with %c", y, cipherkey[N], cipherkey[N+1]);
  113.       printf("\n%s",plaintext2);
  114.      
  115.    }
  116.      
  117. }