我今天一整天都在尝试找出如何读取二进制文件并对其进行解密。
在我的文件中,前 4 个字节描述文件格式,后 32 个字节是 header ,使用 Blowfish 加密。
所以我写了这段代码来做到这一点:
string file = "C:\\test.bin";
byte *header = new byte[32];
FILE *data = fopen(file.c_str(), "r");
if(data == NULL)
{
return 1; //Error opening file!
}
char type[6];
type[5] = 0;
if(fread(type, sizeof(type) - 1, 1, data) < 1)
{
return 2;
}
if(strcmp(type, "ABCD") != 0)
{
return 3;
}
if(fread(header, sizeof(header), 1, data) < 1)
{
return 2; //Error reading file!
}
vector<byte> key;
key.push_back(0xAA);
key.push_back(0xBB);
key.push_back(0xCC);
key.push_back(0xDD);
key.push_back(0xAA);
key.push_back(0xBB);
key.push_back(0xCC);
key.push_back(0xDD);
ECB_Mode<Blowfish>::Decryption decryption(key.data(), key.size());
byte out[32];
decryption.ProcessData(out, header, 32);
FILE *outer = fopen("C:\\out.bin", "w");
fwrite (out, sizeof(byte), sizeof(out), outer);
但这并不能正确解密数据。
我做错了什么?
最佳答案
这里有很多东西有点臭
fopen
应该使用"rb"
和"wb"
二进制模式- 你应该使用
memcmp
而不是strcmp
- 您没有验证
fread
实际读取了 4 个字节 - 对于二进制数据,您应该更喜欢
unsigned char
(与符号扩展和溢出时的未定义行为有关的陷阱更少) - 如果您使用的是 C++,为什么首先要使用 cstdlib、cstdio 和 cstring?
这是一个错误
if(fread(header, sizeof(header), 1, data) < 1)
sizeof (header)
在这里是sizeof(byte*)
,而不是你似乎期望的32
这里是对 C++ 风格代码的快速回顾:Update 为我的真实往返测试添加了一个长度字段(见下文)。
解密器.cpp
:
#include <fstream>
#include <algorithm>
#include <iterator>
#include <crypto++/blowfish.h>
#include <crypto++/modes.h>
static std::vector<byte> const key { 's','e','c','r','e','t' };
static byte const SIGNATURE[] = "ABCD"; //{ 'A','B','C','D' };
int main()
{
if (std::ifstream data {"test.bin", std::ios::binary})
{
char type[] = { 0, 0, 0, 0 };
if (!data.read(type, 4))
{
return 2;
}
auto mismatch = std::mismatch(std::begin(SIGNATURE), std::end(SIGNATURE), std::begin(type));
if (mismatch.first != std::end(SIGNATURE))
{
return 3;
}
uint32_t length = 0;
if (!data.read(reinterpret_cast<char*>(&length), sizeof(length))) // TODO use portable byte-order
{
return 4;
}
std::vector<byte> const ciphertext { std::istreambuf_iterator<char>(data), {} };
// to read 32 bytes:
// std::copy_n(std::istreambuf_iterator<char>(data), 32, std::back_inserter(ciphertext));
assert(data.good() || data.eof());
assert(ciphertext.size() >= length);
assert(ciphertext.size() % CryptoPP::Blowfish::BLOCKSIZE == 0);
CryptoPP::ECB_Mode<CryptoPP::Blowfish>::Decryption decryption(key.data(), key.size());
std::vector<char> plaintext(ciphertext.size());
decryption.ProcessData(reinterpret_cast<byte*>(plaintext.data()), ciphertext.data(), plaintext.size());
plaintext.resize(length); // trim padding
std::ofstream out("out.bin", std::ios::binary);
out.write(plaintext.data(), plaintext.size());
} else
{
return 1; //Error opening file
}
}
我还没有文件来测试它。
更新 所以,我做了 an encryptor.cpp
现在也是。
echo "Hello world" | ./encryptor
结果是一个 40 字节的文件(sig + length + ciphertext = 4 + 4 + 32 = 40),base64:
base64 test.bin
QUJDRAwAAABCaDMrpG0WEYePd7fI0wsHAQoNkUl1CjIBCg2RSXUKMg==
现在,解密测试结果很好。请注意,我发现我需要确保对 BLOCKSIZE 进行了填充,因此我添加了一个 length
字段来存储明文的实际大小,以避免在解密后出现尾随垃圾。
你可以通过这样做看到往返
echo 'Bye world!!' | ./encryptor && ./decryptor && cat out.bin
解密后确实会打印问候语。
特别注意TODO
。你可能应该 use StreamTransformationFilter
which adds padding as required.
关于c++ - 如何正确使用crypto++ Blowfish,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505297/