Java工具类(30)—DES加密工具类
package com.awifi.cloudnative.container.manage.provider.utils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
public class EncryptUtil {
public static String encryptByDes(String message, String keyString) {
if(StringUtils.isEmpty(message) || StringUtils.isEmpty(keyString)){
return StringUtils.EMPTY;
}
String keyHexString = stringToHexString(keyString);
byte[] key = hexStringToBytes(keyHexString);
String dataHexString = stringToHexString(message);
byte[] data = hexStringToBytes(dataHexString);
try {
SecureRandom sr = new SecureRandom();
SecretKeyFactory keyFactory;
DESKeySpec dks = new DESKeySpec(key);
keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretkey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretkey, sr);
byte[] result = cipher.doFinal(data);
return new String(Base64.encodeBase64(result));
} catch (Exception e) {
e.printStackTrace();
}
return StringUtils.EMPTY;
}
public static String decryptByDes(String dataHexString, String keyString) {
if(StringUtils.isEmpty(dataHexString) || StringUtils.isEmpty(keyString)){
return StringUtils.EMPTY;
}
String keyHexString = stringToHexString(keyString);
byte[] key = hexStringToBytes(keyHexString);
byte[] result = null;
try {
SecureRandom sr = new SecureRandom();
SecretKeyFactory keyFactory;
DESKeySpec dks = new DESKeySpec(key);
keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretkey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretkey, sr);
result = cipher.doFinal(Base64.decodeBase64(dataHexString));
return new String(result).trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String stringToHexString(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return sb.toString();
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
int hexLength = length;
while (hexLength % 8 != 0) {
hexLength++;
}
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[hexLength];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
public static String bytesToHexString(byte[] b) {
StringBuilder stringBuilder = new StringBuilder();
if (b == null || b.length <= 0) {
return null;
}
for (int i = 0; i < b.length; i++) {
int v = b[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static String hexStringToString(String str) {
byte[] baKeyword = new byte[str.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
str = new String(baKeyword, "utf-8");
} catch (Exception e1) {
e1.printStackTrace();
}
return str;
}
public static String md5Digest(String data) {
byte[] bytes = DigestUtils.md5Digest(data.getBytes(StandardCharsets.UTF_8));
return byte2hex(bytes);
}
public static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
public static void main(String[] args) {
String str = "18989861590";
String encryptStr = encryptByDes(str, "50739180");
System.out.println("加密后的字符串 = " + encryptStr);
String decryptStr = decryptByDes(encryptStr,"50739180");
System.out.println("解密后的字符串 = " + decryptStr);
}
}