c++ - 在解密 session key 期间出现异常 "RSA/OAEP-MGF1(SHA-1): ciphertext length of 154 doesn' t 与此 key 所需的长度 192 匹配

标签 c++ crypto++

当我尝试解密使用 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/

相关文章:

c++创建一个介于0.1和10之间的随机小数

c++ - boost::asio 不是完全符合 UNICODE 标准吗?

crypto++ - 在 Mac OS X 上安装 Crypto++ 5.6.2

c++ - 在 AES 256 Crypto++ 中获取十六进制加密字符串

c++ - 使用密文 Crypto++ CBC AES 加密存储 IV

c++ - mfc c++ 中是否存在用于 URL 编码的 API?

c++ - 从 void 指针转换(创建通用存储)有什么问题吗?

c++ - 为什么 C++ 编译器会在这里生成一个临时文件?

c - 如何检查 openssl 或 cryptopp 是否安装并使用系统中实际存在的库(已安装)?

c++ - cryptopp base64编码/解码后的值不一样