📌  相关文章
📜  Java中的 SecureRandom getAlgorithm() 方法及示例

📅  最后修改于: 2022-05-13 01:55:37.640000             🧑  作者: Mango

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

Java.security.SecureRandom类的getAlgorithm()方法用于返回此SecureRandom 对象实现的算法的名称。
句法:

public String getAlgorithm()

返回值:该方法返回算法名称,如果无法确定算法名称,则返回未知。
以下是说明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);
        }
    }
}


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);
        }
    }
}


输出:
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