【发布时间】:2017-06-10 08:52:57
【问题描述】:
我正在尝试使用 rsa 加密缓冲区,然后将数据以十六进制格式保存到文件中。我正在使用 Crypto++ 5.6.5。
加载键(工作):
try
{
// Read RSA public
FileSource fs1("public.pem", true);
PEM_Load(fs1, pubKey);
// Read RSA encrypted private
FileSource fs2("private.pem", true);
PEM_Load(fs2, privKey, "1234", 4);
}
catch(const Exception& ex)
{
cout << "ERROR: RSA:" << ex.what() << endl;
SystemLog_Print("RSA: Couldn't load keys");
}
加密(好吗?):
std::string RSA_Encrypt(unsigned char *buf, uint8_t len)
{
AutoSeededRandomPool rng;
std::string plain;
std::string cipher, recovered;
for(int i = 0; i < len; ++i) {
plain.push_back(buf[i]);
}
// Encryption
RSAES_OAEP_SHA_Encryptor e(pubKey);
StringSource ss1(plain, true, new PK_EncryptorFilter(rng, e, new StringSink(cipher)));
// Test Decryption
RSAES_OAEP_SHA_Decryptor d(privKey);
StringSource ss2(cipher, true, new PK_DecryptorFilter(rng, d, new StringSink(recovered)));
if(memcmp(plain.data(), recovered.data(), plain.size()) != 0) {
cout << "RSA Mismatch" << endl;
}
return cipher;
}
现在我坚持将加密数据写入可读 HEX 格式的文件,例如:
AB123CDE456
使用像 std::hex 这样的流操作符似乎不起作用。 您能给我一些建议吗?
不工作:
unsigned char *buf[] = "123456789";
file << std::hex << RSA_Encrypt(buf, 9);
只打印一些不可读的二进制数据;
【问题讨论】:
-
你的意思是它似乎不起作用。显示您的结果和您的期望,如minimal reproducible example
-
这不起作用:文件
-
不,edit它在问题中,变成minimal reproducible example
-
std::hexI/O 操纵器旨在修改整数的输出(作为文本)。在您的情况下,它充其量什么都不做(因为您想编写二进制数据)。您必须编写自己的“格式化程序”,但这很简单......
标签: c++ encryption hex rsa crypto++