Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Cipher AES/GCM/NoPadding "iv==null"

Trying to encrypt and decrypt some data, on Marshmallow. Encryption works well, but when trying to decrypt, I'm getting a RuntimeException, saying "iv == null".

Basically, cipher.getIV() and cipher.getParameters() returns null. Am I missing something?

  private static final String ALGORITHM_NAME = "AES/GCM/NoPadding";
  private static final int IV_SIZE = 128;
  private static final int ALGORITHM_SIZE = 256;

  private static final String KEYSTORE_PROVIDER = "AndroidKeyStore";
  private static final String SYMMETRIC_ALIAS = "secret_key";

  private static SecretKey getSymmetricKey()
      throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException,
      CertificateException, KeyStoreException, UnrecoverableEntryException {

    KeyStore ks = KeyStore.getInstance(KEYSTORE_PROVIDER);
    ks.load(null);

    return (SecretKey) ks.getKey(SYMMETRIC_ALIAS, null);
  }

  private static SecretKey createSecretKey()
      throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {

    KeyGenerator keyGenerator = KeyGenerator.getInstance(
        KeyProperties.KEY_ALGORITHM_AES, KEYSTORE_PROVIDER);

    keyGenerator.init(new KeyGenParameterSpec.Builder(SYMMETRIC_ALIAS,
        KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
        .setKeySize(ALGORITHM_SIZE)
        .setRandomizedEncryptionRequired(true)
        .setUserAuthenticationRequired(false)
        .build());

    return keyGenerator.generateKey();
  }

  public static byte[] encrypt(byte[] data)
      throws CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableEntryException,
      KeyStoreException, NoSuchProviderException, InvalidAlgorithmParameterException, NoSuchPaddingException,
      InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

    SecretKey secretKey = getSymmetricKey();
    if (secretKey == null) {
      secretKey = createSecretKey();
    }

    Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
  }

  public static byte[] decrypt(byte[] data)
      throws CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableEntryException,
      KeyStoreException, NoSuchProviderException, InvalidAlgorithmParameterException, NoSuchPaddingException,
      InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

    SecretKey secretKey = getSymmetricKey();

    Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
    GCMParameterSpec spec = new GCMParameterSpec(IV_SIZE, cipher.getIV());
    cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
    return cipher.doFinal(data.getBytes());
  }
like image 436
marius bardan Avatar asked Apr 16 '26 06:04

marius bardan


1 Answers

You must to provide a non-null IV to AES/GCM/NoPadding Cipher.init when decrypting. However, your code provides a null IV, obtained by invoking Cipher.getIV() on an uninitialized Cipher instance.

The fix is to save the IV used when encrypting, and then use that same saved IV when decrypting.

like image 104
Alex Klyubin Avatar answered Apr 17 '26 20:04

Alex Klyubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!