i want use authenticated encryption in code. according jdk, seems java 7 support aes/gcm/nopadding.
however, got following error following code.
error:
java.security.nosuchalgorithmexception: cannot find provider supporting aes/gcm/nopadding @ javax.crypto.cipher.getinstance(cipher.java:524) @ cipherservice.main(cipherservice.java:25) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.intellij.rt.execution.application.appmain.main(appmain.java:120)
code:
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);
in short, cannot (as brett pyke said). because sunjce crypto provider (and oracle) not include aes/gcm implementation. thankfully, included @ least gcmparameterspec.
your 2 options (afaik) crypto providers bouncycastle , iaik.
edit/update: oracle jdk-8 seems provide working implementation of aes/gcm.