凯撒加密c++
以下是凯撒加密的C++代码示例:
#include <iostream>
#include <string>
using namespace std;
string caesarCipher(string str, int shift) {
string result = "";
for (int i = 0; i < str.length(); i++) {
if (isalpha(str[i])) {
char c = (tolower(str[i]) - 'a' + shift) % 26 + 'a';
result += isupper(str[i]) ? toupper(c) : c;
} else {
result += str[i];
}
}
return result;
}
int main() {
string plaintext;
int shift;
cout << "Enter plaintext: ";
getline(cin, plaintext);
cout << "Enter shift: ";
cin >> shift;
string ciphertext = caesarCipher(plaintext, shift);
cout << "Ciphertext: " << ciphertext << endl;
return 0;
}
该程序接受用户输入的明文和移位数,然后使用凯撒加密算法对明文进行加密,并输出密文。在加密过程中,该程序将非字母字符保留在密文中,而只对字母字符进行加密
原文地址: https://www.cveoy.top/t/topic/fDjS 著作权归作者所有。请勿转载和采集!