OpenSSL测试-大数

任务详情
0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
1. 基于OpenSSL的大数库计算2的N次方,N为你学号的后四位(5‘)
2. 基于OpenSSL的大数库计算你以及前面5位同学和后面5位同学的8位学号的乘积,N为你学号的后四位(5‘)
3. 用Python或bc命令验证计算的正确性(5’)
4.  **提交代码(或代码链接)和运行结果截图**

计算2的N次方

#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
int main()
{
        BIGNUM* a = BN_new(), * b = BN_new(), * t = BN_new();
        BN_CTX* ctx=BN_CTX_new();
        BN_dec2bn(&a, "2");//赋10进制值字符串到*a
        BN_dec2bn(&b, "1329");
        char* p;
        char* q;
        p = BN_bn2dec(a);//取a的十进制值,返回一个字符串指针p
        BN_exp(t,a,b,ctx);
        p=BN_bn2dec(t);
        printf("result=%s\n", p);
        BN_CTX_free(ctx);
        OPENSSL_free(p);
        OPENSSL_free(q);
        BN_free(a);
        BN_free(b);
        BN_free(t);
        BN_free(d);
        return 0;
}

运行结果

计算学号的乘积

#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
int main()
{
        BIGNUM* a = BN_new(), * b = BN_new(), * t = BN_new();
        BN_CTX* ctx=BN_CTX_new();
        char* p;
        char* q;
        BN_dec2bn(&a, "20191322");//赋10进制值字符串到*a
        BN_dec2bn(&b, "1");
        BIGNUM* d=BN_new();
        BN_dec2bn(&d,"1");
        for(int w=0;w<10;w++)
        {
            BN_add(a, a, b);
            p = BN_bn2dec(t);
            BN_mul(d, a, d, ctx);
            q = BN_bn2dec(d);
        }
        printf("result=%s\n", q);
        BN_CTX_free(ctx);
        OPENSSL_free(p);
        OPENSSL_free(q);
        BN_free(a);
        BN_free(b);
        BN_free(t);
        BN_free(d);
        return 0;
}

运行结果

运行结果和上次一样

posted @ 2022-05-12 14:53  修勾  阅读(106)  评论(0编辑  收藏  举报