#include <iostream>
#include <string>
// 凯撒密码实现
std::string caesarEncrypt(std::string text, int shift) {
std::string result = "";
for (char& c : text) {
if (isalpha(c)) {
char encrypted = (c + shift - 'a') % 26 + 'a';
result += encrypted;
} else {
result += c;
}
}
return result;
}
std::string caesarDecrypt(std::string text, int shift) {
std::string result = "";
for (char& c : text) {
if (isalpha(c)) {
char decrypted = (c - shift - 'a' + 26) % 26 + 'a';
result += decrypted;
} else {
result += c;
}
}
return result;
}
int main() {
std::string plaintext = "hello world";
int shift = 3;
std::string ciphertext = caesarEncrypt(plaintext, shift);
std::cout << "加密后的文本:" << ciphertext << std::endl;
std::string decryptedText = caesarDecrypt(ciphertext, shift);
std::cout << "解密后的文本:" << decryptedText << std::endl;
return 0;
}
凯撒密码是一种简单的替换密码,将字母按照一定的位移进行替换。在示例中,明文为"hello world",位移为3,加密后的文本为"khoor zruog",解密后的文本为"hello world"。
1010

被折叠的 条评论
为什么被折叠?



