OpenSSL 的 BN_bn2bin 函数出现问题
我正在尝试在 OpenSSL 中使用 BN_* 函数。具体来说,我有以下代码:
#import <openssl/bn.h>
BIGNUM * num = BN_new();
BN_set_word(num, 42);
char * buffer = malloc((BN_num_bytes(num)+1) * sizeof(char));
buffer[BN_num_bytes(num)] = '\0';
int len = BN_bn2bin(num, buffer);
printf("42 in binary is %s\n", buffer);
但是,当我这样做时,我没有得到一串 1 和 0。相反,它会打印 “二进制中的 42 是 *”
。据我所知,从我与之比较的网络上可用的非常有限的示例来看,我已经正确实现了这一点。
有什么想法为什么它不起作用吗?
I'm trying to use the BN_* functions in OpenSSL. Specifically, I have the following code:
#import <openssl/bn.h>
BIGNUM * num = BN_new();
BN_set_word(num, 42);
char * buffer = malloc((BN_num_bytes(num)+1) * sizeof(char));
buffer[BN_num_bytes(num)] = '\0';
int len = BN_bn2bin(num, buffer);
printf("42 in binary is %s\n", buffer);
However, when I do this, I don't get a string of ones and zeros. Instead it prints "42 in binary is *"
. As far as I can tell, and from the very limited number of examples available on the web that I've compared this to, I've implemented this correctly.
Any ideas why it isn't working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BN_bn2bin
不会创建可打印的字符串 - 相反,它会创建真正二进制的表示形式(即位序列)。更具体地说,它创建数字的大端表示。由于 42 适合一个字节,因此您得到一个字节 0x2a,即 ASCII 中的“*”。如果您想要 0/1 表示,则需要迭代所有字节,并自己进行打印(例如使用移位或查找表)。
BN_bn2bin
doesn't create a printable string - instead, it creates a representation that is truly binary (i.e. a sequence of bits). More specifically, it createas a big-endian representation of the number. Since 42 fits into one byte, you get one byte 0x2a, which is "*" in ASCII.If you want a 0/1 representation, you need to iterate over all bytes, and do the printing yourself (e.g. with shifting or a lookup table).
下面的一些代码实际上将
BN_bn2bin
输出转换为可打印字符串,就像BN_bn2dec
和BN_bn2hex
的输出一样。它位于 NodeJS 库中,但为了速度而用 C++ 编写。花了我一整天的时间,可能不是最佳的(因为从大学第一年起我就没有写过任何 C++ 代码)。但它通过了一系列单元测试,所以我知道它有效!https://github.com/malcolmocean/node-bignum
Here's some code that actually turns the
BN_bn2bin
output into a printable string just like the output ofBN_bn2dec
andBN_bn2hex
. It's in a NodeJS library but is written in C++ for speed. Took me all day and probably isn't optimal (because I haven't written any C++ code since first year of uni). But it passes a bunch of unit tests so I know it works!https://github.com/malcolmocean/node-bignum