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 << "nn";
}

它应该取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上。此外,区段AB7615AD06EA16A2seeds中总是匹配。匹配的部分从第17个字符到第32个字符。我尝试的每一粒种子都会发生这种情况。但其他一切通常都不一样。

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

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())

此函数看起来可疑:

void unhexlify(std::string hex_string, CryptoPP::byte* &bytes)
{
std::string decoded;
// stuff added to decoded, maybe...
bytes = (CryptoPP::byte*)decoded.data();
}

decoded是一个局部变量,当函数退出时会被破坏,但您正在使bytes指向它的内部数据,从而导致一个悬空指针(这是未定义的行为,不同的输出由此解释。(

您应该只返回解码后的返回值(按值(,并将其存储在调用方的堆栈中。然后在该函数返回调用代码后,将字节设置为其数据。

最新更新