【发布时间】:2014-07-23 18:58:24
【问题描述】:
我目前正在研究如何使用 Crypto++ 进行加密/解密。为了测试我的项目,我获得了以下值来帮助设置和验证我的项目是否正常工作:
original string: "100000"
encrypted value: "f3q2PYciHlwmS0S1NFpIdA=="
key and iv: empty byte array
key size: 24 bytes
iv size: 16 bytes
项目运行并解密加密值没问题,但不是返回
"100000"
返回
"1 0 0 0 0 0 "
每个空格实际上是“\0”。这是我用于解密的最小代码:
#include "modes.h"
#include "aes.h"
#include "base64.h"
using namespace CryptoPP;
void main()
{
string strEncoded = "f3q2PYciHlwmS0S1NFpIdA==";
string strDecrypted;
string strDecoded;
byte abKey[24];
byte abIV[AES::BLOCKSIZE];
memset(abKey, 0, sizeof(abKey));
memset(abIV, 0, AES::BLOCKSIZE);
AES::Decryption cAESDecryption(abKey, sizeof(abKey));
CBC_Mode_ExternalCipher::Decryption cCBCDecryption(cAESDecryption, abIV);
StringSource(strEncoded, true, new Base64Decoder(new StringSink(strDecoded)));
StreamTransformationFilter cDecryptor(cCBCDecryption, new StringSink(strDecrypted));
cDecryptor.Put(reinterpret_cast<const byte*>(strDecoded.c_str()), strDecoded.size());
cDecryptor.MessageEnd();
}
我可以按原样使用解密值,但我需要帮助理解的是为什么解密值显示“1 0 0 0 0 0”而不是“100000”?顺便说一句,这是在 VS2005 中作为 Windows 控制台应用程序构建的,Crypto++ 作为静态库,我使用调试模式来查看值。
【问题讨论】:
-
你输入的字符是8位还是16位?
-
@bmm6o 我不确定如何准确检查。如果您要问的话,我将字符集设置为“未设置”。
-
您的代码示例不够完整,我无法引用任何变量,但是使用“100000”的地方,它存储在什么地方?它的 c_str() 的 type 和 sizeof 是什么?
-
@bmm6o 我无法告诉你,因为我无法直接访问未加密的值,但 jww 正确地知道它是在 C# 中创建的,因此导致加密字符串为 16 位二进制字符串。
-
加密算法总是对原始字节进行操作,你有责任正确解释这些字节——包括在两端使用相同的字符编码。
标签: c++ encryption aes crypto++