title: java.security.InvalidAlgorithmParameterException: Unsupported IV length: 10 bytes. Only 12 bytes long IV supported
body: <p>Hello Everyone I am working on Android Project and i have to save JWT token in my sharedprefrences so for that i use Android KeyStore System to encrypt the data and store it i found some code from Internet and i rewrite it its works fine here it is</p>
  <pre><code>  fun Key_Store(Apikey: String){
     val keyGenerator : KeyGenerator = KeyGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_AES,
          &quot;AndroidKeyStore&quot;
      )
  
      val keyGenParameterSpec = KeyGenParameterSpec.Builder(
          &quot;alias&quot;,
          KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
      )
          .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
          .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
          .build()
  
  
      keyGenerator.init(keyGenParameterSpec);
      val  secretKey: SecretKey = keyGenerator.generateKey()
  
      val  cipher:Cipher = Cipher.getInstance(&quot;AES/GCM/NoPadding&quot;)
      cipher.init(Cipher.ENCRYPT_MODE, secretKey)
  
      val iv = cipher.getIV();
      val encryption = cipher.doFinal(Apikey.toByteArray())
      Log.e(&quot;Real&quot;, &quot;is &quot; + Apikey)
      Log.e(&quot;Encription&quot;, &quot;is &quot; + encryption)
  
  
      //decrypting
      val keyStore = KeyStore.getInstance(&quot;AndroidKeyStore&quot;);
      keyStore.load(null);
  
      val secretKeyEntry = keyStore
          .getEntry(&quot;alias&quot;, null) as KeyStore.SecretKeyEntry
  
      val secretKey2 = secretKeyEntry.secretKey
  
      val cipher2 = Cipher.getInstance(&quot;AES/GCM/NoPadding&quot;)
  
      val spec = GCMParameterSpec(128, iv)
      cipher2.init(Cipher.DECRYPT_MODE, secretKey2, spec)
  
      val decodedData = cipher2.doFinal(encryption)
      val unencryptedString = String(decodedData)
      Log.e(&quot;UnEncription&quot;, &quot;is &quot; + unencryptedString)
  }
  </code></pre>
  <p>this works fine its encript the data and also decryopt it but when i devide this function in two functions then its give error
  <strong>java.security.InvalidAlgorithmParameterException: Unsupported IV length: 10 bytes. Only 12 bytes long IV supported</strong>
  and not works for me here it is</p>
  <pre><code>  private fun encryptedKey (KEY_NAME: String, value: String){
      val keyGenerator : KeyGenerator = KeyGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_AES,
          &quot;AndroidKeyStore&quot;
      )
  
       val keyGenParameterSpec = KeyGenParameterSpec.Builder(
          &quot;pryze&quot;,
          KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
      )
          .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
          .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
          .build()
  
  
      keyGenerator.init(keyGenParameterSpec);
      val  secretKey: SecretKey = keyGenerator.generateKey()
  
      val  cipher: Cipher = Cipher.getInstance(&quot;AES/GCM/NoPadding&quot;)
      cipher.init(Cipher.ENCRYPT_MODE, secretKey)
  
      val iv = cipher.getIV();
      val encryption = cipher.doFinal(value.toByteArray())
  
      val editor: SharedPreferences.Editor = sharedPrefs.edit()
      editor.putString(KEY_NAME+&quot;EN&quot;, encryption.toString())
      editor.putString(KEY_NAME+&quot;IV&quot;, iv.toString())
      editor.commit()
      Log.e(&quot;IV Top&quot;, &quot;is &quot; + iv)
  
  }
  
  private fun decryptKey (KEY_NAME: String): String?{
  
      val keyStore = KeyStore.getInstance(&quot;AndroidKeyStore&quot;);
      keyStore.load(null);
  
      val secretKeyEntry = keyStore
          .getEntry(&quot;pryze&quot;, null) as KeyStore.SecretKeyEntry
  
      val secretKey2 = secretKeyEntry.secretKey
  
      val cipher2 = Cipher.getInstance(&quot;AES/GCM/NoPadding&quot;)
  
      val spec = GCMParameterSpec(128, sharedPrefs.getString(KEY_NAME+&quot;IV&quot;, null)?.toByteArray())
      cipher2.init(Cipher.DECRYPT_MODE, secretKey2, spec)
  
  
      val decodedData = cipher2.doFinal(sharedPrefs.getString(KEY_NAME+&quot;EN&quot;, null)?.toByteArray())
      val unencryptedString = String(decodedData)
      Log.e(&quot;UnEncription&quot;, &quot;is &quot; + unencryptedString)
  
      return unencryptedString
  }
  </code></pre>