问题
I am getting "RSA/OAEP-MGF1(SHA-1): ciphertext length of 154 doesn't match the required length of 192 for this key" when I try to decrypt encrypted session key which uses crypto++ library.
Following are code snippets for the same:
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;
}
Here I have given both code for encrypting and decrypting session key.
Can some one tell me Why I am getting above exception ?
Please note that : I am using Version 5.6.3 of crypto++ library.
Thanks in Advance.
回答1:
ciphered.assign((char *)block.BytePtr());
The cipher text will likely have an embedded NULL
. You cannot treat it like a char*
.
Use the fourth append overload instead, which provides the pointer and the length of a buffer:
ciphered.assign((char *)block.BytePtr(), block.SizeInBytes());
You are still casting it to a char*
to get it into the std::string
object. But its really a more like a Rope - its a string of octets without character traits.
来源:https://stackoverflow.com/questions/38545180/getting-exception-rsa-oaep-mgf1sha-1-ciphertext-length-of-154-doesnt-match