import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.GCMParameterSpec; public class GCMError { public static void main (String[] args) throws Exception { byte[] plainText = null; byte[] sunFinalCipherText = null; byte[] sunUpdateCipherText = null; byte[] aad = null; Cipher sunFinal = null; Cipher sunUpdate = null; Cipher[] ciphers = new Cipher[2]; String trans = "AES/GCM/NOPADDING"; String sunProvider = "SunJCE"; String cipherName = "AES"; int keySize = 192; int numBytes = 1024; SecretKey secretKey = null; KeyGenerator keygen = null; // Create ciphers sunFinal = Cipher.getInstance(trans, sunProvider); sunUpdate = Cipher.getInstance(trans, sunProvider); ciphers[0] = sunFinal; ciphers[1] = sunUpdate; // Generate a key keygen = KeyGenerator.getInstance(cipherName); keygen.init(keySize); secretKey = keygen.generateKey(); // Generate IV byte[] iv = new byte[sunFinal.getBlockSize()]; for (int i = 0; i < iv.length; i++) { iv[i] = (byte) (i*2); } // Generate some AAD data aad = new byte[100]; for (int i = 0; i < aad.length; i++) { aad[i] = (byte) (i*2); } // Generate some plaintext plainText = new byte[numBytes]; for (int i = 0; i < plainText.length; i++) { plainText[i] = (byte)(i+1); } // Create the paramspec GCMParameterSpec paramSpec = new GCMParameterSpec(sunFinal.getBlockSize() * Byte.SIZE, iv); // Initialize the ciphers; for (Cipher cipher : ciphers) { cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); } // Set up some default data for the ciphertexts sunFinalCipherText = new byte[sunFinal.getOutputSize(numBytes)]; for (int i = 0; i < numBytes; i++) { sunFinalCipherText[i] = (byte) i; } sunUpdateCipherText = sunFinalCipherText.clone(); // Pass the AAD to ciphers for (Cipher cipher : ciphers) { cipher.updateAAD(aad, 0, aad.length); } // Do updates sunUpdate.update(plainText, 0, plainText.length, sunUpdateCipherText); // Do finals sunFinal.doFinal(plainText, 0, plainText.length, sunFinalCipherText); sunUpdate.doFinal(plainText, 0, 0, sunUpdateCipherText); System.out.print("Ciphertexts:"); for (int a=0; a