Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Expected IV length 0 error in AES 128 Decypt

Posted on November 11, 2022 By

Solution 1 :

For CBC mode you should call

 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

NoPadding options means that apply no padding. This is useful if

  • Your data is always multiple of the AES block size, i.e. 128k
  • You will do your padding, probably developing a new one.

If you are talking about ECB mode, that doesn’t need IV and don’t use ECB. It is insecure. If you really need then call it without an IV.

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

If your Android target matches prefer GCM mode instead of ECB or CBC. That is a modern encryption mode; Authenticated Encryption (with Associated Data). You will get Confidentiality, authentication, and integrity.

Problem :

I know a lot of these questions were asked. but in my case the error is:

java.security.InvalidAlgorithmParameterException: expected IV length of 0

I’m trying AES 128 CBC mode

code:

byte[] iv = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
                            , (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};

        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(encrypted);
        return decryptedBytes;

If I change init vector to something like this as error suggested:

byte[] iv = new byte[]{};

I’m receiving error :

java.security.InvalidAlgorithmParameterException: expected IV length of 16

Comments

Comment posted by Fajar Khan

Changed as suggested but now throwing this error: javax.crypto.BadPaddingException: EVP_CipherFinal_ex

Comment posted by Fajar Khan

Also my data will be always 128 bit, 128-bit key and IV

Comment posted by Fajar Khan

encyption is done by NFC based card reader (I’m using this for card auth). I will receive 16-byte encrypted data with the key.

READ  [FIXED] android - Text is placed on the screen but it scroll
Powered by Inline Related Posts

Comment posted by Fajar Khan

As per doc ‘encrypted/decrypted using AES 128 CBC with an IV with value 00h’

Comment posted by Fajar Khan

I have NDA signed for the doc. I cant share link or its copy. but it doesn’t say nopadding or AES/CBC/nopadding. what I posted before was the full text mentioned from doc.

Android Tags:aes, android, caesar-cipher, java

Post navigation

Previous Post: [FIXED] android – how to change notification background color in nougat?
Next Post: [FIXED] android – How to make width ratio equal in XML GridView

Related Posts

[FIXED] android – Possibility to create a log file of activities on a Google Home? Android
[FIXED] Scaling an Image on Android makes the Image render outside the ImageView boundaries Android
[FIXED] android – Write a PCM encoded sounds with silence gaps using MediaCodec and MediaMuxer Android
[FIXED] Is there a way to make a drawable or a custom view with a diagonal line in android studio? Android
[FIXED] java – How to clear HttpAuthRequest credentials Android
[FIXED] android – Cannot resolve method getUserAgent() with exoplayer Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme