import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class EncryptUtil {
public static final String ENCRYPT_KEY = "ABCD1234";
private static final String ALGORITHM_DESDE = "DESede";
public static void main(String[] args) {
String encPassword = desEdeEncoder("123456", EncryptUtil.ENCRYPT_KEY);
System.out.println(encPassword);
String desPassword = desEdeDecoder(encPassword, EncryptUtil.ENCRYPT_KEY);
System.out.println(desPassword);
}
public static boolean isEmpty(String str) {
return null == str || str.trim().length() == 0;
}
public static String desEdeEncoder(String src, String key) {
byte[] bytes = null;
try {
SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);
Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
bytes = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new RuntimeException(e);
}
return byte2HexStr(bytes);
}
public static String desEdeDecoder(String dest, String key) {
String desStr;
try {
SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);
Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(str2ByteArray(dest));
desStr = new String(bytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
}
return desStr;
}
public static String md5Digest(String src) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(src.getBytes(StandardCharsets.UTF_8));
return byte2HexStr(bytes);
}
private static byte[] build3DesKey(String keyStr) {
byte[] bytes = new byte[24];
byte[] temp = keyStr.getBytes(StandardCharsets.UTF_8);
System.arraycopy(temp, 0, bytes, 0, Math.min(bytes.length, temp.length));
return bytes;
}
private static byte[] str2ByteArray(String str) {
int byteArrayLength = str.length() / 2;
byte[] bytes = new byte[byteArrayLength];
for (int i = 0; i < byteArrayLength; i++) {
byte b0 = (byte) Integer.valueOf(str.substring(i * 2, i * 2 + 2), 16).intValue();
bytes[i] = b0;
}
return bytes;
}
private static String byte2HexStr(byte[] bytes) {
StringBuilder sbf = new StringBuilder();
for (byte aByte : bytes) {
String s = Integer.toHexString(aByte & 0xFF);
if (s.length() == 1) {
sbf.append("0");
}
sbf.append(s.toUpperCase());
}
return sbf.toString();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108