Java7中的身份验证加密

Java7中的身份验证加密,java,encryption-symmetric,Java,Encryption Symmetric,我想在我的代码中使用经过身份验证的加密。根据JDK的说法,Java7似乎支持AES/GCM/NoPadding 但是,我用下面的代码得到了下面的错误 错误: java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/GCM/NoPadding at javax.crypto.Cipher.getInstance(Cipher.java:524) at CipherService.

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

但是,我用下面的代码得到了下面的错误

错误:

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)
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);
代码:

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

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

简而言之,您不能(正如Brett Pyke所说)。因为SunJCE加密提供程序(和Oracle)不包括AES/GCM实现。谢天谢地,它们至少包括了GCMParameterSpec

您仅有的两个选项(AFAIK)是加密提供程序和


编辑/更新:Oracle JDK-8似乎提供了AES/GCM的有效实现。

谢谢!但是如何使用内置的Sun/Oracle提供的?有什么想法吗?不过有一个警告:Oracle JDK-8实现的AES/GCM比BouncyCastle纯Java AES/GCM实现(在Mac OS X Intel Core i5和i7上测试)慢整整一个数量级。然而,使用AES/CBC时,Oracle代码的速度大约是BouncyCastle的三倍。