c++凯撒密码编写

以下是使用 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. }

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

  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++是一种通用的高级编程语言,它支持面向对象编程和泛型编程。C++由Bjarne Stroustrup于1983年开发,是C语言的扩展版本。C++具有强大的功能和广泛的应用领域,包括系统开发、游戏开发、嵌入式系统、图形界面等。 凯撒密码是一种简单的替换密码,它通过将字母按照一定的偏移量进行替换来加密消息。例如,偏移量为3时,字母A会被替换为D,字母B会被替换为E,以此类推。解密过程则是将每个字母按照相反的偏移量进行替换。 在C++中实现凯撒密码可以使用字符数组和循环来完成。首先,将明文消息存储在字符数组中,然后通过循环遍历数组中的每个字符,并根据偏移量进行替换。最后,将加密后的消息输出。 以下是一个简单的C++代码示例,实现了凯撒密码的加密和解密功能: ```cpp #include <iostream> using namespace std; void encrypt(string& message, int offset) { for (int i = 0; i < message.length(); i++) { if (isalpha(message[i])) { if (isupper(message[i])) { message[i] = ((message[i] - 'A' + offset) % 26) + 'A'; } else { message[i] = ((message[i] - 'a' + offset) % 26) + 'a'; } } } } void decrypt(string& message, int offset) { encrypt(message, 26 - offset); } int main() { string message = "Hello, World!"; int offset = 3; cout << "Original message: " << message << endl; encrypt(message, offset); cout << "Encrypted message: " << message << endl; decrypt(message, offset); cout << "Decrypted message: " << message << endl; return 0; } ``` 这段代码中,encrypt函数用于加密消息,decrypt函数用于解密消息。在main函数中,我们定义了一个明文消息和偏移量,然后调用encrypt函数进行加密,再调用decrypt函数进行解密。最后,输出加密和解密后的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crmeb专业二开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值