其他分享
首页 > 其他分享> > OpenSSL测试-大数

OpenSSL测试-大数

作者:互联网

1. 基于OpenSSL的大数库计算2的N次方,N为你学号的后四位(5‘)

2^1024
源代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <openssl/bn.h>
int main(int argc, char* argv[])
{
    {
        BN_CTX* pBNctx = BN_CTX_new();
        BIGNUM* n = BN_new();
        BN_set_word(n, 1210);
        BIGNUM* two = BN_new();
        BN_set_word(two, 2);
        BIGNUM* r = BN_new();
        BN_exp(r, two, n, pBNctx);
        char* pR = BN_bn2dec(r);
        printf("2^1210 =%s \n", pR);
    }
    return 0;
}

2. 基于OpenSSL的大数库计算你以及前面5位同学和后面5位同学的8位学号的乘积,N为你学号的后四位(5‘)

20191205--20191215
源代码如下:

#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <openssl/bn.h>
int main(int argc, char* argv[])
{
    //
        BN_CTX* pBNctx = BN_CTX_new();
        BIGNUM* n = BN_new();
        BN_set_word(n, 1210);
        BIGNUM* two = BN_new();
        BN_set_word(two, 2);
        BIGNUM* r = BN_new();
        BN_exp(r, two, n, pBNctx);
        char* pR = BN_bn2dec(r);
        //printf("2^1210 =%s \n", pR);
        BIGNUM* rr = BN_new();
        BIGNUM* n1 = BN_new();
        BIGNUM* n2 = BN_new();
        int i;
        BN_set_word(n1, 20191205);
        BN_set_word(n2, 20191206);
        BN_mul(rr, n1, n2,pBNctx);
        for (i = 20191207; i <= 20191215; i++)
        {
            BN_set_word(n1, i);
           // char* pR = BN_bn2dec(n1);
           // printf(" =%s \n", pR);
            BN_mul(rr, rr, n1,pBNctx);
        }
        pR = BN_bn2dec(rr);
        printf("20191205 * …… * 20191215 =%s \n", pR);
    return 0;
}

结果如图:

3. 用Python或bc命令验证计算的正确性(5’)

bc验证
代码如下(包括bc安装)

标签:set,BIGNUM,大数,BN,OpenSSL,two,测试,new,include
来源: https://www.cnblogs.com/pogbar/p/16262319.html