当我尝试解密使用 crypto++ 库的加密 session key 时,收到“RSA/OAEP-MGF1(SHA-1): ciphertext length of 154 does not match the required length 192 for this key”。
以下是相同的代码片段:
std::string encrypt_session_key(PAES_KEY_WITH_IV pKey)
{
std::string ciphered;
CryptoPP::SecByteBlock block(pKey->key.size());
try {
CryptoPP::RSAES< CryptoPP::OAEP<CryptoPP::SHA> >::Encryptor enc(RSA_master_pubKey);
enc.Encrypt(rng, pKey->key, pKey->key.size(), block);
ciphered.assign((char *)block.BytePtr());
}
catch (const CryptoPP::Exception& e)
{
std::cerr << e.what() << std::endl;
b_success = false;
}
return ciphered;
}
PAES_KEY_WITH_IV decrypt_session_key(std::string & ciphered)
{
CryptoPP::SecByteBlock rec(ciphered.size());
CryptoPP::SecByteBlock block((const byte *)ciphered.data(), ciphered.size());
PAES_KEY_WITH_IV pKey = new AES_KEY_WITH_IV;
try {
CryptoPP::RSAES< CryptoPP::OAEP<CryptoPP::SHA> >::Decryptor dec(RSA_master_privKey);
dec.Decrypt(rng, block, block.size(), rec);
pKey->key = rec;
}
catch (const CryptoPP::Exception& e)
{
std::cerr << e.what() << std::endl;
b_success = false;
}
return pKey;
}
这里我给出了加密和解密 session key 的代码。
有人可以告诉我为什么我会遇到上述异常吗?
请注意:我使用的是 crypto++ 库的 5.6.3 版本。
提前致谢。
最佳答案
ciphered.assign((char *)block.BytePtr());
密文可能会嵌入一个NULL
。您不能将其视为 char*
。
使用fourth append
overload相反,它提供指针和缓冲区的长度:
ciphered.assign((char *)block.BytePtr(), block.SizeInBytes());
您仍在将其转换为 char*
以将其放入 std::string
对象中。但它确实更像是Rope
- 它是一个没有字符特征的八位字节字符串。
关于c++ - 在解密 session key 期间出现异常 "RSA/OAEP-MGF1(SHA-1): ciphertext length of 154 doesn' t 与此 key 所需的长度 192 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38545180/