找不到任何支持 AES/GCM/NoPadding 的提供商

Cannot find any provider supporting AES/GCM/NoPadding

我们正在尝试在 java 7 中支持 AES/GCM/NoPadding 的加密。

找不到任何支持 AES/GCM/NoPadding

的提供商

生成密码实例的代码示例如下。

SecretKeySpec eks = new SecretKeySpec(k, "AES");
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, eks, new GCMParameterSpec(128, iv));

Java 7 SE (exception for Solaris) 不支持此密码。

public static void main(String[] args) throws Exception {
    for (Provider provider : Security.getProviders()) {
        for (Map.Entry<Object, Object> entry : provider.entrySet()) {
            if (((String) entry.getValue()).contains("GCM")) {
                System.out.printf("key: [%s]  value: [%s]%n",
                    entry.getKey(),
                    entry.getValue());
            }
        }
    }
}

在这种情况下,您可能会考虑 Bouncy Castle 作为服务提供商。

使用 Bouncycastle 的小片段。

  1. http://www.bouncycastle.org/latest_releases.html
  2. 下载 bcprov-jdk15on-154.jar
  3. 在您的代码中注册服务提供商

    Security.addProvider(new BouncyCastleProvider());
    
  4. 那么你就可以使用密码了(参数"BC"指定使用Bounce Castle作为服务提供者,如果没有其他提供相同密码的提供者可以省略)

    Cipher c = Cipher.getInstance("AES/GCM/NOPADDING", "BC");
    

Java 8 支持开箱即用的密码 with

Cipher c = Cipher.getInstance("AES/GCM/NOPADDING");