シンプルなROT13暗号化/復号化。復号化関数を作成する必要はありません。13文字を元の状態に戻すだけなので、ROT13と呼ばれます。
#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