title: Pycrypto AES GCM encryption and Java decryption
body: <p>I'm using Pycryptodome (a PyCrypto fork) to create AES-GCM ciphertexts. I use the following Python code to encrypt:</p>
  
  <pre><code>cek = os.urandom(16)
  nonce = os.urandom(12)
  
  cipher = AES.new(cek, AES.MODE_GCM, nonce=nonce, mac_len=16)
  ciphertext = cipher.encrypt(message)
  </code></pre>
  
  <p>I then pass this to Java to decrypt:</p>
  
  <pre><code>byte[] nonce = new byte[12];
  
  Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
  GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
  SecretKeySpec secretKeySpec = new SecretKeySpec(cek, "AES");
  
  IvParameterSpec ivParameterSpec = new IvParameterSpec(nonce);
  cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmSpec);
  
  byte[] decBytes = mCipher.doFinal(cipherText);
  </code></pre>
  
  <p>However, I get the following error:</p>
  
  <pre><code>Exception in thread "main" javax.crypto.AEADBadTagException: Tag mismatch!
      at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:524)
      at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1023)
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:960)
      at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
      at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
      at javax.crypto.Cipher.doFinal(Cipher.java:2165)
  </code></pre>