如何解决 javax.crypto.IllegalBlockSizeException:数据未对齐块大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4062015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:06:41  来源:igfitidea点击:

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

java

提问by Rebecca

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

我正在做一个关于使用河豚在 Java 中进行加密和解密的作业。

I had added a provider, and get instance "Blowfish/ECB/NoPadding", but I still get this error when I do the encryption.

我已经添加了一个提供者,并获得了实例“Blowfish/ECB/NoPadding”,但在我进行加密时仍然出现此错误。

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

e.g.:

例如:

public static byte[] encrypt(byte to_encrypt[], byte strkey[]) {
    try {           
        SecretKeySpec key = new SecretKeySpec(strkey, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);  
        return cipher.doFinal(to_encrypt); // <=========== error
    } catch (Exception e) { 
        e.printStackTrace();
        return null; 
    }
}

leads to

造成

javax.crypto.IllegalBlockSizeException: data not block size aligned
    at org.bouncycastle2.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:686)
    at javax.crypto.Cipher.doFinal(Cipher.java:1171)

Thank you.

谢谢你。

回答by Andrzej Doyle

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

您已明确要求不进行填充的提供程序(注意NoPadding实例名称中的 )。因此,您的输入不会被填充。

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.
  1. 自己将输入填充为块大小的倍数。
  2. PKCS5Padding如果您不想手动进行填充,请选择进行填充的提供程序(例如)。鉴于您问题的性质,这可能是最佳选择。

回答by dogbane

You are using NoPaddingand the size of your input data must not match the block size of the cipher, so an IllegalBlockSizeExceptionis being thrown. If you use NoPadding you need to make sure that your input is a multiple of 8 bytes.

您正在使用NoPadding并且输入数据的大小必须与密码的块大小不匹配,因此IllegalBlockSizeException会抛出一个。如果您使用 NoPadding,您需要确保您的输入是 8 字节的倍数。

Try specifying a padding scheme. Change to Blowfish/CBC/PKCS5Paddingand it should work.

尝试指定填充方案。更改为Blowfish/CBC/PKCS5Padding它应该可以工作。