Sunday, May 20, 2018

Affine Cipher Encryption - Decryption Program

/* Program to implement Affine Cipher encryption and decryption technique.
*/

#include<iostream>
#include<string.h>
using namespace std;
int inverse(int);
int main()
{
char str[100];
int key1,key2;
cout<<"\nEnter the string for encryption: ";
cin>>str;
cout<<"Enter the first cipher key: ";
cin>>key1;
cout<<"Enter the second cipher key: ";
cin>>key2;
int h=strlen(str);
for(int i=0;i<h;i++)
{
char T=(((toascii(str[i])-97)*key1)%26)+97;
char E=(((toascii(T)-97)+key2)%26)+97;
cout<<str[i]<<"\t"<<"-->"<<"\t"<<E<<"\n";
}

char str1[100];
int key3,key4;
cout<<"\nEnter the string for decryption: ";
cin>>str1;
cout<<"Enter the first decipher key: ";
cin>>key3;
cout<<"Enter the second decipher key: ";
cin>>key4;
int h1=strlen(str1);
int inv=inverse(key3);
for(int i=0;i<h1;i++)
{
int dkey=((toascii(str1[i])-97)-key4)%26;
if(dkey<0)
dkey+=26;
char T1=dkey+97;
int dkey1=(((toascii(T1)-97)*inv))%26;
char P=dkey1+97;
cout<<str1[i]<<"\t"<<"-->"<<"\t"<<P<<"\n";
}
cout<<"\n";
return 0;
}

int inverse(int key3)
{
int r1,r2,r,q,t1,t2,t;
r1=26; r2=key3;
t1=0; t2=1;
while(r2>0)
{
q=r1/r2;
r=r1-q*r2;
r1=r2; r2=r;
t=t1-q*t2;
t1=t2; t2=t;
}
if(r1==1)
{
if(t1<0)
t1+=26;
return t1;

}
}




OUTPUT SCREEN:



No comments:

Post a Comment

Project on Library Management System (C++)

=> The project titled Library Management system is Library management software for monitoring and controlling the transactions in a lib...