This discussion is archived
11 Replies Latest reply: Oct 26, 2008 3:17 PM by 807589 RSS

byte array size changes

807589 Newbie
Currently Being Moderated
For some reason the size of my byte arrays change, and it is causing problems using an AES Cipher.

The length of the byte array is 16 when I first encrypt text, but when I try and decrypt it, it tells me the length is 29. Then I have an IllegalBlockSizeException.
Can someone explain how to overcome this problem?
I used the UTF-8 encoding on Ubuntu 8.04
  • 1. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    TamalinJava wrote:
    For some reason the size of my byte arrays change, and it is causing problems using an AES Cipher.
    A byte[] has a fixed size, you cannot change it.
    The length of the byte array is 16 when I first encrypt text, but when I try and decrypt it, it tells me the length is 29. Then I have an IllegalBlockSizeException.
    I used the UTF-8 encoding on Ubuntu 8.04
    This is your problem. It translates values >= 128 to be two characters.
    Can someone explain how to overcome this problem?
    Don't try to write binary data as if it were text. (Write it as binary data)
  • 2. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    I had an original array of bytes with a length of 16, I need to display the bytes in a JTextArea, be able to copy it to a clipboard or file, close the program, open the program, paste the text back into the JTextArea, and get back the 16 byte array.

    I had used the String.getBytes() method, but that had returned a 29 byte array. I had constructed a String with the constructor: String string = new String(bytes[], "utf-8");, then writtern it to a JTextArea.
  • 3. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    For previous posts [http://forums.sun.com/thread.jspa?threadID=5342820&messageID=10481037#10481037]
  • 4. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    Yes, but only the password is put through the MD5. Not the ciphertext. So why is the ciphertext length different? I though that the MD5 converted something to a 16 byte fingerprint...
    (Which is a multiple of 16)
  • 5. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    but if 16-byte binary values are converted to UTF-8 you get about 24-bytes, possibly more..
  • 6. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    Ok, that makes sense, but how do I avoid this problem?
  • 7. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    I just removed all utf-8 references, but I still have that problem even though I am not coverting to UTF-8 anymore. I have discovered that it happens as soon as I covert the bytes to a string.
    byte[] ciphertextBytes = cipher.doFinal(plaintext);  //creates a 16 byte array.
    String ciphertext = new String(ciphertextBytes); //Creates a new string containing the ciphertext bytes.
    
    byte[] newCiphertextBytes = ciphertext.getBytes();  //Creates a 28 byte array!!! (Even though the String 'ciphertext was created with 16)
    I hope this helps...
  • 8. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    This is the same problem. Converting bytes to text attempts to decode the bytes as e.g. the reverse of UTF-8.
    You shouldn't be converting binary data into text or from text.
  • 9. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    Oh, ok. Thank you for the help. Is there any way I can get around that, or is that just a hard fact of life?
    Thank you.
  • 10. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    Don't use text to hold binary data. text is for letters and words. Encrypted data is binary data and is not intended to readable or in encoding in a given character set.
  • 11. Re: byte array size changes
    807589 Newbie
    Currently Being Moderated
    Ok, I will try and fix up my code or something... Thanks!