首页 > 解决方案 > Crypto++ HexEncoder 无法始终如一地工作

问题描述

这是我的代码

#include <cryptopp/hex.h>

#include <string>
#include <iostream>

void hexlify(CryptoPP::byte* bytes, std::string &hex_string, size_t size)
{
    CryptoPP::StringSource ss(bytes, size, true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hex_string)));
}

void unhexlify(std::string hex_string, CryptoPP::byte* &bytes)
{
    std::string decoded;

    CryptoPP::StringSource ss(hex_string, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(decoded)));

    std::cout << decoded + "\n"; // For testing
    bytes = (CryptoPP::byte*)decoded.data();
}

int main()
{
    std::string seed = "BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60";
    std::cout << "\n" + seed + "\n";

    CryptoPP::byte* b;
    unhexlify(seed, b);

    std::string s;
    hexlify(b, s, 32);

    std::cout << s;
    std::cout << "\n\n";
}

它应该获取 32 字节、64 个字符的十六进制字符串seed,打印它,将其转换为字节,然后将其转换回十六进制字符串 ( s) 并打印。我还让它在unhexlify函数中打印解码的字符串。

我的期望是输出看起来像这样:

BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60
?1#?`?v????D?L?M????`
BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60

第一行(seed)和第三行(s,种子转换为字节并返回)应该相同。很少,这正是发生的事情。但是,大多数时候第一行和第三行是完全不同的。

奇怪的是我对代码做了零更改,甚至没有重新编译它。但是每次我运行可执行文件时,它都会说一些不同的东西。更奇怪的是,每次运行第三行都不一样。同样,即使我在不​​更改代码或重新编译的情况下运行相同的可执行文件,也会发生这种情况。以下是 5 次运行的输出:

BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60
?1#?`?v????D?L?M????`
205BC0AE8B7F0000AB7615AD06EA16A2889A960301000000189C960301000000
BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60
?1#?`?v????D?L?M????`
205B40C2B87F0000AB7615AD06EA16A288FA9B060100000018FC9B0601000000
BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60
?1#?`?v????D?L?M????`
205BC07DA67F0000AB7615AD06EA16A2885A6C0B01000000185C6C0B01000000
BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60
?1#?`?v????D?L?M????`
205BC06EB47F0000AB7615AD06EA16A288DAAE090100000018DCAE0901000000
BF0F3123B21A60E5AB7615AD06EA16A2BD44D84CED4DCC10AA0413127F87DC60
?1#?`?v????D?L?M????`
205BC0F9EB7F0000AB7615AD06EA16A288DAF5010100000018DCF50101000000

第二行每次都是一样的,所以我知道问题出在hexlify函数和/或 Crypto++ HexEncoder 上。此外,该部分始终在和AB7615AD06EA16A2中匹配。匹配的部分从第 17 个字符到第 32 个字符。我尝试的每颗种子都会发生这种情况。但其他一切通常都不同。seeds

我不明白为什么它没有正确地将字节编码为十六进制字符串。而且我不明白为什么即使我没有对代码进行任何更改,它也很少起作用。而且我特别不明白每次运行相同的编译代码时输出如何不同。这里发生了什么?

标签: c++crypto++

解决方案


void unhexlify(std::string hex_string, CryptoPP::byte* &bytes)
{
    std::string decoded;

    CryptoPP::StringSource ss(hex_string, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(decoded)));

    std::cout << decoded + "\n"; // For testing
    bytes = (CryptoPP::byte*)decoded.data();  // <--
}

您将指针返回到decoded函数退出后死亡的局部变量所拥有的缓冲区。将悬空指针传递给hexlify. 要解决问题,请从返回字符串std::string unhexlify(std::string hex_string)并调用hexlify(bytes.data(), s. bytes.size()).


推荐阅读