package com.test;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class des3 {
// 向量
// private final static String iv = "01234567";
// 加解密统一使用的编码方式
private final static String encoding = "utf-8";
private static final String KEY_ALGORITHM = "DESede";
private static final String DEFAULT_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";// 默认的加密算法
public static void main(String[] args) throws Exception {
String key = "00003402982617113613670000000000";
String str = "测试";
System.out.println("随机key-----------> " + key);
String encrypt3DES = encode(str, key);
System.out.println("加密-----------> " + encrypt3DES);
String decrypt3DES = decode(encrypt3DES,key);
System.out.println("解密-----------> " + decrypt3DES);
}
/**
*
* 3DES加密 32位密钥
*
* @param plainText
* 普通文本
* @return
*
* @throws Exception
*/
public static String encode(String plainText, String secretKey)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory
.getInstance(KEY_ALGORITHM);
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
return bytesToHexString(encryptData);
}
/**
*
* 3DES解密 32位密钥
*
* @param encryptText
* 加密文本
* @return
* @throws Exception
*/
public static String decode(String encryptText, String secretKey)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory
.getInstance(KEY_ALGORITHM);
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] decryptData = cipher.doFinal(hexStringToBytes(encryptText));
return new String(decryptData, encoding);
}
public static String bytesToHexString(byte[] bytes) {
StringBuilder stringBuilder = new StringBuilder();
if (bytes != null && bytes.length > 0) {
byte[] arg1 = bytes;
int arg2 = bytes.length;
for (int arg3 = 0; arg3 < arg2; ++arg3) {
byte b = arg1[arg3];
int v = b & 255;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString().toUpperCase();
} else {
return "";
}
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString != null && !"".equals(hexString)) {
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;
} else {
return null;
}
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}