在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务

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

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

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

提交代码(或代码链接)和运行结果截图

运行代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
//20201310
int main()
{
        BN_CTX *r[3];
        BIGNUM *a;
        BIGNUM *b;
        BIGNUM *c;
        char s[512+1] = "2";
        char t[512+1] = "51e"; //1310
	char *x;
        r[0] = BN_CTX_new();
        a = BN_new();
        b = BN_new();
        c = BN_new();
        BN_hex2bn(&a, s);
        BN_hex2bn(&b, t);
        BN_exp(c, a, b, r[0]);//r=pow(a,b)
        x = BN_bn2dec(c);
        puts(x);
        BN_free(a);
        BN_free(b);
        BN_free(c);
        BN_CTX_free(r[0]);
        free(x);
        return 0;
}


验证:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
//20201310
int main()
{
    BN_CTX *r[11];
    BIGNUM *bn[12];
    int i;
    const char s[11][9] = {"20201305", "20201306", "20201307", "20201308", "20201309", "20201310", "20201311", "20201312", "20201313", "20201314", "20201315"};
    char *out;
    char t[2] = "1";
    for (i = 0; i < 11; i++)
    {
        bn[i] = BN_new();
        BN_dec2bn(&bn[i], s[i]);
        r[i] = BN_CTX_new();
    }
    bn[11] = BN_new();
    BN_dec2bn(&bn[11], t);
    for (i = 0; i < 11; i++)
    {
        BN_mul(bn[11], bn[11], bn[i], r[i]);
    }
    out = BN_bn2dec(bn[11]);
    puts(out); //打印出字符串
    for (i = 0; i < 11; i++)
    {
        BN_free(bn[i]);
        BN_CTX_free(r[i]);
    }
    BN_free(bn[i]);
    free(out);
    return 0;
}


验证

posted on 2023-04-11 15:09  20201310寸头  阅读(34)  评论(0编辑  收藏  举报