Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C++

Applied Crypto++: Pseudo Random Number Generators

Rate me:
Please Sign up or sign in to vote.
4.77/5 (35 votes)
10 Apr 2008CPOL17 min read 196.2K   5.9K   85  
How to Choose a Crypto++ Pseudo Random Number Generator
// sample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

// Runtime Library Includes
#include <string>
#include <iostream>
#include <iomanip>

// Crypto++ Include
#include "osrng.h" // PRNG

int main(int argc, char* argv[])
{   
    // Scratch Area
    //   Use as seed to the LCG
    const unsigned int BLOCKSIZE = 16 * 8;
    byte pcbScratch[ BLOCKSIZE ];

    std::cout << "Seed:" << BLOCKSIZE << std::endl << std::endl;
    
    // 1st LCG
    CryptoPP::LC_RNG lcg1( BLOCKSIZE /*32 bit word */);
    lcg1.GenerateBlock( pcbScratch, BLOCKSIZE );

    // Output
    std::cout << "The 1st LCG produced:" << std::endl;
    for( unsigned int i = 0; i < BLOCKSIZE; i++ )
    {
        std::cout << "0x" << std::setbase(16) << std::setw(2) << std::setfill('0');
        std::cout << static_cast<unsigned int>( pcbScratch[ i ] ) << " ";
    }
    std::cout << std::endl;
    
    // @nd LCG
    CryptoPP::LC_RNG lcg2( BLOCKSIZE /*32 bit word */);
    lcg2.GenerateBlock( pcbScratch, BLOCKSIZE );

    // Output
    std::cout << "The 2nd LCG produced:" << std::endl;
    for( unsigned int i = 0; i < BLOCKSIZE; i++ )
    {
        std::cout << "0x" << std::setbase(16) << std::setw(2) << std::setfill('0');
        std::cout << static_cast<unsigned int>( pcbScratch[ i ] ) << " ";
    }
    std::cout << std::endl;    

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions