1. 程式人生 > >SecretKeyCreateUtil工具類,採用安全的生成隨機數方法(SecureRandom),此處為10位

SecretKeyCreateUtil工具類,採用安全的生成隨機數方法(SecureRandom),此處為10位

public class SecretKeyCreateUtil {
/**
* 產生金鑰資訊
* 採用安全的生成隨機數方法(SecureRandom)
* @throws IOException
* @throws NoSuchAlgorithmException 
*/
public static String createSecretKey() throws IOException, NoSuchAlgorithmException {
final String base = "qwertyuiopasdfghjklzxcvbnm1234567890";
SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < 10; i++) {
int number = sRandom.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
}