将作为字符串给出的大数字转换为 OpenSSL BIGNUM [英] Convert a big number given as a string to an OpenSSL BIGNUM
本文介绍了将作为字符串给出的大数字转换为 OpenSSL BIGNUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 OpenSSL 库将表示大整数的字符串 p_str
转换为 BIGNUM
p
.
I am trying to convert a string p_str
representing a big integer to a BIGNUM
p
using the OpenSSL library.
#include <stdio.h>
#include <openssl/bn.h>
int main ()
{
/* I shortened the integer */
unsigned char *p_str = "82019154470699086128524248488673846867876336512717";
BIGNUM *p = BN_bin2bn(p_str, sizeof(p_str), NULL);
BN_print_fp(stdout, p);
puts("");
BN_free(p);
return 0;
}
编译:
gcc -Wall -Wextra -g -o convert convert.c -lcrypto
但是,当我执行它时,我得到以下结果:
But, when I execute it, I get the following result:
3832303139313534
推荐答案
unsigned char *p_str = "82019154470699086128524248488673846867876336512717";
BIGNUM *p = BN_bin2bn(p_str, sizeof(p_str), NULL);
使用 int BN_dec2bn(BIGNUM **a, const char *str)
代替.
当您有一个 bytes
数组(而不是 NULL 终止的 ASCII 字符串)时,您将使用 BN_bin2bn
.
You would use BN_bin2bn
when you have an array of bytes
(and not a NULL terminated ASCII string).
手册页位于 BN_bin2bn(3)
.
The man pages are located at BN_bin2bn(3)
.
正确的代码如下所示:
#include <stdio.h>
#include <openssl/bn.h>
int main ()
{
static const
char p_str[] = "82019154470699086128524248488673846867876336512717";
BIGNUM *p = BN_new();
BN_dec2bn(&p, p_str);
char * number_str = BN_bn2hex(p);
printf("%s\n", number_str);
OPENSSL_free(number_str);
BN_free(p);
return 0;
}
这篇关于将作为字符串给出的大数字转换为 OpenSSL BIGNUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文