凯撒密码的C++实现方法及代码示例
凯撒密码是一种简单的加密算法,通过对明文进行移位操作来生成密文。在C++编程中实现凯撒密码可以通过使用字符数组和循环语句来实现。下面是一个简单的凯撒密码C++代码示例:
#include <iostream>
using namespace std;
void caesarCipher(string& text, int shift) {
    for (int i = 0; i < text.length(); i++) {
        if (isalpha(text[i])) {
          if (islower(text[i])) {
            text[i] = char((int(text[i] - 'a' + shift) % 26) + 'a');
          } else {
            text[i] = char((int(text[i] - 'A' + shift) % 26) + 'A');
          }
        }
    }
}
int main() {
    string plaintext = "Hello, World!";
    int shift = 3;
    cout << "明文:" << plaintext << endl;
    caesarCipher(plaintext, shift);
    cout << "密文:" << plaintext << endl;
    return 0;
}
iostream>
                            下载地址
                        
                    
                            用户评论
                        
                    