麻烦OpenSSL的BN_bn2bin功能 [英] Trouble with OpenSSL's BN_bn2bin function

查看:2604
本文介绍了麻烦OpenSSL的BN_bn2bin功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用BN_ *功能的OpenSSL。具体来说,我有以下的code:

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

然而,当我这样做,我不明白的1和0的字符串。相反,它打印42二进制是*。至于我可以告诉大家,从的非常的的,我已经比这个在网络上可用的实例数量有限,我实现了这个正确的。

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?

推荐答案

BN_bn2bin 不产生打印字符串 - 相反,它创建了一个重新presentation了是真正的二进制(即比特序列)。更具体地,它createas数的大端重新presentation。自从42适合一个字节,你会得到一个字节0x2a,这是*的ASCII码。

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.

如果你想要一个0/1重presentation,你需要遍历所有字节,DO(移轴或查找表例如)印刷自己。

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

这篇关于麻烦OpenSSL的BN_bn2bin功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆