How to solve javax.crypto.IllegalBlockSizeException: data not block size aligned

前端 未结 2 2256
滥情空心
滥情空心 2020-12-16 05:05

I am doing an assignment about use blowfish to do encryption & decryption in java.

I had added a provider, and get instance \"Blowfish/ECB/NoPadding\", but I sti

2条回答
  •  青春惊慌失措
    2020-12-16 05:48

    You've explicitly asked for a provider that doesn't do padding (notice the NoPadding in the instance name). Consequently, your input will not be padded.

    Furthermore, this is a block cipher, so the input must be a multiple of the block length. With the crypto provider not doing padding, you need to ensure yourself that your input is a multiple of the block size, else encryption/decryption will not be possible and you'll get this error.

    Thus you have two options in order to solve this:

    1. Pad the input yourself to a multiple of the block size.
    2. Choose a provider that does padding (e.g. PKCS5Padding), if you don't want to do it manually. Given the nature of your question, this is likely to be the best option.

提交回复
热议问题