我想在我的代码中使用经过身份验证的加密。根据 JDK,似乎java 7 支持 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);