You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Insecuere_Randomness.java 949 B

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package Insecuere_Randomness;
  2. import java.security.NoSuchAlgorithmException;
  3. import java.security.SecureRandom;
  4. import java.util.Random;
  5. import java.util.logging.Logger;
  6. public class Insecuere_Randomness
  7. {
  8. static final Logger log = Logger.getLogger("logger");
  9. public void bad()
  10. {
  11. Random rand = new Random();
  12. /* FLAW: seed is static, making the numbers always occur in the same sequence */
  13. rand.setSeed(System.currentTimeMillis());
  14. log.info("Random int: " + rand.nextInt(21)); // bad 不安全的随机数
  15. }
  16. public void good()
  17. {
  18. /* FIX: use SecureRandom to be cryptographically secure */
  19. SecureRandom securerand;
  20. try {
  21. securerand = SecureRandom.getInstance("SHA1PRNG");
  22. log.info("Random int: " + securerand.nextInt(21)); // good 不安全的随机数
  23. } catch (NoSuchAlgorithmException e) {
  24. log.info("NoSuchAlgorithmException");
  25. }
  26. }
  27. }

No Description

Contributors (1)