如何使用Crypto++并为RSA返回可打印的字节/字符数组

How to use Crypto++ and return printable byte/char array for RSA?

本文关键字:字节 字符 数组 打印 Crypto++ 何使用 并为 RSA 返回      更新时间:2023-10-16

我使用以下函数使用RSA加密包含不可打印字节的字节数组。但是,此函数返回一个字符串值和不可打印的未命中字节。

string RSAencrypt(RSA::PublicKey publicKey,string plain) {
string cipher;
try {
AutoSeededRandomPool rng;
RSAES_OAEP_SHA_Encryptor e(publicKey);
StringSource ss1(plain, true,
new PK_EncryptorFilter(rng, e,
new StringSink(cipher)
) // PK_EncryptorFilter
); // StringSource
}
catch (CryptoPP::Exception & e)
{
cerr << "Caught Exception..." << endl;
cerr << e.what() << endl;
}
return cipher;
}

如何返回字节数组?

来自文档:

new HexEncoder(
new StringSink(encoded)
) // HexEncoder

主页面底部第二行还列出了各种其他编码器。Base64Encoder最有意义。

当然,在解密之前,您需要再次解码