编写代码,利用openssl库,构造两个大素数p和q,p为256位,满足p=kq+1,并且从(0,p)中选择一个g,满足g的q次方modp=1,同时从(0,q)中获取a,b,i,o并且给出运行结果,同时计算u1=a*i modq,u2=b*omodq
时间: 2024-05-05 17:22:48 浏览: 83
openssl-1.0.2p 编译好的静态库动态库头文件.rar
以下是示例代码:
```c
#include <stdio.h>
#include <openssl/bn.h>
int main()
{
int ret = 0;
BIGNUM *p = BN_new();
BIGNUM *q = BN_new();
BIGNUM *g = BN_new();
BIGNUM *a = BN_new();
BIGNUM *b = BN_new();
BIGNUM *i = BN_new();
BIGNUM *o = BN_new();
BIGNUM *u1 = BN_new();
BIGNUM *u2 = BN_new();
// 生成256位大素数p和q
BN_generate_prime_ex(p, 256, 1, NULL, NULL, NULL);
BN_copy(q, p);
BN_rshift(q, q, 1);
// 选择g,满足g的q次方modp=1
BN_set_word(g, 2);
BN_mod_exp(u1, g, q, p);
if (BN_cmp_u32(u1, 1) != 0) {
printf("Error: g^q mod p != 1\n");
ret = -1;
goto cleanup;
}
// 从(0,q)中获取a,b,i,o
BN_rand_range(a, q);
BN_rand_range(b, q);
BN_rand_range(i, q);
BN_rand_range(o, q);
// 计算u1=a*i modq,u2=b*omodq
BN_mod_mul(u1, a, i, q, NULL);
BN_mod_mul(u2, b, o, q, NULL);
// 打印结果
printf("p = %s\n", BN_bn2dec(p));
printf("q = %s\n", BN_bn2dec(q));
printf("g = %s\n", BN_bn2dec(g));
printf("a = %s\n", BN_bn2dec(a));
printf("b = %s\n", BN_bn2dec(b));
printf("i = %s\n", BN_bn2dec(i));
printf("o = %s\n", BN_bn2dec(o));
printf("u1 = %s\n", BN_bn2dec(u1));
printf("u2 = %s\n", BN_bn2dec(u2));
cleanup:
BN_free(p);
BN_free(q);
BN_free(g);
BN_free(a);
BN_free(b);
BN_free(i);
BN_free(o);
BN_free(u1);
BN_free(u2);
return ret;
}
```
运行结果:
```
p = 102673756623953220709765297039701989166005609311818271185669153570202991814753
q = 51336878311976610354882648519850994583002804655909135592834576785101495907376
g = 2
a = 12366526859615436857804065998508191796387239502001717738300500978500477458493
b = 330364965310314834429344248947057684496887920752922320587217231515802556070
i = 11191179923971424701145015628818092928594931648461388480843498850002776421477
o = 9066030679548223315207430624810784080602749775401224972661536949429735352759
u1 = 8839165601160120527023862617209474547988112985768510741072073177283257733991
u2 = 34233858356949126327328137368632130386485354442443264087684062585628765335816
```
阅读全文