以下是使用 C++ 实现凯撒密码的示例代码:
- #include <iostream>
- #include <string>
- using namespace std;
- string encrypt(string plaintext, int shift) {
- string ciphertext = "";
- for (int i = 0; i < plaintext.length(); i++) {
- char c = plaintext[i];
- if (isalpha(c)) {
- c = toupper(c);
- c = ((c - 'A' + shift) % 26) + 'A';
- }
- ciphertext += c;
- }
- return ciphertext;
- }
- string decrypt(strin