des解密 Input length not multiple of 8 bytes 错误
使用Java自带的库解密,密文由别人提供,当密文是64位的时候能够解密,但是当密文非64位的时候就出现javax.crypto.IllegalBlockSizeException: Input length not multiple of 8 bytes错误提醒,请问一下这是什么原因?
代码如下:
代码如下:
private final static String DES = "DES";
public static String decrypt(String data, String key) throws IOException, Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, key.getBytes());
return new String(bt);
}private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
...全文


Java SE
