c++ - 如何正确使用crypto++ Blowfish

标签 c++ binary blowfish crypto++

我今天一整天都在尝试找出如何读取二进制文件并对其进行解密。

在我的文件中,前 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/

相关文章:

c++ - 在 Unix/Linux 中将 Windows 文件时间转换为秒

r - 从 r 中的多个列表制作二进制(存在/不存在)数据矩阵

java - 使用 Blowfish 时如何修复 Java 中的 NoSuchAlgorithmException?

ruby - 在 ruby​​ 中解密河豚加密字符串仅返回该字符串的 1/2

c++ - 无法将运行时类绑定(bind)到 XAML T 必须是 WinRT 类型

c++ - 无法在 Visual Studio 2015 Update 1 CMake 中将工具集设置为新的 v140_clang_3_7

c++ - 在 C++11 中使用 "auto"推导出的 lambda 类型是什么?

python - 有没有一种简单的方法可以从 mpg 文件中打印出 1's and 0' s?

algorithm - Xnary(像二进制但不同)计数

java - 在 Java 中使用自定义盐进行 Blowfish 加密