Note that there are some explanatory texts on larger screens.

plurals
  1. POAES 128bit encryption. Inconsistent byte reads from stored SecretKey
    primarykey
    data
    text
    <p>I'm trying to set up the following. I have a password to encrypt, I generate a key and store the key to file, I use this stored key to encrypt a string 'password' and it returns 'OePTgm4jy8vbOO8Kf+XTaA==' for example, I later pass this string into a decrypter which loads the stored key from file and converts it back to password in a java class file.</p> <p>I have this working, but only some of the time. The problem appears to be when I read the key file. When I write the key using:</p> <pre><code>System.out.println("write:" + key.getEncoded().length); </code></pre> <p>I get "write:16"</p> <p>when I read this again I get either read:16 or read:17 (most usually read:17).</p> <p>The code I'm using to write the SecretKey is:</p> <pre><code> KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); // 128 default; 192 and 256 also possible SecretKey key = keyGenerator.generateKey(); return key; </code></pre> <p>the to read it:</p> <pre><code> String hex = new String(readFileToByteArray(file)); byte[] encoded = new BigInteger(hex, 16).toByteArray(); SecretKey key = new SecretKeySpec(encoded, "AES"); System.out.println("read:" + key.getEncoded().length); return key; </code></pre> <p>When it fails the error is caught here:</p> <pre><code> byte[] raw = key.getEncoded(); System.out.println(key.getEncoded().length); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); try { ecipher = Cipher.getInstance("AES"); dcipher = Cipher.getInstance("AES"); ecipher.init(Cipher.ENCRYPT_MODE, skeySpec); dcipher.init(Cipher.DECRYPT_MODE, skeySpec); } catch (javax.crypto.NoSuchPaddingException e) { e.printStackTrace(); } catch (java.security.NoSuchAlgorithmException e) { e.printStackTrace(); **} catch (java.security.InvalidKeyException e) { e.printStackTrace();** } read:17 17 java.security.InvalidKeyException: Invalid AES key length: 17 bytes at com.ibm.crypto.provider.AESCipher.engineGetKeySize(Unknown Source) at javax.crypto.Cipher.b(Unknown Source) at javax.crypto.Cipher.a(Unknown Source) at javax.crypto.Cipher.a(Unknown Source) at javax.crypto.Cipher.a(Unknown Source) at javax.crypto.Cipher.init(Unknown Source) at javax.crypto.Cipher.init(Unknown Source) at au.edu.uts.itd.encryption.manager.AESEncrypter.&lt;init&gt;(AESEncrypter.java:21) at au.edu.uts.itd.encryption.util.Encrypt.main(Encrypt.java:26) Exception in thread "main" java.lang.IllegalStateException: Cipher not initialized at javax.crypto.Cipher.c(Unknown Source) at javax.crypto.Cipher.doFinal(Unknown Source) at au.edu.uts.itd.encryption.manager.AESEncrypter.encrypt(AESEncrypter.java:39) at au.edu.uts.itd.encryption.util.Encrypt.main(Encrypt.java:27) </code></pre> <p>So the fact I writing 16 and retrieving 17 bytes is the problem I believe.</p> <p>Can someone advise on how to ensure the read is 16 bytes as it was written?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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