- 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
- 基于OpenSSL的大数库计算1000内的素数的乘积(5‘)
- 基于OpenSSL的大数库计算你以及后面15位同学的8位学号(数字)的乘积(5‘)
- 用Python或bc命令验证计算的正确性(5’)
- 提交代码(或代码链接)和运行结果截图
一、基于OpenSSL的大数库计算1000内的素数的乘积
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
int is_prime(int num)
{
if (num < 2)
return 0;
else if (num == 2)
return 1;
else if (num % 2 == 0)
return 0;
else
{
int i;
for (i = 3; i <= num / 2; i += 2)
{
if (num % i == 0)
return 0;
}
return 1;
}
}
int main()
{
BIGNUM *result = BN_new();
BIGNUM *num = BN_new();
BN_CTX *r = BN_CTX_new();
int i;
BN_set_word(result, 1);
for (i = 2; i < 1000; i++)
{
if (is_prime(i))
{
BN_set_word(num, i);
BN_mul(result, result, num, r);
}
}
char *result_str = BN_bn2dec(result);
printf("%s\n", result_str);
BN_free(result);
BN_free(num);
BN_CTX_free(r);
OPENSSL_free(result_str);
return 0;
}
![image](https://img2023.cnblogs.com/blog/2166406/202305/2166406-20230510082225531-1151966644.png)
二、基于OpenSSL的大数库计算你以及后面15位同学的8位学号(数字)的乘积
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
//20201212
int main()
{
BN_CTX *r[11];
BIGNUM *bn[12];
int i;
const char s[11][9] = {"20201207", "20201208", "20201209", "20201210", "20201211", "20201212", "20201213", "20201214", "20201215", "20201216", "20201217"};
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;
}
![image](https://img2023.cnblogs.com/blog/2166406/202305/2166406-20230510080442485-560682063.png)
三、用Python或bc命令验证计算的正确性
![image](https://img2023.cnblogs.com/blog/2166406/202305/2166406-20230510080505986-981153635.png)