凯撒密码加密算法c++
时间: 2023-11-17 21:08:09 浏览: 158
kaisamima.rar_kaisamima_kaisamima c++_凯撒加密c++_凯撒密码_凯撒密码c++
以下是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;
}
```
阅读全文