我有一个使用CryptoPP的问题。我使用AES,并且想通过将它编码为base64来表示二进制密文。在二进制数据(密文)上使用CryptoPP :: Base64Encoder
我的问题是,运行下面的代码时,我随机得到的断言错误:
std::string encoded;
// ciphertext is of type std::string from AES
CryptoPP::StringSource(ciphertext, true,
new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded)));
具体的断言错误是:
Assertion failed: m_allocated, file include\cryptopp\secblock.h, line 197
因为这个“乱”行为,它领先我相信问题在于密文的内容。
我的问题是:我这样做是否正确?我被困了一段时间,并一直在研究一点没有成功。我能找到最接近的是:http://www.mail-archive.com/[email protected]/msg06053.html
我的完整实现:
std::string key = "key";
std::string in = "This is a secret message.";
CryptoPP::SHA1 sha;
byte digest[CryptoPP::SHA1::DIGESTSIZE];
sha.CalculateDigest(digest, reinterpret_cast<const byte *>(key.c_str()), key.length());
byte iv[CryptoPP::AES::BLOCKSIZE];
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
CryptoPP::AES::Encryption encrypt(reinterpret_cast<const byte *>(digest), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbc_encrypt(encrypt, iv);
std::string ciphertext;
CryptoPP::StreamTransformationFilter encryptor(cbc_encrypt,
new CryptoPP::StringSink(ciphertext));
encryptor.Put(reinterpret_cast<const unsigned char *>(in.c_str()), in.length() + 1);
encryptor.MessageEnd();
std::string encoded;
CryptoPP::StringSource(ciphertext, true,
new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded)));
你是什么意思的错误?崩溃?输出无效?输出错误? –
用特定的断言错误更新了我的问题。 – Parazuce