我试图在c++中使用openssl库添加两大数字,但我真的不知道如何使用它。
openssl - BN_add()的文档在这里add.html
//#include <openssl/bn.h>
//BN_add() adds a and b and places the result in r (r=a+b).
//r may be the same BIGNUM as a or b.
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);a & b对我的期望是什么?一个字节数组?一根绳子?
任何在openssl中添加大数并有一个简单示例的人?
发布于 2022-01-20 17:15:37
要使用BN_add,您需要三个BIGNUM工件:要添加的两个操作数和存储结果的位置。操作数的来源可能会有所不同,这取决于源数据的性质和其中可能的转换。结果应通过BN_new获得。
下面是一个使用两个十进制数字字符串的简单示例:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
int main()
{
const char n1[] = "12345678912345789123456789123456789";
const char n2[] = "12345678912345789123456789123456789";
BIGNUM *bn1 = NULL;
BN_dec2bn(&bn1, n1);
BIGNUM *bn2 = NULL;
BN_dec2bn(&bn2, n2);
BIGNUM *bn3 = BN_new();
BN_add(bn3, bn1, bn2);
char *n3 = BN_bn2dec(bn3);
printf("%s\n%s\n%s\n", n1, n2, n3);
OPENSSL_free(n3); // don't forget to free this.
BN_free(bn1);
BN_free(bn2);
BN_free(bn3);
return 0;
}输出
12345678912345789123456789123456789
12345678912345789123456789123456789
24691357824691578246913578246913578请注意,我们确保释放所获得的所有内容,其中包括将结果转换为十进制字符字符串,根据BN_bn2dec文档,结果字符串必须用OPENSSL_free释放。
这不是唯一的方法,不同的方法高度依赖于源数据。例如,我们可以修改上面的程序,将大端字节八进制中的两个256位数字转换为BIGNUM。要做到这一点,我们可以使用BN_bin2bn。然而,代码的其余部分看起来类似(除了一些必须释放的额外转换缓冲区)。
#include <stdio.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
int main()
{
unsigned char n1[32];
unsigned char n2[32];
RAND_bytes(n1, sizeof n1);
RAND_bytes(n2, sizeof n2);
BIGNUM *bn1 = BN_bin2bn(n1, sizeof n1, NULL);
BIGNUM *bn2 = BN_bin2bn(n2, sizeof n2, NULL);
BIGNUM *bn3 = BN_new();
BN_add(bn3, bn1, bn2);
char *s1 = BN_bn2dec(bn1);
char *s2 = BN_bn2dec(bn2);
char *s3 = BN_bn2dec(bn3);
printf("%s\n%s\n%s\n", s1, s2, s3);
OPENSSL_free(s1);
OPENSSL_free(s2);
OPENSSL_free(s3);
BN_free(bn1);
BN_free(bn2);
BN_free(bn3);
return 0;
}输出(显然是不同的)
12848991999079618356122471791635779303297173135339588614513279277444395635360
47516096440756489210553139954635811049213722081352926332729551875133172387567
60365088439836107566675611746271590352510895216692514947242831152577568022927最后,一些操作利用上下文来执行操作。对于我们在这里所做的一次性操作来说,这并不是特别有益,但是大量的密码算法代码会对大数字执行许多复杂的操作。提供临时存储上下文来管理本来会有大量内存分配和发布的内容,这是上下文的主要用途。
下面您将看到一个示例,生成两个128位数作为随机点,然后将它们相乘以产生所需的结果。
#include <stdio.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
int main()
{
unsigned char n1[16];
unsigned char n2[16];
RAND_bytes(n1, sizeof n1);
RAND_bytes(n2, sizeof n2);
BIGNUM *bn1 = BN_bin2bn(n1, sizeof n1, NULL);
BIGNUM *bn2 = BN_bin2bn(n2, sizeof n2, NULL);
BIGNUM *bn3 = BN_new();
// create context
BN_CTX *ctx = BN_CTX_new();
BN_mul(bn3, bn1, bn2, ctx);
BN_CTX_free(ctx);
char *s1 = BN_bn2dec(bn1);
char *s2 = BN_bn2dec(bn2);
char *s3 = BN_bn2dec(bn3);
printf("%s\n%s\n%s\n", s1, s2, s3);
OPENSSL_free(s1);
OPENSSL_free(s2);
OPENSSL_free(s3);
BN_free(bn1);
BN_free(bn2);
BN_free(bn3);
return 0;
}输出(显然是不同的)
132784775043065614238831600062417274250
106732277254089889576391506780872414280
14172421425018434896232647200508450669828492339359021022467696136374376290000有各种各样的网站,您可以在这里验证上面的产品是否准确,如果您有任何疑问的话;我意识到长时间分发比我们之前做的简单的加法操作要麻烦得多。
https://stackoverflow.com/questions/70789893
复制相似问题