凯撒密码加密算法c++
时间: 2023-11-17 09:08:09 浏览: 162
以下是C++实现凯撒密码加密算法的代码:
```c++
#include<iostream>
#include<string>
using namespace std;
string encrypt(string plaintext, int key) {
string ciphertext = "";
for (int i = 0; i < plaintext.length(); i++) {
if (isalpha(plaintext[i])) {
if (isupper(plaintext[i])) {
ciphertext += char(int(plaintext[i] + key - 65) % 26 + 65);
}
else {
ciphertext += char(int(plaintext[i] + key - 97) % 26 + 97);
}
}
else {
ciphertext += plaintext[i]; }
}
return ciphertext;
}
int main() {
string plaintext = "hello world";
int key = 3;
string ciphertext = encrypt(plaintext, key);
cout << "Plaintext: " << plaintext << endl;
cout << "Ciphertext: " << ciphertext << endl;
return 0;
}
```
阅读全文