c++ - 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 个字符。我尝试的每颗种子都会发生这种情况。但其他一切通常都不同。seed
s
我不明白为什么它没有正确地将字节编码为十六进制字符串。而且我不明白为什么即使我没有对代码进行任何更改,它也很少起作用。而且我特别不明白每次运行相同的编译代码时输出如何不同。这里发生了什么?
解决方案
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())
.
推荐阅读
- django - drf 壮观未在 swagger ui 中显示查询参数
- contentful - 如何使用 ruby on rails 仅使用 url insted 的 entry-id 获取内容条目?
- java - 令牌“{”上的语法错误,在此令牌切换 (char) 之后需要 SwitchLabels
- amazon-web-services - 如果运行的底层 Powershell 脚本失败,如何使 Ansible 任务失败?
- c# - 不正确的警告将 null 文字或可能的 null 值转换为不可为 null 的类型
- reactjs - 在 rooks 中使用 useDebounce 时如何设置加载为真?
- javascript - 为什么这个 getter 方法在 React 中被调用了多次?
- azure-service-fabric - Service Fabric 中的 Stateless Worker 服务在同一进程中重新启动
- c# - 如何在 C# 的套接字程序中通过网络发送/接收数据
- reactjs - 如何将组件从子组件注入根组件?