vendredi 25 mai 2018

Repeating values for in a random bytes generator in c++

I have made a random bytes generator for intialization vector of CBC mode AES implementation,

#include <iostream>
#include <random>
#include <climits>
#include <algorithm>
#include <functional>
#include <stdio.h>

using bytes_randomizer =    std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>;

int main()
{
bytes_randomizer br;
char x[3];
uint8_t data[100];
std::generate(std::begin(data), std::end(data), std::ref(br));

for(int i = 0; i < 100; i++)
{
    sprintf(x, "%x", data[i]);
    std::cout << x << "\n";
}
}

But the problem is it gives the same sequence over and over, I found a solution to on Stack which is to use srand() but this seems to work only for rand().

Any solutions to this, also is there a better way to generate nonce for generating an unpredictable Initialization Vector.

Aucun commentaire:

Enregistrer un commentaire