CryptoPP的BlockingRng算法的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lunar_Queen/article/details/81982753

随机数发生器是密码学的一个重要原语。密码学库CryptoPP中提供了一些随机数发生器算法。今天,讲解一下BlockingRng随机数发生器算法的使用。
注意:该算法是对linux下的 /dev/random和/dev/srandom进行了封装,因此,该算法仅能在linux系统下使用。在其帮助文档可以看到相关声明,如下图所示。
这里写图片描述
源代码如下:

//#define BLOCKING_RNG_AVAILABLE
#include<cryptlib.h>
#include<osrng.h>//包含BlockingRng算法的头文件
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
using namespace CryptoPP;

#define Array_Size 64

int main()
{
    //该算法在Windows系统上不可以使用
    //定义一个BlockingRng对象
    BlockingRng  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;
}

猜你喜欢

转载自blog.csdn.net/Lunar_Queen/article/details/81982753