<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. This program is used to implement Substitution Cipher
  3. */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include<ctype.h>
  9.  
  10. void validate_cipher_key(char*);
  11. void validate_plain_text(char*);
  12.  
  13. int main()
  14. {
  15.   FILE* fp;
  16.   char lenc[10],cipherkey[10],lenp[10],oplaintext[200],plaintext[200];
  17.   int ilenc,ilenp,i,j,k;
  18.  
  19.   //File open and get file handler
  20.   fp = fopen("file.txt", "r");
  21.  
  22.   //Read first four lines in file, which in the order is
  23.   //cipher length, cipher key, plain text length, plain text
  24.   gets(lenc);
  25.   gets(cipherkey);
  26.   validate_cipher_key(cipherkey);
  27.   gets(lenp);
  28.   gets(oplaintext);
  29.   validate_plain_text(oplaintext);
  30.  
  31.   //Convert from string to integer (cipher and plain text length)
  32.   sscanf(lenc,"%d",&ilenc);
  33.   sscanf(lenp,"%d",&ilenp);
  34.   i=0;
  35.   j=0;
  36.   printf("Original plaintext: %s\n",oplaintext);
  37.   strcpy(plaintext,oplaintext); //Have a backup string
  38.  
  39.   //Repeat until length of cipher key
  40.   while(i<ilenc)
  41.   {
  42.      printf("Step %d substitue %c with %c\n",(i+1),cipherkey[j],cipherkey[(j+1)%ilenc]);
  43.      //Repeat until length of plain text
  44.      for(k=0;k<ilenp;k++)
  45.      {
  46.        //Check for substitution required?
  47.        if(oplaintext[k]==cipherkey[j])
  48.           plaintext[k]=cipherkey[(j+1)%ilenc];
  49.      
  50.      }
  51.      printf("%s\n",plaintext);
  52.     i++;
  53.      j++;
  54.   }
  55.  
  56.   //printf("Cipherlen=%d,Cipherkey=%s,lenp=%d, plaintext=%s",ilenc,cipherkey,ilenp,plaintext);
  57.  
  58.   //Close the file
  59. fclose(fp);
  60.  
  61.  
  62. }
  63. /*
  64. Function to validate cipher key
  65. Length should be 2 to 16
  66. No characters other than lower case alphabets
  67. No duplicate characters
  68. */
  69.  
  70. void validate_cipher_key(char *ck)
  71. {
  72.    int i,j;
  73.   //printf("%s\n",ck);
  74.   if(strlen(ck)<2 || strlen(ck)>16 )
  75.   {
  76.       printf("Wrong cipher key length");
  77.       exit(0);
  78.   }
  79.   for(i=0;i<(strlen(ck)-1);i++)
  80.    {
  81.      //printf("%c\n",ck[i]);
  82.      
  83.       if(islower(ck[i])==0)
  84.       {
  85.         printf("Cipher character not allowed");
  86.         exit(0);
  87.       }
  88.      
  89.     } 
  90.    for(i=0;i<strlen(ck);i++)
  91.    {
  92.      for(j=i+1;j<strlen(ck);j++)
  93.      {
  94.         if(ck[i]==ck[j])
  95.         {
  96.            printf("Duplicate characters in cipher text");
  97.            exit(0);
  98.         }
  99.      }
  100.    }
  101.  
  102. }
  103.  
  104. /*
  105. Function to validate plain text
  106. Length should be 4 to 256
  107. No characters other than lower case alphabets and spaces
  108.  
  109. */
  110.  
  111. void validate_plain_text(char *ck)
  112. {
  113.    int i,j;
  114.   //printf("%s\n",ck);
  115.   if(strlen(ck)<4 || strlen(ck)>256 )
  116.   {
  117.       printf("Wrong plain key length");
  118.       exit(0);
  119.   }
  120.   for(i=0;i<(strlen(ck)-1);i++)
  121.    {
  122.      //printf("%c\n",ck[i]);
  123.      
  124.       if((islower(ck[i])==0) && (ck[i]!=' '))
  125.       {
  126.         printf("Plain text character not allowed");
  127.         exit(0);
  128.       }
  129.      
  130.     } 
  131.    
  132.  
  133. }