|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
维吉尼亚密码(又译维热纳尔 Vigenère Cipher 密码)是使用一系列凯撒密码组成密码字母表的加密算法,属于多表密码的一种简单形式。
因为无聊,突然想写简单代码,所以就花了 15 分钟写了,代码见笑了 。因为其加密过程简单,这里就不多做叙述了。維吉尼亞表格如下:
C++ 代码:- #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;
- }
复制代码 |
评分
-
查看全部评分
|