首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 7中的认证加密

Java 7中的认证加密
EN

Stack Overflow用户
提问于 2013-08-23 06:31:29
回答 1查看 4.4K关注 0票数 2

我想在我的代码中使用经过验证的加密。根据JDK的说法,java 7似乎支持AES/GCM/NoPadding

但是,下面的代码出现了以下错误。

错误:

代码语言:javascript
复制
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/GCM/NoPadding
    at javax.crypto.Cipher.getInstance(Cipher.java:524)
    at CipherService.main(CipherService.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

代码:

代码语言:javascript
复制
Cipher c = Cipher.getInstance ("AES/GCM/NoPadding");
final int blockSize = c.getBlockSize();
final byte[] ivData = new byte[blockSize];
final SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
rnd.nextBytes(ivData);
GCMParameterSpec params = new GCMParameterSpec(blockSize * Byte.SIZE, ivData);
SecureRandom sr = new SecureRandom();
byte[] aesKey = new byte[KEY_SIZE];
byte[] ciphertext;
byte[] head = "Head".getBytes();
byte[] data = "Data".getBytes();
sr.nextBytes(aesKey);
SecretKeySpec sks = new SecretKeySpec(aesKey, "AES");
c.init(Cipher.ENCRYPT_MODE, sks, params);
c.updateAAD(head);
ciphertext = c.doFinal(data);
EN

回答 1

Stack Overflow用户

发布于 2013-08-23 06:36:56

您需要使用加密提供程序(如BouncyCastle )。一旦您在上下文中注册了它,那么您应该能够使用任何支持的算法。您的另一个选择是使用内置的Sun/Oracle提供的程序,但这违反了Java的要点,即能够在任何JVM上运行应用程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18396280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档