DES 解密错 Given final block not properly padded 转

Given final block not properly padded

 

我也有碰到过这样的问题
原因是:
1、加密完byte[] 后,需要将加密了的byte[] 转换成base64保存,如:
BASE64Encoder base64encoder = new BASE64Encoder();
String encode=base64encoder.encode(bytes);

2、解密前,需要将加密后的字符串从base64转回来再解密,如:
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encodeByte = base64decoder.decodeBuffer(str);

后面是我的源代码,希望对你有所帮助

import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Security 提供了一个安全算法类,其中包括对称密码算法和散列算法
 */
public final class EbotongSecurity {
// The length of Encryptionstring should be 8 bytes and not be
// a weak key
private final static BASE64Encoder base64encoder = new BASE64Encoder();
private final static BASE64Decoder base64decoder = new BASE64Decoder();
private final static String encoding = "UTF-8";

/**
 * 加密字符串
 */
public static String ebotongEncrypto(String str) {
String result = str;
if (str != null && str.length() > 0) {
try {
byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));
result = base64encoder.encode(encodeByte);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}

/**
 * 解密字符串
 */
public static String ebotongDecrypto(String str) {
String result = str;
if (str != null && str.length() > 0) {
try {
byte[] encodeByte = base64decoder.decodeBuffer(str);
byte[] decoder = EbotongSecurity.symmetricDecrypto(encodeByte);
result = new String(decoder, encoding);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}

/**
 * 对称加密方法
 * 
 * @param byteSource
 *            需要加密的数据
 * @return 经过加密的数据
 * @throws Exception
 */
public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = { 1, 9, 8, 2, 0, 8, 2, 1 };
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
// int blockSize = cipher.getBlockSize();
// int position = 0;
// int length = byteSource.length;
// boolean more = true;
// while (more) {
// if (position + blockSize <= length) {
// baos.write(cipher.update(byteSource, position, blockSize));
// position += blockSize;
// } else {
// more = false;
// }
// }
// if (position < length) {
// baos.write(cipher.doFinal(byteSource, position, length
// - position));
// } else {
// baos.write(cipher.doFinal());
// }
// return baos.toByteArray();
return result;
} catch (Exception e) {
throw e;
} finally {
baos.close();
}
}

/**
 * 对称解密方法
 * 
 * @param byteSource
 *            需要解密的数据
 * @return 经过解密的数据
 * @throws Exception
 */
public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = { 1, 9, 8, 2, 0, 8, 2, 1 };
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
// int blockSize = cipher.getBlockSize();
// int position = 0;
// int length = byteSource.length;
// boolean more = true;
// while (more) {
// if (position + blockSize <= length) {
// baos.write(cipher.update(byteSource, position, blockSize));
// position += blockSize;
// } else {
// more = false;
// }
// }
// if (position < length) {
// baos.write(cipher.doFinal(byteSource, position, length
// - position));
// } else {
// baos.write(cipher.doFinal());
// }
// return baos.toByteArray();
return result;
} catch (Exception e) {
throw e;
} finally {
baos.close();
}
}

/**
 * 散列算法
 * 
 * @param byteSource
 *            需要散列计算的数据
 * @return 经过散列计算的数据
 * @throws Exception
 */
public static byte[] hashMethod(byte[] byteSource) throws Exception {
try {
MessageDigest currentAlgorithm = MessageDigest.getInstance("SHA-1");
currentAlgorithm.reset();
currentAlgorithm.update(byteSource);
return currentAlgorithm.digest();
} catch (Exception e) {
throw e;
}
}

}

<script></script>

个人签名

-------------------------------------

 

图盾 淘宝保护 保护图片 图片防盗

解密的时候报错: 
javax.crypto.BadPaddingException: Given final block not properly padded 
详细错误信息: 
javax.crypto.BadPaddingException: Given final block not properly padded 
at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DashoA6275) 
at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DashoA6275) 
at javax.crypto.Cipher.doFinal(DashoA6275) 
at ThreeDesTool.decryptMode(ThreeDesTool.java:41) 
at Authentication.Authentication(Authentication.java:122) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. 
java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces 
sorImpl.java:25) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值