AES加密中的javax.crypto.IllegalBlockSizeException

时间:2023-01-20 18:30:56

I am using AES/CBC/PKCS5Padding padding standard in java and my friend uses PKCS7 standard in c#.NET If My friend encrypt the data using AES and send me the key then I can decrypt it.

我在java中使用AES / CBC / PKCS5Padding填充标准,我的朋友在c#.NET中使用PKCS7标准如果我的朋友使用AES加密数据并将密钥发送给我,那么我可以解密它。

But If my data length increases more than 2920 bytes then if i encrypt the data in c#.NET and decrypt the data in java then my decryption does not work good. It gives me the following error.

但是如果我的数据长度增加超过2920字节,那么如果我加密c#.NET中的数据并解密java中的数据,那么我的解密效果不好。它给了我以下错误。

"javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher"

“javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是16的倍数”

Thanks Bapi

2 个解决方案

#1


You again forgot to flush the buffers which means that the data stream is incomplete.

您再次忘记刷新缓冲区,这意味着数据流不完整。

[EDIT] I don't know about C# but in Java, you must call doFinal() once after all data has arrived. (See the docs).

[编辑]我不知道C#,但在Java中,你必须在所有数据到达后调用doFinal()一次。 (参见文档)。

The source of the problem is that the encryption API needs to know when you're done. It can't tell from the data, you must call a method to say "wrap it up, create the final checksum, whatever, so the receiver can decode it".

问题的根源是加密API需要知道何时完成。它无法从数据中分辨出来,你必须调用一个方法来说“把它包起来,创建最终的校验和,无论如何,所以接收器可以解码它”。

#2


just use cipher.dofinal("Your byte array","offset...put 0","block size...16");

只需使用cipher.dofinal(“你的字节数组”,“偏移...放0”,“块大小...... 16”);

eg : c.dofinal(ByteArray,0,16);

#1


You again forgot to flush the buffers which means that the data stream is incomplete.

您再次忘记刷新缓冲区,这意味着数据流不完整。

[EDIT] I don't know about C# but in Java, you must call doFinal() once after all data has arrived. (See the docs).

[编辑]我不知道C#,但在Java中,你必须在所有数据到达后调用doFinal()一次。 (参见文档)。

The source of the problem is that the encryption API needs to know when you're done. It can't tell from the data, you must call a method to say "wrap it up, create the final checksum, whatever, so the receiver can decode it".

问题的根源是加密API需要知道何时完成。它无法从数据中分辨出来,你必须调用一个方法来说“把它包起来,创建最终的校验和,无论如何,所以接收器可以解码它”。

#2


just use cipher.dofinal("Your byte array","offset...put 0","block size...16");

只需使用cipher.dofinal(“你的字节数组”,“偏移...放0”,“块大小...... 16”);

eg : c.dofinal(ByteArray,0,16);