问题描述
我们正在尝试在 java 7 中进行支持AES/GCM/NoPadding的加密,以免出现异常.
We are trying to do encryption supporting AES/GCM/NoPadding in java 7 getting below exception.
找不到任何支持AES/GCM/NoPadding的提供商
用于生成密码实例的代码示例如下.
Code sample for generating cipher instance is below.
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不支持此密码( Solaris的例外情况).
This cipher is not supported by 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 .
You might have a look at Bouncy Castle as service provider in that case.
使用Bouncycastle的小片段.
Small snippet for using Bouncycastle.
- download
bcprov-jdk15on-154.jar
from http://www.bouncycastle.org/latest_releases.html register the service provider in your code
Security.addProvider(new BouncyCastleProvider());
然后您就可以使用该密码,因为(参数"BC"
指定将Bounce Castle用作服务提供者,如果同一密码没有其他提供者,则可以省略)
then you are able to use the cipher as (the paramter "BC"
specifies to use Bounce Castle as service provider, can be omitted if there is no other provider for the same cipher)
Cipher c = Cipher.getInstance("AES/GCM/NOPADDING", "BC");
Java 8支持开箱即用的密码
Java 8 support the cipher out of the box with
Cipher c = Cipher.getInstance("AES/GCM/NOPADDING");
这篇关于找不到任何支持AES/GCM/NoPadding的提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!