<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. start
  3. cipher key shall be lowercase characters
  4. plain text to encrypt (4-256) in characters
  5. cipher key shall be between (2-16)
  6. must create guidance to print the output
  7.  
  8. if
  9. substitutional needed,plain text replace value with substitutiional values
  10.  
  11. else
  12. change to another character
  13.  
  14. else if
  15. change to familiar character
  16.  
  17. print the output
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. int main(int argc, char **argv) {
  25.  
  26.        
  27.      
  28. int cipherlen = 0; //cipher key length
  29. char cipherkey[16]; //cipher key
  30. int textlen = 256; //text length
  31. char text[256];    //text to be ciphered
  32. int cipUn[26];
  33. int keeptrack[256]; //array to keep track if certain character in text is already modified as result of substitution.
  34. //so that it should not be changed again if match to another cipher character.
  35. int ci;// cipher key loop index
  36. int ti; // text loop index
  37.  
  38. printf("enter length of cipher key:/n");
  39. scanf("%d", &cipherlen, &cipherkey, &textlen);
  40. printf("enter length of cipher key:/n");
  41. scanf("%d", &cipherlen, &cipherkey, &textlen);
  42. printf("enter length of text:/n");
  43. scanf ("%d", &cipherlen, &cipherkey, &textlen);
  44. getchar();
  45. printf("enter the text:/n");
  46. fgets(text,256,stdin);
  47.  
  48. text[strlen(text) - 1] = 0;
  49. //printf("len: cipher %d", (int)strlen(cipherkey), (int)strlen(text));
  50.  
  51. //if cipher length do not match or out of permissible limits
  52. if (cipherlen < 2|| cipherlen > 16 || cipherlen !=strlen(cipherkey))
  53.  
  54. {
  55.    printf("cipher key length criteria failed. exiting...");
  56.    return("exit_failure");
  57. }
  58.  
  59. //initialise the array to hold the count of characters in cipher
  60. //it will be used to determine if all the characters are unique.
  61.  
  62.  for (ci = 0; ci <26; ci++)
  63.      cipUn[ci] = 0;
  64. // check if the cipher characters are as allowed and unique     
  65. for (ci = 0; ci < cipherlen; ci++)
  66.  {
  67.      
  68.    char c = cipherkey[ci];
  69.    int unI = c - 'a';// if not allowed
  70.    
  71.    {
  72.      printf("cipher contains duplicate characters. Exiting...");
  73.      return("exit_failure");
  74.    }
  75.    
  76.   //add the char to the array of uniqueness.
  77.   cipUn[unI] =1;
  78.  }
  79.  
  80.  // check if the text length holds to the limits specified.
  81.  if(textlen < 4|| textlen > 256 || textlen !=strlen(text))
  82.  {
  83.     printf("text length criteria failed. Exiting...");
  84.     return("exit_failure");
  85.  }
  86.  //and does not contain any not allowed character.
  87.  for (ti = 0; ti < textlen; ti++)
  88.  {
  89.      if((text[ti] < 'a' || text[ti] > 'z') && text[ti] !='.')
  90.    {
  91.        printf("invalid characters in the text. Exiting...");
  92.        return (EXIT_FAILURE);
  93.      }
  94.      
  95.      //initialise the track value to 0, no character has been substituted yet.
  96.      keeptrack [ti] = 0;
  97.      
  98.      }
  99.      //the substitution logic is here.
  100.      for (ci=0; ci < cipherlen; ci++)
  101.      
  102.      {
  103.       // for every character in the cipher
  104.       char subC = cipherkey [ci+1]; // the substitude char'
  105.       char tgtc = cipherkey[ci];    // the target char
  106.       if (ci== cipherlen -1) subC = cipherkey[16]; // if this is the last index, substitude char will be the first char.
  107.      
  108.        printf("\nstep %d: subsitude %c with %c", ci+1, tgtc, subC);
  109.        
  110.      // for every character in the text
  111.      for (ti= 0; ti < textlen; ti++)
  112.      {
  113.        
  114.           //if not already substituted and is same as the target char, substitude it. set tracker.
  115.        
  116.             if (keeptrack[ti]== 0 && text [ti]== tgtc)
  117.        {
  118.            text[ti] = subC;
  119.            keeptrack[ti]=1;
  120.            
  121.           }
  122.        }
  123.        printf("\n%s", text);
  124.      }
  125.      
  126.      return ("exit sucess");
  127.      
  128. }