首页 > 代码库 > 3DES 加、解密
3DES 加、解密
package com.suning.hrqz.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Base64Utils;
//import java.util.Base64;
//import org.apache.commons.net.util.Base64;
public class DES3 {
public static void main(String[] args) throws UnsupportedEncodingException,
NoSuchAlgorithmException {
String keyStr = "HRDS20170810";
System.out.println("密钥明文:" + keyStr);
byte keys[] = keyStr.getBytes("UTF-8");
System.out.println("UTF-8十六进制:" + bytes2hex(keys));
String keyBase64 = Base64Utils.encodeToString(keys);
System.out.println("Base64密钥:" + keyBase64);
System.out.println("Base64密钥还原:"
+ new String(Base64Utils.decodeFromString(keyBase64), "UTF-8")
+ "\n");
String strText = "1";
System.out.println("待加密的串:" + strText);
String text1 = DES3.encryptProperty(strText, keyBase64);
System.out.println("Base64密文:" + text1);
String text2 = DES3.decryptProperty(text1, keyBase64);
System.out.println("解密出的串:" + text2);
// ////加密指纹
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update((strText + keyStr).getBytes("UTF-8"));
System.out.println("MD5加密指纹:" + bytes2hex(md5.digest()));
}
private DES3() {
}
public static String encryptProperty(String clearText, String keyBase64) {
if(StringUtils.isBlank(clearText)){
return "";
}
byte[] key = Base64Utils.decodeFromString(keyBase64);
return DES3.performDESedeCoder(clearText, key, true);
}
public static String decryptProperty(String cipherText, String keyBase64) {
byte[] key = Base64Utils.decodeFromString(keyBase64);
return DES3.performDESedeCoder(cipherText, key, false);
}
public static String performDESedeCoder(String inputValue, byte[] key,
boolean encrypt) {
byte[] k = key;
byte keyByteArray[];
if (k.length < 24) {
keyByteArray = new byte[24];
System.arraycopy(k, 0, keyByteArray, 0, k.length);
for (int i = k.length; i < 24; i++) {
keyByteArray[i] = 48;// 不足24字节时,补0
}
k = keyByteArray;
}
String KEY_ALGORITHM = "DESede";
String CIPHER_ALGORITHM = "DESede/CBC/PKCS5Padding";
byte[] data = null;
try {
DESedeKeySpec dks = new DESedeKeySpec(k);
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance(KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
byte[] input;
if (encrypt) {
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
input = inputValue.getBytes("UTF-8");
} else {
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
input = Base64Utils.decodeFromString(inputValue);
}
data = cipher.doFinal(input);
} catch (Exception e) {
throw new HRQZRuntimeException(e);
}
String rtnValue;
if (data == null) {
rtnValue = inputValue;
} else {
if (encrypt) {
rtnValue = Base64Utils.encodeToString(data);
} else {
try {
rtnValue = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HRQZRuntimeException(e);
}
}
}
return rtnValue;
}
public static String bytes2hex(byte[] bytes) {
final String HEX = "0123456789ABCDEF";
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
// 取出这个字节的高4位,然后与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数
sb.append(HEX.charAt((b >> 4) & 0x0f));
// 取出这个字节的低位,与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数
sb.append(HEX.charAt(b & 0x0f));
}
return sb.toString();
}
}3DES 加、解密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。