import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
public class DESUtil {
public static byte[] desKey;
static{
try{
desKey = initKey();
}catch(Exception e){
e.printStackTrace();
}
}
/*
* 生成密钥
*/
public static byte[] initKey() throws Exception{
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
/*
* DES 加密
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception{
SecretKey secretKey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
/*
* DES 解密
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception{
SecretKey secretKey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
//==========================新版随机密文===========
private static final String ALGORITHM = "DES";
private static final String TRANSFORMATION = "DES/CBC/PKCS5Padding";
/**
* DES 加密
*
* @param data 明文数据
* @param key 密钥
* @return 加密后的密文 + IV
* @throws Exception
*/
public static byte[] encryptIV(byte[] data, byte[] key) throws Exception {
// 创建 DES 密钥
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
// 生成随机的 IV
byte[] iv = generateIV();
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 获取 Cipher 实例并初始化为加密模式
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
// 执行加密操作
byte[] encryptedData = cipher.doFinal(data);
// 将 IV 和加密后的数据拼接在一起返回
byte[] result = new byte[iv.length + encryptedData.length];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(encryptedData, 0, result, iv.length, encryptedData.length);
return result;
}
/**
* DES 解密
*
* @param data 加密后的密文 + IV
* @param key 密钥
* @return 解密后的明文
* @throws Exception
*/
public static byte[] decryptIV(byte[] data, byte[] key) throws Exception {
// 创建 DES 密钥
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
// 提取 IV
byte[] iv = new byte[8]; // DES 的 IV 长度为 8 字节
System.arraycopy(data, 0, iv, 0, iv.length);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 提取加密后的数据
byte[] encryptedData = new byte[data.length - iv.length];
System.arraycopy(data, iv.length, encryptedData, 0, encryptedData.length);
// 获取 Cipher 实例并初始化为解密模式
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
// 执行解密操作
return cipher.doFinal(encryptedData);
}
/**
* 生成随机的 IV
*
* @return 随机生成的 IV
*/
private static byte[] generateIV() {
byte[] iv = new byte[8]; // DES 的 IV 长度为 8 字节
new SecureRandom().nextBytes(iv);
return iv;
}
public static void main(String[] args) throws Exception {
Base64 base64 = new Base64(0, null, true);
String encrypt1 = base64.encodeAsString(encryptIV("1313xxxx".getBytes(StandardCharsets.UTF_8), "ah^nacy6".getBytes(StandardCharsets.UTF_8)));
System.err.println(encrypt1);
String decrypt1 = new String(decryptIV(base64.decode(encrypt1), "ah^nacy6".getBytes(StandardCharsets.UTF_8)));
System.err.println(decrypt1);
System.err.println("===========================");
String encrypt2 = base64.encodeAsString(encryptIV("1313xxxx".getBytes(StandardCharsets.UTF_8), "ah^nacy6".getBytes(StandardCharsets.UTF_8)));
System.err.println(encrypt2);
String decrypt2 = new String(decryptIV(base64.decode(encrypt2), "ah^nacy6".getBytes(StandardCharsets.UTF_8)));
System.err.println(decrypt2);
}
}