#include <iostream> using namespace std; int main() { int Choice, option; cout << "1.Caesar Cipher \n 2.Vernam Cipher \n 3.Vigenere Cipher \n 4.Transposition Cipher \n 5.RSA \n"; cout << "Choose one of the options above: "; cin >> Choice; switch(Choice) { case 1: cout <<"1.Encrypt a message \n"; cout <<"2.Decrypt a message \n"; cout <<"Choose one option from above: "; cin >>option; if(option == 1) { string message, cipher; char chr; int shift, x; cout <<"Enter your message: "; cin >> message; cout <<"Enter the shift value: "; cin >> shift; for(int i=0; i<message.length(); i++) { chr = message[i]; if (int(chr) <= 90){ x = ((int(chr) + shift - 65) % 26 + 65); cipher[i] = char(x); } else { x = ((int(chr) + shift - 97) % 26 + 97); cipher[i] = char(x); } } cout<<"Your cipher text is: "; for(int i=0; i<message.length(); i++) { cout<<cipher[i]; } } else if(option == 2) { string message, cipher; char chr; int shift, x; cout<<"Enter your cipher text: "; cin>>cipher; cout<<"Enter the shift value: "; cin>>shift; for(int i=0; i<cipher.length(); i++) { chr = cipher[i]; if(int(chr) < 90) { x = ((int(chr) - shift - 65) % 26 + 65); message[i] = char(x); } else { x = ((int(chr) - shift - 97) % 26 + 97); message[i] = char(x); } } cout<<"Your decrypted message is: "; for(int i=0; i<cipher.length(); i++) { cout<<message[i]; } } break; case 2: cout <<"1.Encrypt a message \n"; cout <<"2.Decrypt a message \n"; cout <<"Choose one option from above: "; cin >>option; if(option == 1) { string message, cipher, key; char chr, keychar; int x; cout <<"Enter your message: "; cin >> message; cout <<"Enter the key: "; cin >>key; for(int i=0; i<message.length(); i++){ char chr = message[i]; char keychar = key[i]; if (int(chr) < 90){ x = ((int(chr) + int(keychar) - 130) % 26 + 65); cipher[i] = char(x); } else{ x = ((int(chr) + int(keychar) - 194) % 26 + 97); cipher[i] = char(x); } } cout<<"Your cipher text is: "; for(int i=0; i<message.length(); i++) { cout<<cipher[i]; } } if(option == 2) { string cipher, message, key; char chr, keychar; int x; cout <<"Enter the cipher text: "; cin >> cipher; cout <<"Enter the key: "; cin >>key; for(int i=0; i<cipher.length(); i++){ chr = cipher[i]; keychar = key[i]; if (int(chr) < 90){ x = ((int(chr) - int(keychar) - 130) % 26 + 65); message[i] = char(x); } else{ x = ((int(chr) - int(keychar)) % 26 + 97); message[i] = char(x); } } cout<<"Your decrypted message is: "; for(int i=0; i<cipher.length(); i++) { cout<<message[i]; } } break; case 3: cout <<"1.Encrypt a message \n"; cout <<"2.Decrypt a message \n"; cout <<"Choose one option from above: "; cin >>option; if(option == 1) { string message, cipher, key; char chr, keychar; int x; cout <<"Enter your message: "; cin >> message; cout <<"Enter the key: "; cin >>key; char newkey[message.length()]; int length = 0; while(length < message.length()) { for(int i=0; i<key.length(); i++) { if(length <= message.length()) { newkey[length] = key[i]; length += 1; } else break; } } for(int i=0; i<message.length(); i++){ chr = message[i]; keychar = newkey[i]; if (int(chr) < 90){ x = ((int(chr) + int(keychar) - 130) % 26 + 65); cipher[i] = char(x); } else{ x = ((int(chr) + int(keychar) - 194) % 26 + 97); cipher[i] = char(x); } } cout<<"Your cipher text is: "; for(int i=0; i<message.length(); i++) { cout<<cipher[i]; } } if(option == 2) { string cipher, message, key; char chr, keychar; int x; cout <<"Enter the cipher text: "; cin >> cipher; cout <<"Enter the key: "; cin >>key; char newkey[cipher.length()]; int length = 0; while(length < cipher.length()) { for(int i=0; i<key.length(); i++) { if(length <= cipher.length()) { newkey[length] = key[i]; length += 1; } else break; } } for(int i=0; i<cipher.length(); i++){ chr = cipher[i]; keychar = newkey[i]; if (int(chr) < 90){ x = ((int(chr) - int(keychar) - 130) % 26); if(x<0){ x += 91; } else{ x += 65; } message[i] = char(x); } else{ x = ((int(chr) - int(keychar)) %26); if(x<0){ x += 123; } else{ x += 97; } message[i] = char(x); } } cout<<"Your decrypted message is: "; for(int i=0; i<cipher.length(); i++) { cout<<message[i]; } } break; } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter