Problems with CryptoPP C++ AES-256+Base64
Problems with CryptoPP C++ AES-256+Base64
有人可以告诉我为什么解密开始搞砸吗?它适用于短字符串,但正如您所看到的那样,它会搞砸。我认为这与字符串转换有关。
std::string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource encryptor(str_in, true,
new CryptoPP::StreamTransformationFilter(encryption,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(str_out),
false // do not append a newline
)
)
);
return str_out;
}
std::string decrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption decryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource decryptor(str_in, true,
new CryptoPP::Base64Decoder(
new CryptoPP::StreamTransformationFilter(decryption,
new CryptoPP::StringSink(str_out)
)
)
);
return str_out;
}
这将是我的程序输出
key:qwertyuiopasdfghjklzxcvbnmqwerty
IV:0123456789123456
STR:I do not like green eggs and ham I do not like them Sam-I-Am. Try them, try them, and you may! Try them and you may, I say. I will not eat them in a house, i will not eat them with a mouse,i will not eat them in a box i will not eat them with a fox, i will not eat them here of there i will not eat them anywhere, I do not like green eggs and ham i do not like them sam i am
STR_ENCRYPTED: ffyHj0rFQ0fn+jJcuZAznaioo+2oqqq+7ayjqe2lrKBF8s6QLdosGuIXzz/+vL+Bz
c3Nzc3Nzc3Nzc3Nzc3Nzd78yyKlu7P47yMSlKi7AhEyLs55pj9nZcEIPPadhISD4bQSGVWiWGbEMr7Ev
UCA+f9XQnePvQrfDwpegOLwYk8YyjXa9rLprhk7gAOU4LcdSRT6Udgohsolvrick6CSyUB9gJmkK6Ng1
MjSw4zBQkYMmt7oobkObBQY5XJHcTX5fVGXE5MJsVkQqGqAAKwD6jq4yZcG26WfA9LkwVgj0AwpxjKGV
VeYM/HKK9gzDA9u0/x0y/H4be8rpOYXPyrxXB8++iBL6cFz/Hq+y37uznfmqgAFdTkoW9FsHcGfmxZpJ
PYqrPKKwbt0EuMVGT1Z1F8kgvnwGiAg7/t7oa8RFStF3dsBd5LIYujx0nbnebSrkAFR0qMPzMDF4+Pox
n8KaEm6dtRYGEyYBfJWju+kWqug7aTtrKA=
STR_DECRYPTED: I do not like green eggs and ham I do not like t y
them, try them π♥┤k≥¬¿♀┼±;PIINry them and you 2ÜÅ║Bp╟↕┬╟ôM/=»éll not eat them ä
┘7£§σKΦsuQ^m_♦ll not eat them ┴W‼%lt├í┘╒(┐è╝°4ill not eat them≈u♦Z╦▬hR╬▼)♀òε↔┴ n
ot eat them wi▲1╣♠<5á"µi+┌≥τ<æ not eat them he+g═╚╕⌠σû∟í╨♀RV█ñll not eat them ÆΘ
..%♂▓╟Ñnot like them sam i amg]╠£▼n┬☺
Press any key to continue . . .
编辑
这就是我在程序中调用代码的方式,我在Windows 7上使用Visual Studio 2012的liscensed版本。
std::string szEncryptionKey= "qwertyuiopasdfghjklzxcvbnmqwerty";;
std::string szEncryptionIV= "0123456789123456";
std::string str="I do not like green eggs and ham I do not like them Sam-I-Am. Try them, try them, and you may! Try them and you may, I say. I will not eat them in a house, i will not eat them with a mouse,i will not eat them in a box i will not eat them with a fox, i will not eat them here of there i will not eat them anywhere, I do not like green eggs and ham i do not like them sam i am";
std::string str_encrypted = encrypt(str, szEncryptionKey, szEncryptionIV);
std::string str_decrypted = decrypt(str_encrypted, szEncryptionKey, szEncryptionIV);
std::cout<< "str encrypted: "<<str_encrypted<<std::endl;
std::cout<< "str decrypted: "<<str_decrypted<<std::endl;
供您参考,这对我来说效果很好:
int main()
{
std::string key = "qwertyuiopasdfghjklzxcvbnmqwerty";
std::string IV = "0123456789123456";
std::string input = "I do not like green eggs and ham I do not like them Sam-I-Am. Try them, try them, and you may! Try them and you may, I say. I will not eat them in a house, i will not eat them with a mouse,i will not eat them in a box i will not eat them with a fox, i will not eat them here of there i will not eat them anywhere, I do not like green eggs and ham i do not like them sam i am";
//Your encrypt function
auto encr = encrypt(input, key, IV);
std::cout << encr << std::endl;
//Your decrypt function
auto decr = decrypt(encr, key, IV);
std::cout << decr << std::endl;
}
另外,我看到您在加密和解密函数中调用new
时没有关联的delete
。我认为这是因为库正在代表您删除对象?
编辑
noloader确认Crypto++确实负责删除对象。谢谢,无负载器!
我修好了!我在库中将 AES::BLOCKSIZE 更改为 32,因为我正在玩 rijndael。由于sci.crypt上的一个答案,我现在已经找到了错误,该答案指出错误是每16个字节(或AES的块大小)
相关文章:
- AES加密到解密未正确输出
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- QT:AES-256-CBC 根据 PHP 代码在C++中加密/解密
- AES 256-cbc 加密C++使用 OpenSSL
- 使用 AES-256 CBC 加密文本文件
- 如何使用用户定义的密码初始化AES-256密钥
- 如何通过AES-256-CBC使用密码代替密钥和IV加密文件
- AES 256 加密 - 相当于 Java 的 Qt
- 在 AES 256 加密++中获取十六进制加密字符串
- AES 256 encryption in C++ and Qt 5
- 博坦 AES-256,32 位初始矢量
- 解密AES-256加密文件不起作用
- AES、128 和 256 密钥长度无效
- Problems with CryptoPP C++ AES-256+Base64
- AES-NI 256位块加密
- 是否有aes 256与CBC加密在ubuntu/linux/unix
- OpenSSL: AES CCM 256位大文件块加密:是否可能?
- 生成AES (AES-256)查找表
- AES-256 使用 ECB 操作模式的 OpenSSL 库进行加密
- AES 256 位加密在 pdf 文件中