集册 Java实例教程 生成安全随机数

生成安全随机数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

624
生成安全随机数

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;


public class Main {/** 来 自 nowjava.com**/

  public void myMethod() {

    try {

      // Create a secure random number generator

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


      // Get 1024 random bits

      byte[] bytes = new byte[1024 / 8];

      sr.nextBytes(bytes);


      // Create two secure number generators with the same seed

      int seedByteCount = 10;

      byte[] seed = sr.generateSeed(seedByteCount);/**来自 nowjava - 时代Java**/


      sr = SecureRandom.getInstance("SHA1PRNG");

      sr.setSeed(seed);

      
展开阅读全文