encryption - Cipher text in C,How to repeat key characters -


  • explanation ciphertext generated plaintext “adding” corresponding characters of plaintext , key together. if plaintext shorter key, of key used. similarly, if plaintext shorter key, key used multiple times.

    for example, encode plaintext “hello” key “cat”:

plaintext: hello

key: catca

ciphertext: kffop

and encode plaintext “dog” key “fido”:

plaintext: dog

key: fid

ciphertext: jxk

to add 2 letters together, use following convention: a=1, b=2, …, z=26. if sum of 2 letters greater 26, subtract 26 sum. example: + e = 1 + 5 = 6 = f, , d + x = 4 + 24 = 28 = 2 = b.

  • now problem code unable repeat key characters further coding of plain text if key characters less,how repeat key characters,so further coding can possible?

help me guys.

here code:

#include<stdio.h>     #include<string.h>     int main()         {             char str[100],k[50],str1[100];             int i,n;             gets(str);// input plain text.              gets(str1);//input key.              for(i=0;str[i]!='\0';i++)                     {                          n=(str[i]-65+1)+(str1[i]-65+1);//extracting numerical position , adding them.                          if(n>26) //if numerical value exceeds 26 subtracting 26 , getting numerical value.                           {                              n=n-26;                           }                          str[i]=n+64;//storing ciphered character.                     }                for(i=0;str[i]!='\0';i++)//printing ciphered characters.               printf("%c",str[i]);               return 0;          } 

you can use loop variable make index of key 0 every time reaches length. have used variable j in case. try code:

#include<stdio.h> #include<string.h> int main()     {         char str[100],k[50],str1[100];         int i,n;         gets(str);// input plain text.          gets(str1);//input key.         int lenk=strlen(str1),j;   //calculate length of key          for(i=0,j=0;str[i]!='\0';i++,j++)     {          if(j==lenk) j=j-lenk;      //make j=0          n=(str[i]-65+1)+(str1[j]-65+1);    // add str1[j] instead          if(n>26)            {              n=n-26;           }          str[i]=n+64;//storing ciphered character.      }            for(i=0;str[i]!='\0';i++)           printf("%c",str[i]);           return 0;      } 

note works capital letter, have change code small letters


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -