早教吧 育儿知识 作业答案 考试题库 百科 知识分享

加解密代码如下:publicstaticbyte[]encryptRes(byte[]data,byte[]desKey){try{IvParameterSpecspec=newIvParameterSpec(desKey);DESKeySpecdks=newDESKeySpec(desKey);SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance

题目详情
加解密代码如下:
public static byte[] encryptRes(byte[] data, byte[] desKey) {
try {
IvParameterSpec spec = new IvParameterSpec(desKey);
DESKeySpec dks = new DESKeySpec(desKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);
byte[] encryptContent = cipher.doFinal(data);
return encryptContent;
} catch (Exception e) {
return null;
}
}
public static byte[] decryptRes(byte[] data, byte[] desKey) {
try {
IvParameterSpec spec = new IvParameterSpec(desKey);
DESKeySpec dks = new DESKeySpec(desKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, securekey, spec);
//data = Base64.decode(data);
byte[] encryptContent = cipher.doFinal(data);
return encryptContent;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
解密时,异常信息如下所示:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at beans.DESUtil.decryptRes(DESUtil.java:39)
at beans.DESUtil.decryptCertKey(DESUtil.java:61)
at beans.PublicKeyList.getPublicKeyList(PublicKeyList.java:53)
at servlet.GetPublicKey.doGet(GetPublicKey.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
……
请各位高手指点一下,应该怎么解决,不甚感激!
▼优质解答
答案和解析
根据你提供的错误消息.
我认为出现这个问题可能是你的加密和解密使用的填充算法不一致导致的.
1、要确认下是否加密和解密都是使用相同的填充算法(也就是说,是否都是使用PKCS5Padding)
2、确认下你要解密的字节数组是否正确.
看了 加解密代码如下:public...的网友还看了以下: