3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。
CBC(cipher block chaining) 为链式加密,即将待加密的数据分成n块,前一块加密完的数据作为salt加入后一个模块的加密。
其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密表,这样,
3DES加密过程为:C=Ek3(Dk2(Ek1(P)))
3DES解密过程为:P=Dk1((EK2(Dk3(C)))
---
3DES对称加密解密,3DES CBC Padding PKCS7
此处固定密钥与偏移量,采用3DES对加密字符串进行加密并以BASE64格式返加密串返回参数,采用3DES对解密串(Base64格式)进行解密。
<!-- maven-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.60</version>
</dependency>
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;
public class 3DESUtils {
private static Logger logger = LoggerFactory.getLogger(3DESUtils.class);
private static final key = "a1b2c3d4e5f6g7h8i9j0k1l2"; //密钥长度必须是8的倍数,可取24位或32位
private static final iv = "abcd1234"; //偏移量
/*
* 3DES+BASE64加密
*/
public static String encrypt3DESToBase64(String sourceString) {
String strRTN;
try {
byte[] bytesString = sourceString.getBytes(StandardCharsets.UTF_8);
byte[] bytesKey = hexStringToBytes(getHexString(key.getBytes(StandardCharsets.UTF_8)));
byte[] ivBytes = hexStringToBytes(getHexString(iv.getBytes(StandardCharsets.UTF_8)));
Security.addProvider(new BouncyCastleProvider());
//Cipher对象实际完成加密操作
Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC");
//用密钥和偏移量初始化Cipher对象
cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytesKey, "DESede"), new IvParameterSpec(ivBytes));
byte[] bytes3DES = cEnc.doFinal(bytesString);
BASE64Encoder base64en = new BASE64Encoder();
strRTN = base64en.encode(bytes3DES);
}
catch (Exception e) {
logger.info("异常:" + e.getMessage());
return null;
}
return strRTN;
}
/*
* 3DES+BASE64解密
*/
public static String decrypt3DESFromBase64(String sourceString) {
String strRTN;
try {
BASE64Decoder base64en = new BASE64Decoder();
byte[] bytesString = base64en.decodeBuffer(sourceString);
byte[] bytesKey = hexStringToBytes(getHexString(key.getBytes(StandardCharsets.UTF_8)));
byte[] ivBytes = hexStringToBytes(getHexString(iv.getBytes(StandardCharsets.UTF_8)));
Security.addProvider(new BouncyCastleProvider());
Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC");
cEnc.init(Cipher.DECRYPT_MODE, new SecretKeySpec(bytesKey, "DESede"), new IvParameterSpec(ivBytes));
byte[] bytes3DES = cEnc.doFinal(bytesString);
strRTN = new String(bytes3DES,"UTF-8");
} catch (Exception e) {
logger.info("异常:" + e.getMessage());
return null;
}
return strRTN;
}
/*
* 数组转换为16进制字符串
*/
private static String getHexString(byte[] data) {
StringBuilder sb = new StringBuilder();
for (byte b : data) {
String ch = Integer.toHexString(b & 0xFF).toUpperCase();
if (ch.length() == 2)
sb.append(ch);
else
sb.append("0").append(ch);
}
return sb.toString();
}
/*
* 将16进制的字符串转换为byte数组
*/
private static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}