Java中的SecureRandom getAlgorithm()方法及其示例

这个 getAlgorithm() 方法 JAVA安全安全随机 类用于返回此SecureRandom对象实现的算法的名称。 语法:

null
public String getAlgorithm()

返回值: 此方法返回算法名称,如果无法确定算法名称,则返回unknown。 下面是一些例子来说明 getAlgorithm() 方法: 例1:

JAVA

// Java program to demonstrate
// getAlgorithm() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of SecureRandom
SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG" );
// getting the Algorithm
// by using method getAlgorithm()
String algo = sr.getAlgorithm();
// printing the string algo
System.out.println( "Algorithm: " + algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

Algorithm: SHA1PRNG

例2:

JAVA

// Java program to demonstrate
// getAlgorithm() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of SecureRandom
SecureRandom sr = SecureRandom.getInstance( "TAJMAHAL" );
// getting the Algorithm
// by using method getAlgorithm()
String algo = sr.getAlgorithm();
// printing the string algo
System.out.println(algo);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL SecureRandom not available

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享