一个简单的 ROT13 加密器/解密器。无需编写解密函数。它之所以称为 ROT13,是因为它只是将 13 个字符旋转回其原始状态。
#include <iostream>
using namespace std;
//encrypt or decrypt string
void ROT13_Encrypt_Decrypt_String(char str[]){
for( int i=0; str[i] != '\0'; i++ ){
if(str[i] >= 'a' && str[i] <= 'm'){
str[i] += 13;
}
else if(str[i] > 'm' && str[i] <= 'z'){
str[i] -= 13;
}
else if (str[i] >= 'A' && str[i] <= 'M'){
str[i] += 13;
}
else if(str[i] > 'M' && str[i] <= 'Z'){
str[i] -= 13;
}
}
}
int main()
{
char mystring [] = "Hello World!";
cout << "Original string: " << mystring << endl;
//encrypt
ROT13_Encrypt_Decrypt_String(mystring);
cout << "Encrypted string: " << mystring << endl;
//decrypt
ROT13_Encrypt_Decrypt_String(mystring);
cout << "Decrypted string: " << mystring << endl;
return 0;
}
输出:
Original string: Hello World!
Encrypted string: Uryyb Jbeyq!
Decrypted string: Hello World!
Press any key to continue