Listing the Available Cipher Algorithms – 列出可用的密码算法

最后修改: 2020年 9月 24日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll learn about the Cipher class in Java. Then, we’ll see how to list the available cipher algorithms and their providers.

在这个快速教程中,我们将了解Java中的Cipher类。然后,我们将看到如何列出可用的密码算法和它们的提供者。

2. The Cipher Class

2.密码类

The Cipher class, which is located in the javax.crypto package is the core of the Java Cryptography Extension (JCE) framework. This framework provides a set of cryptographic ciphers for data encryption, decryption, and hashing.

位于javax.crypto包中的Cipher类是Java Cryptography Extension(JCE)框架的核心。这个框架提供了一套用于数据加密、解密和散列的加密密码器。

3. Listing the Cipher Algorithms

3.列出密码算法

We can instantiate a cipher object by calling the Cipher.getInstance() static method with the name of the requested transformation as the argument:

我们可以通过调用Cipher.getInstance()静态方法来实例化一个密码对象,并将要求的转换名称作为参数:

Cipher cipher = Cipher.getInstance("AES");

There are some cases we need to get the list of available cipher algorithms and their providers. For instance, we want to check if a specific algorithm is available based on the libraries present in the classpath.

在某些情况下,我们需要获得可用的密码算法及其提供者的列表。例如,我们想根据classpath中存在的库来检查一种特定的算法是否可用。

First, we need to get the list of registered providers using the Security.getProviders() method. Then, calling the getServices() method on the Provider object will return an unmodifiable set of all services supported by this Provider:

首先,我们需要使用Security.getProviders()方法获取已注册的供应商列表然后,调用getServices()方法在Provider对象上将返回一个由该Provider支持的所有服务的不可修改的集合:

for (Provider provider : Security.getProviders()) {
    for (Provider.Service service : provider.getServices()) {
        String algorithm = service.getAlgorithm();
        // ...
    }
}

The list of available algorithms :

可用的算法列表 :

SHA3-224
NONEwithDSA
DSA
JavaLoginConfig
DSA
SHA3-384
SHA3-256
SHA1withDSA
...

However, not all of the listed algorithms are supported as a transformation by Cipher.getInstance() static method. For example, instantiating a cipher object with SHA3-224, which is a hashing algorithm, will throw a NoSuchAlgorithmException:

然而,并不是所有列出的算法都Cipher.getInstance()静态方法支持作为一种转化。例如,用SHA3-224实例化一个密码对象,这是一个散列算法,将抛出一个NoSuchAlgorithmException:

Cipher cipher = Cipher.getInstance("SHA3-224");

Let’s take a look at the runtime exception message:

让我们来看看运行时异常信息。

java.security.NoSuchAlgorithmException: Cannot find any provider supporting SHA3-224

So, we need to filter the list and keep services with the Cipher type. We can use a Java stream to filter and collect the list of the names of the compatible algorithms:

因此,我们需要过滤该列表并保留具有Cipher类型的服务。我们可以使用一个Java流来过滤和收集兼容算法的名称列表。

List<String> algorithms = Arrays.stream(Security.getProviders())
  .flatMap(provider -> provider.getServices().stream())
  .filter(service -> "Cipher".equals(service.getType()))
  .map(Provider.Service::getAlgorithm)
  .collect(Collectors.toList());
// ...

The result will be something like:

结果将是这样的。

AES_192/CBC/NoPadding
AES_192/OFB/NoPadding
AES_192/CFB/NoPadding
AESWrap_192
PBEWithHmacSHA224AndAES_256
AES_192/ECB/NoPadding
AES_192/GCM/NoPadding
ChaCha20-Poly1305
PBEWithHmacSHA384AndAES_128
AES_128/ECB/NoPadding
AES_128/OFB/NoPadding
AES_128/CBC/NoPadding
...

4. Conclusion

4.总结

In this tutorial, we first learned about the Cipher class. Then, we learned how to list the available cipher algorithms.

在本教程中,我们首先了解了Cipher类。然后,我们学习了如何列出可用的密码算法。

As usual, all the examples are available over on GitHub.

像往常一样,所有的例子都可以在GitHub上找到