【问题标题】:Encryption message in javajava中的加密消息
【发布时间】:2010-11-15 20:29:58
【问题描述】:

我是一个关于使用 java 的 bouncycastle 进行加密的项目。

但是,当我加密消息时,它会为我抛出异常。

javax.crypto.IllegalBlockSizeException:数据未对齐块大小

我正在使用 Blowfish/ECB/NoPadding,消息是 xml。

public static void main(String args[]){ 
     String message = "<abc>ABCDEFG</abc>"; 
     String key = "key"; 
     byte[] b = encrypt(message.getBytes(), key.getBytes());
}

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

} 

谁能帮帮我?

谢谢

【问题讨论】:

  • public static void main(String args[]){ String message = "ABCDEFG";字符串键 = "键"; byte[] b = encrypt(message.getBytes(), key.getBytes()); } public byte[] encrypt(byte encrypt[], byte en_key[]) { try { SecretKeySpec key = new SecretKeySpec(en_key, "Blowfish"); Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, en_key);返回 cipher.doFinal(加密); } 捕捉(异常 e){ e.printStackTrace();返回空值; } }
  • 您可以编辑您的问题并将您的代码放在那里(格式化为代码,使用“0101010”图标)而不是放在评论中吗?

标签: java


【解决方案1】:

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

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

用空字节手动填充:
创建一个大小为 8 的倍数的更大的新数组,然后将旧数组复制到其中。

public static byte[] encrypt(byte encrypt[], byte en_key[]) {

    if(encrypt.length % 8 != 0){ //not a multiple of 8
        //create a new array with a size which is a multiple of 8
        byte[] padded = new byte[encrypt.length + 8 - (encrypt.length % 8)];

        //copy the old array into it
        System.arraycopy(encrypt, 0, padded, 0, encrypt.length);
        encrypt = padded;
    }

    try {
        SecretKeySpec key = new SecretKeySpec(en_key, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(encrypt);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

【讨论】:

  • 我觉得一定要用NoPadding...因为我用了PKCS5Padding之后,服务器没有响应。
  • 是的,确实如此。尝试打印出encrypt 方法返回的字节。
  • 另外,如果您使用 NoPadding,您需要确保您的输入是 8 个字节的倍数。如何将输入更改为 8 字节的倍数?
  • 为什么要将输入更改为 8 的倍数?使用PKCS5Padding 更容易,它将为您处理加密和解密。
  • 因为项目有一个服务器并且它使用 nopadding 来做加密和解密。我需要在发送到服务器之前对消息进行加密。
【解决方案2】:
public static void main(String args[]){ 
     String message = "<abc>ABCDEFG</abc>"; 
     String key = "key"; 
     byte[] b = encrypt(message.getBytes(), key.getBytes());
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 2021-08-07
    • 2012-11-10
    • 2017-02-16
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多