维吉尼亚密码(对称加密算法)
2026-03-07 20:45:11
发布于:江苏
详见维吉尼亚密码
#include <iostream>
#include <string>
// plaintext - 明文
std::string plaintext = "Minecraft is a classic 3D sandbox game developed by Mojang under Microsoft, officially released in 2011. Players can freely explore, build, and adventure in a randomly generated block world, choosing between survival or creation modes, collecting resources, building buildings, and resisting monsters. There are no fixed tasks, and the degree of freedom is extremely high. It is a phenomenon level game with extremely high global sales.";
// ciphertext - 密文(彩蛋)
std::string ciphertext = "Erhfva bg h wbuf-ytosj, xghcycgxhck ekceynbfwln atbebnvx roni wq jxwsjf ehdssng tmy xmg jbcvwql pgr jyttf zlcmov ncw nvjtktss uxorbetl. Pg linwbgmg thamwnsr ifmnepfagut iophqxzaq, xgqjbqxgu voyxqr-bgbslars, dbarmwmuna, yuq ifmjrsnfys ekceynbfwln, foipav wr fjbhyiyt tmy qxuguatkg ab zsr fitfrlq tbb jxwsjf jlsb vc dpvstlggvape blitecntrcm dprawg. Cnmvmu wtg h gbqf fitbbhes zgiepkm has hfpes-dyygn amkhaxg, pdosppav assgxizc qxksaavdgg zhra yz lxp krkxzmwztgh, qpmo hapemqpf, opavubqghy bbrlyabucupt, ulo vfydyxgu, ncw ybgdforlq hdcynibclz. Bhq pghgq cathdveb tchgjks lapuzcz rhrc gd fsu hxoksrhlzw bc gwzgtfg zhra yz Lbbbvjh, khpDL, Svcnl, riv. Hpibjc pdfasuvir yuq tpsuqpgh srpkbgut ksqvhgvsq zpds pg hbc bu hfl bhgr cdiijhe ifmnepfagut eolnhpzsq pjkfcugar ycnxeozsr.";
// key - 密钥(不区分大小写,只能为字母)
std::string key = "Minecraft";
int toIndex(char c) {
if ('a' <= c && c <= 'z') {
return c - 'a';
}
if ('A' <= c && c <= 'Z') {
return c - 'A';
}
return -1;
}
// encipher - 加密(只能加密英文)
std::string encipher(std::string plaintext, std::string key) {
std::string ciphertext = "";
for (int i = 0; i < plaintext.size(); i++) {
if ('a' <= plaintext[i] && plaintext[i] <= 'z') {
ciphertext += 'a' + (toIndex(plaintext[i]) + toIndex(key[i % key.size()]) + 26) % 26;
} else if ('A' <= plaintext[i] && plaintext[i] <= 'Z') {
ciphertext += 'A' + (toIndex(plaintext[i]) + toIndex(key[i % key.size()]) + 26) % 26;
} else {
ciphertext += plaintext[i];
}
}
return ciphertext;
}
// decrypt - 解密
std::string decrypt(std::string ciphertext, std::string key) {
std::string plaintext = "";
for (int i = 0; i < ciphertext.size(); i++) {
if ('a' <= ciphertext[i] && ciphertext[i] <= 'z') {
plaintext += 'a' + (toIndex(ciphertext[i]) - toIndex(key[i % key.size()]) + 26) % 26;
} else if ('A' <= ciphertext[i] && ciphertext[i] <= 'Z') {
plaintext += 'A' + (toIndex(ciphertext[i]) - toIndex(key[i % key.size()]) + 26) % 26;
} else {
plaintext += ciphertext[i];
}
}
return plaintext;
}
int main() {
std::cout << encipher(plaintext, key) << std::endl;
std::cout << decrypt(encipher(plaintext, key), key) << std::endl;
std::cout << decrypt(ciphertext, "WrongKey") << std::endl;
return 0;
}
(仅供娱乐,安全性极低)
这里空空如也













有帮助,赞一个