用C++实现Caesar Cipher的解密算法
Caesar Cipher是一种基础的加密技术,它将明文中的字母逐个替换为字母表中其后N位的字母,其中N被称为密钥。使用Caesar Cipher进行加密是极为简单的,但是要想解密却需要一定的技巧。
对于给定的一个密文串和密钥,我们需要使用C++编写一个程序来解密该密文并将其还原为原始文本。
下面是使用C++实现Caesar Cipher的解密算法的源代码:
#include <iostream>
#include <string>
using namespace std;
string decrypt(string ciphertext, int key) {
string plaintext = "";
for (int i = 0; i < ciphertext.length(); i++) {
char c = ciphertext[i];
if (isalpha(c)) {
c = tolower(c);
c = ((c - 'a' - key + 26) % 26) + 'a';
}
plaintext += c;
}
return plaintext;
}
int main() {
strin