import lombok.extern.slf4j.Slf4j;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
/**
* DesUtil
*
* @author XiaoWeng
* @version v1.0
* @date 2022/12/12
*/
@Slf4j
public class DesUtil {
private static final String DES = "DES";
private static final Charset ENCODE = StandardCharsets.UTF_8;
private static final String DEFAULT_KEY = "shige123";
/**
* 使用 默认key 加密
*/
public static String encrypt(String data) {
byte[] bt;
try {
bt = encrypt(data.getBytes(StandardCharsets.UTF_8), DEFAULT_KEY.getBytes(ENCODE));
} catch (Exception e) {
log.error("加密失败=====>" + data);
return null;
}
return parseByte2HexStr(bt);
}
/**
* 使用 默认key 解密
*/
public static String decrypt(String data) {
if (data == null) {
return null;
}
byte[] buf;
byte[] bt;
try {
buf = parseHexStr2Byte(data);
bt = decrypt(buf, DEFAULT_KEY.getBytes(ENCODE));
} catch (Exception e) {
log.error("解密失败=====>" + data);
return null;
}
return new String(bt, ENCODE);
}
/**
* Description 根据键值进行加密
*/
public static String encrypt(String data, String key) {
byte[] bt;
try {
bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
} catch (Exception e) {
log.error("加密失败=====>" + data);
return null;
}
return parseByte2HexStr(bt);
}
/**
* Description 根据键值进行解密
*/
public static String decrypt(String data, String key) {
if (data == null) {
return null;
}
byte[] buf;
byte[] bt;
try {
buf = parseHexStr2Byte(data);
bt = decrypt(buf, key.getBytes(ENCODE));
} catch (Exception e) {
log.error("解密失败=====>" + data);
return null;
}
return new String(bt, ENCODE);
}
/**
* Description 根据键值进行加密
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/**
* Description 根据键值进行解密
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
public static String parseByte2HexStr(byte[] buf) {
StringBuilder sb = new StringBuilder();
for (byte b : buf) {
String hex = Integer.toHexString(b & 255);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return new byte[0];
} else {
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; ++i) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
public static void main(String[] args) throws Exception {
String test = "18513100195";
//加密解密
System.out.println(encrypt(test));
System.out.println(decrypt(encrypt(test)));
}
}
数据加密DesUtil
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 一、引言 本篇博客主要讨论如何在客户端与服务端之间进行HTTPS网络传输,为了深入理解网络传输的基础原理,更加灵活...
- 先点击这里查看http://www.cnblogs.com/guola/archive/2012/10/30/27...
- 实战需求 SwiftUI SQLite数据大全之 如何创建加密数据库并在项目中读取加密数据 (SQLite.swi...
- function getArr() { var arr = []; //创建了一个空的新数组 var rand =...