#include <iostream>
#include <vector>
using namespace std;
// 維吉尼亞密码
class VigenèreCipher {
public:
string Ciphertext; // 密文
VigenèreCipher(string, string); // 构造函数
private:
string Plaintext, key; // 明文、密钥
vector<vector<char>> VigenèreTable; // 用來加密解密的維吉尼亞表格
};
VigenèreCipher::VigenèreCipher(string Plaintext, string key): Plaintext(Plaintext), key(key) {
// 初始化維吉尼亞表格
vector<char> temp;
for (int i = 0; i < 26; i++) {
VigenèreTable.push_back(temp);
for (int j = 'A'; j <= 'Z'; j++) {
VigenèreTable[i].push_back(j + i <= 'Z' ? j + i : ((j + i) % 'Z' + 'A' - 1));
}
}
// 对照維吉尼亞表格,开始加密
int m, n;
m = Plaintext.size();
n = key.size();
for (int i = 0; i < m; i++) {
Ciphertext.push_back(VigenèreTable[toupper(key[i % n]) - 'A'][toupper(Plaintext[i]) - 'A']);
}
}
int main() {
string Plaintext = "thequickbrownfoxjumpsoverthelazydog";
string key = "LION";
VigenèreCipher cipher(Plaintext, key);
cout
<< "明文:" << Plaintext << endl
<< "密钥:" << key << endl
<< "密文:" << cipher.Ciphertext << endl;
return 0;
}