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();
}
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);
}
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";
public static byte[] encryptIV(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
byte[] iv = generateIV();
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
byte[] encryptedData = cipher.doFinal(data);
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;
}
public static byte[] decryptIV(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
byte[] iv = new byte[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.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
return cipher.doFinal(encryptedData);
}
private static byte[] generateIV() {
byte[] iv = new byte[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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律