Need solution for wrong IV length in AES

后端 未结 5 1118
南旧
南旧 2020-12-08 18:17

I\'m trying to implement AES in Java and this is the code I use:

 byte[] sessionKey = {00000000000000000000000000000000};
 byte[] iv = {000000000000000000000         


        
5条回答
  •  醉酒成梦
    2020-12-08 18:42

    The reason why I had this problem was because I was reading the IV as a String, and converting it to a byte array the wrong way.

    Right way:

    Hex.decodeHex(initializationVector.toCharArray()
    

    using org.apache.commons.codec.binary.Hex

    Wrong way:

    initializationVector.getBytes()

    The reason this was wrong, is because when you call getBytes(), it just takes all bits that represent the string and cuts them into bytes. So 0 ends up being written as the bits that make up the index of 0 in the Unicode table, which is not 0 but 30, and which would be written on 2 bytes.

    Conversely, what you want here is actually that 0 be represented as the byte 00000000.

提交回复
热议问题