title: Difference between IvParameterSpec and GCMParameterSpec with AES/GCM/NoPadding
body: <p>I am using <code>AES/GCM/NoPadding</code> algorithm to encrypt some data on Android (API 19 and onwards) and then later decrypt it back.</p>
  
  <p>The key size I use is 32 bytes and is provided to me</p>
  
  <p>In addition to the encryption, <strong>I also want to know when I try to decrypt and use a wrong key</strong>. Which is why I prefer to use GCM as my mode to get the benefits of verifying integrity (I believe its safe to assume whether the ciphertext or the key whichever is faulty would result in a bad decrypt exception rather than garbled text)</p>
  
  <p>The problem I face is that on Android API 19 using the algorithm above and initializing the cipher with <code>GCMParameterSpec</code> I get a <code>NoSuchAlgorithmException</code>, I do not specify any provider myself allowing Android to pick one for me which can support my algorithm. On 21+ the Algorithm is available. 
  This is how I am initializing(similar for decryption), the entire class is posted at the end of this post.</p>
  
  <pre><code>cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));
  </code></pre>
  
  <p><strong>However, if I use <code>IvParameterSpec(iv)</code> as my <code>AlgorithmParameters</code> instead of <code>GCMParameterSpec</code> then the code works fine.</strong> </p>
  
  <p>So what happens by changing these parameters? Do I still get all the same benefits of GCM? </p>
  
  <p>Because the exceptions thrown are different when attempting to use a wrong key. On API 19 <code>BadPaddingException</code> is thrown when <code>IvParameterSpec</code> is used, on API 21+ <code>AEADBADTagException</code> is thrown when <code>GCMParameterSpec</code> is used.</p>
  
  <p>Is it correct and secure to use just the <code>IvParameterSpec</code> through all the Android API levels and verify the integrity through <code>BadPaddingException</code>? I do not want to have different implementations for different platforms so I would want to use one only.</p>
  
  <p>Also, on API 21+, if I encrypt using <code>GCMParameterSpec</code> and then later use <code>IvParameterSpec</code> to decrypt it decrypts fine! and the same vice versa. How is that working?</p>
  
  <p>If the above is not possible on API 19 then what are my possible options to use as an encryption algorithm and a strategy to use(<code>AES/CBC/PKCS5Padding</code> with HMAC?) to verify the integrity of the key.</p>
  
  <p>Full class code:</p>
  
  <pre><code>import android.util.Base64;
  
  import java.nio.ByteBuffer;
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  import java.security.SecureRandom;
  import java.util.Arrays;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.GCMParameterSpec;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  final class Encryption {
      private static final String ALGORITHM = "AES/GCM/NoPadding";
      private static final int TAG_LENGTH_BIT = 128;
      private static final int IV_LENGTH_BYTE = 12;
  
      private final SecureRandom secureRandom;
      private Cipher cipher;
      private final Charset charset = StandardCharsets.UTF_8;
  
      public Encryption() {
          secureRandom = new SecureRandom();
      }
  
      public String encrypt(byte[] key, String rawData) throws Exception {
          try {
              byte[] iv = new byte[IV_LENGTH_BYTE];
              secureRandom.nextBytes(iv);
  
              cipher = Cipher.getInstance(ALGORITHM);
              //This is where I switch to IvParameterSpec(iv)
              cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));
  
              byte[] encrypted = cipher.doFinal(rawData.getBytes(charset));
  
              ByteBuffer byteBuffer = ByteBuffer.allocate(1 + iv.length + encrypted.length);
              byteBuffer.put((byte) iv.length);
              byteBuffer.put(iv);
              byteBuffer.put(encrypted);
              return Base64.encodeToString(byteBuffer.array(), Base64.NO_WRAP);
          } catch (Exception e) { //ignore this SO
              throw new Exception(e);
          }
      }
  
  
      public String decrypt(byte[] key, String encryptedData) throws Exception {
          try {
              ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.decode(encryptedData, Base64.NO_WRAP));
  
              int ivLength = byteBuffer.get();
              byte[] iv = new byte[ivLength];
              byteBuffer.get(iv);
              byte[] encrypted = new byte[byteBuffer.remaining()];
              byteBuffer.get(encrypted);
  
              cipher = Cipher.getInstance(ALGORITHM);
              cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));
              byte[] decrypted = cipher.doFinal(encrypted);
  
              //Paranoia
              Arrays.fill(iv, (byte) 0);
              Arrays.fill(rawEncryptionKey, (byte) 0);
              Arrays.fill(encrypted, (byte) 0);
  
              return new String(decrypted, charset);
          } catch (Exception e) { //ignore this SO
              // On API 19 BadPaddingException is thrown when IvParameterSpec is used
              // On API 21+ AEADBADTagException is thrown
              throw new Exception("could not decrypt", e);
          }
      }
  }
  </code></pre>
  
  <p>Also, feel free to suggest improvements to the provided class along with your answer, thanks.</p>