Java 中的 RNGCryptoServiceProvider

Al *_*ath 4 c# java random

我正在将一些 C# 代码转换为 Java。我找不到相当于RNGCryptoServiceProvider. 我该怎么做呢?

private static String GetRandomSalt()
{
    RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
    byte[] salt = new byte[32]; //256 bits
    random.GetBytes(salt);
    ...
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 6

To expand on my comment:

Java's SecureRandom is the equivalent you're looking for.

SecureRandom random = new SecureRandom();
byte[] salt = new byte[32];
random.nextBytes(salt);
Run Code Online (Sandbox Code Playgroud)

The documentation details some other ways to get an instance of SecureRandom, depending on your requirements.