C语言利用OPENSSL 生成定制位的随机数

在linux命令行下,输入
man BN

查看所有的关于大数运算的API接口,此处,我们只对大数的随机数生成进行简单的举例,满足大部分需求。

随机数生成的API如下所示,

        #include <openssl/bn.h>

        int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);

        int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);

        int BN_rand_range(BIGNUM *rnd, BIGNUM *range);

        int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);


BN_rand(BIGNUM *rnd,int bits,int top,int bottom):生成一个强密码学随机函数,是一个无法预测的随机数。rnd:需要生成的随机数,bits:需要生成随机数的位数,top:为设置最高位的值,top=-1表示最高位设置为0,top=0表示最高位设置为1,top=1时,最高两位都设置成1,bottom:不为0时,可以生成奇的随机数。测试程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>

void main(){
	BIGNUM *rnd;
	rnd = BN_new();
	
	int length;
	char * show;
	//BN_random
	int bits = 80;
	int top =0;
	int bottom = 0;
	
	//测试top = -1
	top = -1;
	bottom = 0;
	BN_rand(rnd,bits,top,bottom);	
	length = BN_num_bits(rnd);
	show = BN_bn2dec(rnd);
	printf("length:%d,rnd:%s\n",length,show);
	BN_free(rnd);
}
BN_pseudo_rand:每次生成相同的随机数序列,便于调试。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值