早教吧作业答案频道 -->其他-->
加解密代码如下: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
……
请各位高手指点一下,应该怎么解决,不甚感激!
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、确认下你要解密的字节数组是否正确.
我认为出现这个问题可能是你的加密和解密使用的填充算法不一致导致的.
1、要确认下是否加密和解密都是使用相同的填充算法(也就是说,是否都是使用PKCS5Padding)
2、确认下你要解密的字节数组是否正确.
看了 加解密代码如下:public...的网友还看了以下:
flash如何停止下雨效果我用了如下的代码做的下雨效果.我想在某一帖停止加什么代码可以呀.代码是: 2020-04-06 …
一家鞋店近期销售了一款新鞋40双,其中各种尺码的鞋销售量如下:尺码(cm)2222.52323.5 2020-04-07 …
物体测量中如果“右物左码”怎么测量?某同学将以实际质量为8.7g的食盐放在天平上称量(1g以下用游 2020-05-12 …
如何将BCD码表示的3位十进制数用数码管显示出来就比如64BCD码是1000000,而7447要显 2020-05-15 …
某衬衫店为了准确进货,对一周中商店各种尺码的衬衫的销售情况进行统计,结果如下:38码的5件、39码 2020-05-15 …
关于方程x4-2ax2-x+a2-a=0(a≥3/4)为什么可以将a示为未知数?解得a1(角码)= 2020-05-20 …
如何利用杠杆原理测量待测物体质量(例如硬币)大神们帮帮忙要求:在硬币质量未知情况下如何测出待测物体 2020-06-07 …
给定如下Java代码,不能填入下划线处的语句是?急求1.给定如下Java代码,不能填入下划线处的语 2020-07-17 …
两个质量相同的钩码,下端固定有纸带并穿过打点计时器,在拉力作用下两钩码分别沿竖直向上方向运动,纸带上 2020-11-10 …
数学题某鞋厂为了解生产的某种皮鞋销售情况,在商场进行了一天调查数据如下:38码一双39码2双40码2 2020-11-20 …