How to generate a secure random alphanumeric string in Java efficiently?

后端 未结 9 1431
半阙折子戏
半阙折子戏 2021-01-01 12:24

How do you generate a secure random (or pseudo-random) alphanumeric string in Java efficiently?

相关标签:
9条回答
  • 2021-01-01 12:58

    Generate a public key for an open-key encryption algorithm and convert the byte sequence to string via Base64 algorithm.

    0 讨论(0)
  • 2021-01-01 13:02

    If you use Apache Commons Lang, the easiest way is

    RandomStringUtils.random(20, 0, 0, true, true, null, new SecureRandom());
    
    0 讨论(0)
  • 2021-01-01 13:02

    http://download.oracle.com/javase/6/docs/api/java/security/SecureRandom.html

    From the Javadoc:

    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);
    
    0 讨论(0)
提交回复
热议问题