CryptoPP的 AutoSeededRandomPool算法的使用

AutoSeededRandomPool随机数发生器也是一个CryptoPP的无需外部输入种子的随机数发生器。它会利用系统提供的RNG算法来产生随机数。该算法的设计思想由 Leonard Janke.提出。
这里写图片描述

源代码如下:

#include<cryptlib.h>
#include<osrng.h>//包含AutoSeededRandomPool算法的头文件
#include<rijndael.h>//AES算法
#include<des.h>//DES算法
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
using namespace CryptoPP;

#define Array_Size 64

int main()
{
	//定义一个AutoSeededRandomPool对象
	AutoSeededRandomPool  rng;
	//定义一个文件对象
	ofstream file("data.dat",ios_base::binary | ios_base::out);

	byte output[Array_Size];

	//产生1Gbits的数据。每调用一次随机数发生器,产生8*64=512bits的数据。
	//1Gbits = 1000*1000*1000。
	//1000*1000*1000/512 = 1953125。

	cout << "开始生成数据..." << endl;
	clock_t start = clock();

	for(int i=0; i < 1953125 ; ++i)
	{
		rng.GenerateBlock(output,Array_Size);
		file.write(reinterpret_cast<const char*>(output),Array_Size);
	}
	clock_t end = clock();
	cout << "数据生成完毕..." << endl;

	double  duration;
	duration = (double)(end - start) / CLOCKS_PER_SEC;
	cout << "生成数据总共耗时:" << duration << endl;

	file.close();

	return 0;
}

执行上述程序,运行结果如下图所示:
这里写图片描述

上面的显示结果是该随机数发生器产生1G bits数据的所花费的时间。
这里写图片描述

更多示例代码详见《深入浅出CryptoPP密码学库》随书电子文档:https://github.com/locomotive-crypto/crypto_book_1st

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值