- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试加密已解析为字符串的字节数组。这似乎适用于所有情况,但字节数组包含 0x00 的情况除外。
int main()
{
byte cipherTextWithZeroByte[32] = {
0xD3, 0xFA, 0xD6, 0xEC, 0x84, 0x4E, 0xD3, 0xD8,
0x2B, 0x76, 0x6C, 0xE8, 0x02, 0xF2, 0xB2, 0x6F,
0x00, 0xE8, 0x99, 0x8C, 0xEC, 0x4B, 0x3C, 0x7D,
0xAC, 0xDE, 0x86, 0x02, 0x51, 0xAB, 0x3F, 0x04
};
string cipherText((char *)cipherTextWithZeroByte);
string plainText = decrypt(cipherText, sizeof(cipherTextWithZeroByte));
return 1;
}
string decrypt(string cipherText, int size)
{
string decryptedText;
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption,
new CryptoPP::HexEncoder(
new CryptoPP::StringSink(decryptedText)));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipherText.c_str()), size);
stfDecryptor.MessageEnd();
return decryptedText;
}
在这种情况下,字节数组包含 0x00。这会导致 cipherText 被截短,从而导致长度无效。抛出异常说明:“StreamTransformationFilter:找到无效的 PKCS #7 block 填充”
所以我认为最好使用 ArraySource 和 ArraySink 来确保字符串不以零结尾。
int main()
{
byte cipherTextWithZeroByte[32] = {
0xD3, 0xFA, 0xD6, 0xEC, 0x84, 0x4E, 0xD3, 0xD8,
0x2B, 0x76, 0x6C, 0xE8, 0x02, 0xF2, 0xB2, 0x6F,
0x00, 0xE8, 0x99, 0x8C, 0xEC, 0x4B, 0x3C, 0x7D,
0xAC, 0xDE, 0x86, 0x02, 0x51, 0xAB, 0x3F, 0x04
};
vector<byte> cipherTextData(cipherTextWithZeroByte, cipherTextWithZeroByte + sizeof(cipherTextWithZeroByte) / sizeof(cipherTextWithZeroByte[0]));
vector<byte> plainTextData = decrypt(cipherTextData);
return 1;
}
vector<byte> decrypt(vector<byte> cipherText)
{
vector<byte> plainText;
plainText.resize(cipherText.size());
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryptor;
decryptor.SetKeyWithIV(key, sizeof(key), iv, sizeof(iv));
CryptoPP::ArraySource ss(&cipherText[0], cipherText.size(), true,
new CryptoPP::HexEncoder(
new CryptoPP::StreamTransformationFilter(decryptor,
new CryptoPP::ArraySink(plainText.data(), plainText.size()))));
return plainText;
}
在这种情况下,会抛出密文不是 key 长度的倍数的异常,这里显然不是这种情况。 ( key = 16 字节,密文 = 16 字节)。我认为该库将字节数组强制转换为一个字符串,在 0x00 字节之后省略了所有数据。
我做错了什么?
最佳答案
加密是二进制字节而不是字符操作。因此,加密输出不能转换为字符串。如果您需要字符串输出,请将加密数据编码为字符串兼容格式,常见格式为 Base64 和十六进制。
特别是按照“C”中的约定,字符串中的空字节类似于信号语言字符串的末尾。
即加密时将二进制数据编码为字符串格式,解密时首先使用编码操作的逆运算将字符串表示解码为二进制数据。
关于c++ - 使用 CryptoPP 库加密和解密字节数组/vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47791590/
我正在尝试使用 cryptopp,以下代码会导致 stringsource 函数发生访问冲突。这可能是什么原因?我以前成功运行过类似的代码,几乎没有什么区别。 AesHelper.cpp #inclu
我试图在 Arch Linux (3.12.9) 上通过 cabal 安装 happstack-server-tls 包,但出现了这个错误: Resolving dependencies... Con
我在 IOS 中成功编译并执行了 Cryptopp,但我真的很难在 Android 中使用它。 在这里,我使用的是 Cryptopp 5.6.3、NDK r10e 和 android studio 1
我有一个在 CBC 模式下使用 AES 算法的加密文件。我有数据库中的 key 。我正在尝试使用 cryptopp 5.6.2 库编译以下代码。它在没有 -Wall 标志的情况下编译,但是当我在下面启
这是 log1 应用程序输出: : ... 25 more W/System.err( 1500): java.lang.ClassNotFoundException: android.g
我正在玩 cryptopp,但在 Base64 编码/解码方面遇到了问题。 在下面的代码中,假设 sig 的值应该等于 tsig,但是它们在最后一个字符上是不同的(sig 比 >tsig 一个符号)。
我是第一次玩 Cryptopp,我找到了一个编码为十六进制的示例……一切都很好。现在我想将生成的 std::string 解码为原始字符串,但我得到的只是空字符串。 #include "stdafx.
我正在尝试运行一个使用 AES 加密和解密的程序。 (来自 http://www.codeproject.com/KB/security/AESProductKey.aspx) // From aes
我正在尝试用 C++ 中的 RSA 加密一些文本, 加密时我正在生成 n, e, d但是在尝试解密时,私钥初始值设定项说 key 无效... 所以我构建了一个生成 key 的代码,然后尝试在此之后立即
我正在尝试加密已解析为字符串的字节数组。这似乎适用于所有情况,但字节数组包含 0x00 的情况除外。 int main() { byte cipherTextWithZeroByte[32]
谁能分享一个有效的 RabinMillerTest() 示例?遗憾的是,我的 googlefu 不见了。 这是我的测试代码: #include "integer.h" #include "nbtheo
Crypto++ 库通过针对 cryptlib.lib 和 cryptopp.lib 进行编译来支持后期绑定(bind)。这需要使用 cryptopp.dll。当尝试通过 /DELAYLOAD:cry
我有要解密的流。我将它分成 block 并将每个 block 传递给下面的方法。我需要解密的数据是按 16 字节的 block 加密的,如果最后一个 block 小于 16,那么其余所有字节都用填充填
我在 Windows 中编译 cryptopp 项目时遇到以下错误。 C:\Users\Sajith\AppData\Local\Temp\ccxq8O8x.o:aescbc.cpp:(.text$_
我发现在 RHEL7 和 Debian9 上使用 cryptopp 生成 SHA3 哈希的行为存在非常奇怪的差异。如果我改用 SHA1 或 MD5 哈希,则两个平台上的输出是相同的。我已将其缩减为以下
如何将 cryptopp::integer 转换为 QString? 如果这很重要,我会在 Mac OS 上工作。我完全不知道该怎么做,只是尝试使用 QCA,但它还不够好! 最佳答案 How to c
我正在尝试使用 Crypto++ 在编译时散列一些字符串(不需要检索它们)库和一个 constexpr 函数。这是我到目前为止的代码: constexpr const char* operator "
我的程序可以加密文本并将其保存在文件中,并在从文件中获取密文后解密。 但我一直收到这个错误 terminate called after throwing an instance of 'Crypto
这个问题在这里已经有了答案: How to convert CryptoPP::Integer to char* (4 个答案) 关闭 6 年前。 我找不到将 CryptoPP::Integer(从
我对 Crypto++ 库没有任何经验。在我的项目中,我需要将 Integer 类型转换为 int。这就是我正在尝试的: int low_bound1=8; int low_bound2=9; Int
我是一名优秀的程序员,十分优秀!