Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Cipher problems. InvalidKeyException?
    text
    copied!<p>I'm trying to create an application for Android that uses encryption to save user information and I cannot figure out what I'm doing wrong. I'm trying to create an instance of an AES cipher but the application keeps on throwing "InvalidKeyExceptions." Consider the following code:</p> <pre><code>public static final byte[] IV = new byte[] { 0x04, 0x08, 0x15, 0x16, 0x23, 0x42, 0x00, 0x00, 0x00, 0x00,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; protected final IvParameterSpec params = new IvParameterSpec(IV); protected Cipher myCipher; public AESEncryptor(String passwd, InputStream source, String destinationFile) { try { myCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Log.d("System.out.println", "Block Size: "+myCipher.getBlockSize()); myCipher.init(Cipher.ENCRYPT_MODE, AESEncryptor.generateSecretKeyFromPassword(passwd),params); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>I get this exception:</p> <blockquote> <p>java.security.InvalidKeyException: initialisation vector must be the same length as block size..</p> </blockquote> <p>The myCipher.init(...) line triggers this exception.</p> <p>I understand what it's saying but according to myCipher.getBlockSize() the IV byte array should hold 16 bytes, and it does, but it doesn't work. I have also tried byte arrays of length 0-128, and nothing in that range works either. </p> <p>Oh also, if I take this code, unaltered, and add it to a regular Java application, I get no errors. Compiling for Android seems to be causing this error.</p> <p>Please help. Thanks, Ryan</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload