//*********************************************************************************************
//* CBBS *
//*********************************************************************************************
//* Author: Ravey Alexandre *
//* Contact: CBBS@mars105.net *
//* Date: 07.2007 *
//* Summary: A C++ class implementation of the BBS (Blum Blum Shub) algorithm. *
//* This is a pseudorandom number generator intended for cryptographic usage. *
//* This pice of code demonstrate how to use the class. *
//* *
//* Copyright: You may use this code as you wish, as long as you do not sell, use it for *
//* commercial application, and you let this copyright notice. *
//* Do not take credit for my work, mark your change in the code and all will be *
//* fine. If you improve this code in some way, find bugs, or any thing, please *
//* let me know. *
//* You may NOT use this code in a commercial application, you do not write it so *
//* you do not have to be paid for it. If you want make commercial use of this, *
//* contact me. *
//* *
//* Disclamer: This software is provided "as is", without warranty of any kind. *
//* *
//* Version: 1.0 *
//* History: 1.0: *
//* Initial release. *
//*********************************************************************************************
#include "CBBS.h"
#define SEED_LEN 128 //Seed length in BYTE (NOT bit) ie 1 byte = 8 bit. have it?
#if SEED_LEN > 0
//Generate some random data for the test Seed.
//This is NOT the good way! do NOT generate your seed this way in a production environment.
//the rand() function provide all but random data.
//I use this here only for demonstration.
int Rnd(char* Dest, unsigned int Len)
{
unsigned int i;
for(i=0;i<Len;i++)
Dest[i] = (char)rand();
return 0;
}
#endif //SEED_LEN > 0
int main()
{
//For this demonstration i fill the seeds with os random function
//Do NOT do this in your production application (never to many says of that...)
//To make the seeds get some REAL random data, such as mouse move, keystrike delays, interupts, or anythings else.
#if SEED_LEN > 0 //Generate the seeds only if SEED_LEN > 0
char *Seedp, *Seedq, *Seedx; //Pointers to the seeds
srand((unsigned int)time(NULL)); //Seed the bad ass generator
Seedp = new char[SEED_LEN]; //Initialize all seeds pointers to char of SEED_LEN
Seedq = new char[SEED_LEN];
Seedx = new char[SEED_LEN];
Rnd(Seedp, SEED_LEN); //Fill the seed with some garbage
Rnd(Seedq, SEED_LEN); //I repeat, do NOT use this in you application
Rnd(Seedx, SEED_LEN); //You will shoot your own foot
#endif //SEED_LEN > 0
pCBBS BBS; //BBS is a pointer to the instance of the class CBBS
BBS = new CBBS; //New instance of CBBS
//Two ways for initialise the generator, provide seeds or no seeds.
//Remember, providing two time the same seeds generate the same pseudorandom data.
//If you don't provide seeds, the generator get somme from OS, they are BAD so do this only if you need weak random.
//Note: All seeds must be of the same length
printf("Initializing the generator... ");
#if SEED_LEN > 0 //Seeds is provided
BBS->Init(Seedp, Seedq, Seedx, SEED_LEN); //Initialize the generator with the seeds
#else //Use the built-in seed generator
BBS->Init(); //Initialize the generator with no seeds. NOT recomended
#endif //SEED_LEN > 0
printf("OK\n\n");
//Print some informations, not very usefull but cool, isn't it?
printf("p is %u decimal digits length and %u bit length\n", BBS->GetpLen(10), BBS->GetpLen(2));
printf("q is %u decimal digits length and %u bit length\n", BBS->GetqLen(10), BBS->GetqLen(2));
printf("M is %u decimal digits length and %u bit length\n", BBS->GetMLen(10), BBS->GetMLen(2));
printf("X is %u decimal digits length and %u bit length\n", BBS->GetXLen(10), BBS->GetXLen(2));
printf("With these numbers we can take a max of %u bits from each X\n\n", BBS->GetMaxBit());
//Now our generator is ready
//We will output some random data in varouis form.
bool bit;
unsigned char byte;
unsigned int rndint;
char *rndhex;
unsigned char *rndbin;
bit = BBS->GetRndBit(); //Get a unique random bit
printf("A random bit: %i\n", bit?1:0);
byte = BBS->GetRndByte(); //Get a random byte (8 bit)
printf("A random byte: %02x\n", byte);
rndint = BBS->GetRndInt(); //Get a random int (32 bit most of time)
printf("A random int: %u\n", rndint);
rndhex = new char[33];
BBS->GetRndBinHex(rndhex, 33); //Get a random binary data printed in hex
printf("A random hex string: %s\n", rndhex);//32 hex digits = 16 byte = 128 bit)
delete [] rndhex;
rndbin = new unsigned char[1024];
BBS->GetRndBin(rndbin, 1024); //Get a random binary data. Dont print...
printf("A random binary data of length: %u (not printed obviously)\n", 1024);
delete [] rndbin;
bit = BBS->GetRndBitAt(1); //Get the first bit we output from generator
printf("The random bit was: %i\n", bit?1:0);
bit = BBS->GetRndBitAt(rndint); //Get the bit at the position of the int we generated in rndint
printf("The last bit at X%u is: %i\n", rndint, bit?1:0);
unsigned int in, i;
//A little play around with the user and GetRndBitAt
printf("\nEnter a number to get all bits generated from Xn\n");
printf("Just enter 0 to exit\n\n");
do
{
printf("Enter a number: ");
scanf("%u", &in);
if(in!=0)
{
//Output the max bit we can take trom Xn
//In the order the generator output it.
printf("The %u bits at X%u is: ", BBS->GetMaxBit(), in);
for(i=0;i<BBS->GetMaxBit();i++)
{
bit = BBS->GetRndBitAt(in, i); //Get The bit at offset i from Xin
printf("%i", bit?1:0);
}
printf("\n");
}
}
while(in!=0);
#if SEED_LEN > 0 //Seeds has been provided, so now we delete it
delete [] Seedp; //We can delete the seeds
delete [] Seedq;
delete [] Seedx;
#endif //SEED_LEN > 0
return 0;
}