DESC 加密解密
package com.example.base.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
public class DesUtil {
private static final Logger logger = LoggerFactory.getLogger(DesUtil.class);
private final static String ALGORITHM = "DES";
private static final String DEFAULT_CHAR_SET = "UTF-8";
private static final String KEY = "5e1de9b31ca44980905dba949869ccec";
public static String encrypt(String plaintext, String key) throws Exception {
try {
byte[] dataBytes = plaintext.getBytes(DEFAULT_CHAR_SET);
byte[] keyBytes = key.getBytes(DEFAULT_CHAR_SET);
SecretKey secretKey = getSecretKey(keyBytes);
SecureRandom random = getSecureRandom();
Cipher cipher = getCipher();
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
byte[] bytes = cipher.doFinal(dataBytes);
return byte2hex(bytes);
} catch (Exception e) {
logger.error("method=encrypt() is error, error_msg:{}, error:{}", e.getMessage(), e);
}
return null;
}
private static String decrypt(String ciphertext, String key) throws Exception {
try {
byte[] dataBytes = ciphertext.getBytes(DEFAULT_CHAR_SET);
byte[] hex2byte = hex2byte(dataBytes);
byte[] keyBytes = key.getBytes(DEFAULT_CHAR_SET);
SecretKey secretKey = getSecretKey(keyBytes);
SecureRandom random = getSecureRandom();
Cipher cipher = getCipher();
cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
byte[] doFinal = cipher.doFinal(hex2byte);
return new String(doFinal, DEFAULT_CHAR_SET);
} catch (Exception e) {
logger.error("method=encrypt() is error, error_msg:{}, error:{}", e.getMessage(), e);
}
return null;
}
private static SecureRandom getSecureRandom() throws Exception {
return new SecureRandom();
}
private static SecretKey getSecretKey(byte[] keyBytes) throws Exception {
DESKeySpec dks = new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
return keyFactory.generateSecret(dks);
}
private static Cipher getCipher() throws Exception {
return Cipher.getInstance(ALGORITHM);
}
private static byte[] hex2byte(byte[] bytes) throws Exception {
final int temp = 2;
if ((bytes.length % temp) != 0) {
throw new IllegalArgumentException("长度不是偶数");
}
byte[] bte = new byte[bytes.length / temp];
for (int n = 0; n < bytes.length; n += temp) {
String item = new String(bytes, n, temp, DEFAULT_CHAR_SET);
bte[n / temp] = (byte) Integer.parseInt(item, 16);
}
return bte;
}
private static String byte2hex(byte[] bytes) throws Exception {
StringBuilder bulid = new StringBuilder();
String stmp;
for (byte bte : bytes) {
stmp = (Integer.toHexString(bte & 0XFF));
if (stmp.length() == 1) {
bulid.append("0").append(stmp);
} else {
bulid.append(stmp);
}
}
return bulid.toString();
}
public static void main(String[] args) throws Exception {
String content = "这是一个测试";
String str = DesUtil.encrypt(content, KEY);
System.out.println("plaintext: " + str);
str = DesUtil.decrypt(str, KEY);
System.out.println("ciphertext: " + str);
}
}