1

I need to generate a key to my algorithm with a seed (byte array). This code doesn't work, because it's give 2 different results on different platforms:

byte seed [] = {50,30,...};
byte result [] = new byte [20];
SecureRandom rand = new SecureRandom();
rand.setSeed(seed);
rand.nextBytes(result);

I need to get the same results on different platforms.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
ilja
  • 79
  • 8

2 Answers2

0

Use the SHA1PRNG algorithm istead of NativePRNG.

SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
rand.setSeed(seed);
rand.nextBytes(result);
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

Always specify the exact PRNG and provider that you wish to use. If you just use the default PRNG, you may end up with different PRNGs on different installations of your application that may need to be called differently in order to work properly. Using the following code to get a PRNG instance is appropriate:

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");

See https://www.cigital.com/justice-league-blog/2009/08/14/proper-use-of-javas-securerandom/

Also see this: Android 4.2 broke my encrypt/decrypt code and the provided solutions don't work

Which suggests this might work for android:

SecureRandom secrand = SecureRandom.getInstance("SHA1PRNG", "Crypto");
Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203