c++凯撒密码编写-LMLPHP

以下是使用 C++ 实现凯撒密码的示例代码:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string encrypt(string plaintext, int shift) {
  5.     string ciphertext = "";
  6.     for (int i = 0; i < plaintext.length(); i++) {
  7.         char c = plaintext[i];
  8.         if (isalpha(c)) {
  9.             c = toupper(c);
  10.             c = ((c - 'A' + shift) % 26) + 'A';
  11.         }
  12.         ciphertext += c;
  13.     }
  14.     return ciphertext;
  15. }
  16. string decrypt(string ciphertext, int shift) {
  17.     string plaintext = "";
  18.     for (int i = 0; i < ciphertext.length(); i++) {
  19.         char c = ciphertext[i];
  20.         if (isalpha(c)) {
  21.             c = toupper(c);
  22.             c = ((c - 'A' - shift + 26) % 26) + 'A';
  23.         }
  24.         plaintext += c;
  25.     }
  26.     return plaintext;
  27. }
  28. int main() {
  29.     string plaintext, ciphertext, decrypted_text;
  30.     int shift;
  31.     cout << "Enter plaintext: ";
  32.     getline(cin, plaintext);
  33.     cout << "Enter shift: ";
  34.     cin >> shift;
  35.     ciphertext = encrypt(plaintext, shift);
  36.     cout << "Encrypted text: " << ciphertext << endl;
  37.     decrypted_text = decrypt(ciphertext, shift);
  38.     cout << "Decrypted text: " << decrypted_text << endl;
  39.     return 0;
  40. }

在这个示例代码中,我们首先定义了两个函数 encrypt 和 decrypt,用于加密和解密凯撒密码。在 encrypt 函数中,我们遍历明文字符串中的每个字符,如果是字母则将其转换为大写字母,然后将其 ASCII 码值加上移位值,再对 26 取模得到加密后的字符。在 decrypt 函数中,我们同样遍历密文字符串中的每个字符,如果是字母则将其转换为大写字母,然后将其 ASCII 码值减去移位值,再对 26 取模得到解密后的字符。最后,我们在 main 函数中获取用户输入的明文和移位值,调用 encrypt 函数加密明文得到密文,再调用 decrypt 函数解密密文得到解密后的明文。

好的,以下是继续编写的C++凯撒密码代码:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string encrypt(string plaintext, int shift) {
  5.     string ciphertext = "";
  6.     for (int i = 0; i < plaintext.length(); i++) {
  7.         char c = plaintext[i];
  8.         if (isalpha(c)) {
  9.             c = toupper(c);
  10.             c = ((c - 'A' + shift) % 26) + 'A';
  11.         }
  12.         ciphertext += c;
  13.     }
  14.     return ciphertext;
  15. }
  16. string decrypt(string ciphertext, int shift) {
  17.     string plaintext = "";
  18.     for (int i = 0; i < ciphertext.length(); i++) {
  19.         char c = ciphertext[i];
  20.         if (isalpha(c)) {
  21.             c = toupper(c);
  22.             c = ((c - 'A' - shift + 26) % 26) + 'A';
  23.         }
  24.         plaintext += c;
  25.     }
  26.     return plaintext;
  27. }
  28. int main() {
  29.     string plaintext, ciphertext, decrypted_text;
  30.     int shift;
  31.     cout << "Enter plaintext: ";
  32.     getline(cin, plaintext);
  33.     cout << "Enter shift: ";
  34.     cin >> shift;
  35.     ciphertext = encrypt(plaintext, shift);
  36.     cout << "Encrypted text: " << ciphertext << endl;
  37.     decrypted_text = decrypt(ciphertext, shift);
  38.     cout << "Decrypted text: " << decrypted_text << endl;
  39.     return 0;
  40. }

这段代码的功能与之前的代码相同,只是在输出结果时将解密后的文本也输出了。用户可以通过输入明文和移位值来加密和解密凯撒密码。

12-08 17:01