我已经很久没有接触 C++ 了,而且我的语言从来都不是很流利,所以请原谅我的无知。
我编写了以下小程序来处理 XOR 加密:
#include <iostream>
#include <iomanip>
#include "string.h"
using std::cout;
using std::endl;
using std::hex;
using std::string;
string cipher(string msg, char key);
int main(void)
{
string msg = "Now is the winter of our discontent, made glorious summer by this sun of York.";
char key = 's'; // ASCII 115
string ctext = cipher(msg, key);
cout << "Plaintext: " << msg << endl;
cout << "Ciphertext (hex): ";
for (int i = 0; i < ctext.size(); i++)
cout << hex << ctext[i];
return 0;
}
string cipher(string msg, char key)
/*
Symmetric XOR cipher
*/
{
for(int i = 0; i < msg.size(); i++)
msg[i] ^= key;
return msg;
}
此代码输出以下内容:
Plaintext: Now is the winter of our discontent, made glorious summer by this sun of York.
Ciphertext (hex): =SSSSSS_SSSS
SSSS*]
为什么我不能输出十六进制值?我究竟做错了什么?
此外,任何一般性建议都值得赞赏。