包含加密和解密的两个过程
#include<iostream>
using namespace std;
int main()
{
int key,i;
string plaintext, ciphertext;
char cmd;
cout << "Choose your command:" << endl;
cout << "a) encryption, b) decode" << endl;
cin >> cmd;
if (cmd == 'a' || cmd == 'b')
{
cout << "Input your key:" << endl;
cin >> key;
if (cmd == 'a')
{
cout << "Input your plaintest:(lowercase, without space)" << endl;
cin >> plaintext;
ciphertext = plaintext;
for (i = 0; i < plaintext.size(); i++)
ciphertext[i] = ((plaintext[i] - 'a' + key) % 26) + 'a' - 32;
cout << "Here is the ciphertext:" << endl;
cout << ciphertext;
}
else
{
cout << "Input your ciphertext:(capital letter, without space)" << endl;
cin >> ciphertext;
plaintext = ciphertext;
for (i = 0; i < ciphertext.size(); i++)
{
if (ciphertext[i] - 'A' - key < 0)
ciphertext[i] += 26;
plaintext[i] = ((ciphertext[i] - 'A' - key) % 26) + 'A' + 32;
}
cout << "Here is the plaintext:" << endl;
cout << plaintext;
}
}
else
cout << "Wrong command, program exit." << endl;
}