By Rati Gupta
This C++ program is to encrypt as well as decrypt a string. In this program, two techniques of cryptography are used for encryption and decryption: 1. Caesar Cipher 2. RSA algorithm
Caesar Cipher
It shifts the entire alphabet by the number picked by the user. For example, if the number chosen by the user is 2, then A would be replaced by C, B would become D, and so on.
RSA algorithm
The value of n should be large to avoid collisions. Therefore, we have to choose prime numbers accordingly.
It involves three steps:-
1. Key generation
a. Choose two distinct prime numbers, p, and q.
b. n = p*q.
c. Find ϕ(n)
d. ϕ(n)=(p-1)(q-1).
e. Choose an e such that 1 < e < ϕ(n), and such that e and ϕ(n) are coprime
f. Determine d such that
de mod ϕ(n)=1
So to calculate d, use
de = ( 1+ k¢(n))/e
Public keys include n and e.
The private key has n and d, where d is kept a secret.
2. Encryption using public key
c= ( m ^ e)%n
3. Decryption using the private key
m= (c ^ d) % n
Approach
For encryption,
1. Encrypt the message using the key of Caesar Cipher.
2. Assign integral values to alphabets.
3. Generate the keys using the RSA algorithm.
4. Then encrypt using public keys e and n.
For Decryption,
1. Decrypt the message using private keys d and n.
2. Assign alphabets to the decrypted integral values.
3. Then, decrypt using the key of Caesar Cipher.
#include #include #include #include #include using namespace std; long long int power (long long int a, long long int b, long long int mod) { long long int ans=1ll; while(b) //calulating (a^b)%mod { if(b&1)ans=(ans*a)%mod; a=(a*a)%mod; b=b/2; } return ans; } int gcd(int a, int b) { if (b == 0) return a; // finding GCD of a and b return gcd(b, a % b); } vector convert_to_numbers(string s) //Giving integral value to each alphabet { vector v; for(int i = 0; s[i] != '\0'; ++i) { if (s[i] == ' ') v.push_back(27); if (s[i] >= 'a' && s[i] <= 'z') v.push_back(int(s[i])-96); if(s[i] >='A' && s[i] <='Z') v.push_back(int(s[i])-91); //Giving negative integral values to uppercase letters } return v; } string convert_to_alphabets(vector v,int len) //Finding alphabets from decrypted integral values { string sm_alpha,cap_alpha,s(len, ' '); sm_alpha="abcdefghijklmnopqrstuvwxyz"; cap_alpha="ZYXWVUTSRQPONMLKJIHGFEDCBA"; for(int i = 0; i=1 && v[i]<=26) s[i]= sm_alpha[v[i]-1]; if(v[i]>=-26 && v[i]<=-1) s[i]=cap_alpha[abs(v[i])-1]; } return s; } int main() { string body=""; int choice,key,p,q,i=0,siz; long long int d,phi,e,flag=0,n; char ans='y',ch; while(ans=='Y'|| ans=='y') { cout<<"\nEnter your choice \n1. Encryption \n2. Decryption\n "; cin>>choice; if (choice==1) //for encryption { flag=0; cout<<"Enter the message:\n"; cin.ignore(); getline(cin, body); siz=body.size(); cout << "Enter key for encryption: "; cin >> key; for( i = 0; body[i] != '\0'; ++i) { ch = body[i]; if (ch >= 'a' && ch <= 'z') { ch = ch + key; //encrypting lowercase letter if (ch > 'z') { ch = ch - 'z' + 'a' - 1; } body[i] = ch; } else if (ch >= 'A' && ch <= 'Z') { ch = ch + key; //encrypting uppercase letter if (ch > 'Z') { ch = ch - 'Z' + 'A' - 1; } body[i] = ch; } } cout<<"Enter first prime number(>19): "; cin>>p; cout<<"Enter second prime number(>19): "; cin>>q; n=p*q; phi=(p-1)*(q-1); e=2; while (e < phi) // e must be smaller than phi. { if (gcd(e, phi)==1) // e must be co-prime to phi. break; else e++; } int k = 2; d = (1 + (k*phi))/e; vector v; v=convert_to_numbers(body); for(int i = 0; i<siz; i++) { if(v[i]<0) flag=1; long long int encrypted = power(abs(v[i]),e,n); //Calculating (abs(v[i])^e)%n v[i]= encrypted; if(flag==1) v[i]*=-1; flag=0; } cout<<"Encrypted message: "; for(int j=0;j<siz;++j) { cout<<v[j]<<" "; } cout<<endl<<"Size of encrypted message: "<<siz; cout<<endl<<"Private keys of encrypted message: "<<d<<" and "<<n; } if(choice==2) { vector en; int len; string s_string; cout<<"Enter size of encrypted message: "; cin>>len; cout<<"Enter message: "; for(int k=0;k<len;k++) { int num; cin>>num; en.push_back(num); } cout << "Enter key for decryption: "; cin >> key; cout<<"Enter private keys for encrypted message: "; cin>>d>>n; for( i = 0; i<len; ++i) { if(en[i]<0) flag=1; long long int decrypted=power(abs(en[i]),d,n); //Calculating (abs(en[i])^d)%n en[i]=decrypted; if(flag==1) { en[i]*=-1; } flag=0; } s_string = convert_to_alphabets(en,len); for( i = 0; i<len; ++i) { ch = s_string[i]; if(ch >= 'a' && ch <= 'z') { ch = ch - key; if(ch < 'a'){ //decrypt for lowercase letter ch = ch + 'z' - 'a' + 1; } s_string[i] = ch; } else if(ch >= 'A' && ch <= 'Z') { ch = ch - key; //decrypting uppercase letter if(ch < 'A') { ch = ch + 'Z' - 'A' + 1; } s_string[i] = ch; } } cout<<endl<<"Decrypted Message: "<<s_string; } cout<<endl<<"\nDo u want to ask for more?(Y/N) :"; cin>>ans; } return 0; }
Submitted by Rati Gupta (rati123)
Download packets of source code on Coders Packet
Comments