加解密方法
package com.my.srv.core;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidParameterException;
import java.security.MessageDigest;
import java.security.SecureRandom;
public class EncryptTool {
private static final String MD5 = "md5";
private static final String SHA1 = "sha1";
private static final String HmacMD5 = "HmacMD5";
private static final String HmacSHA1 = "HmacSHA1";
private static final String DES = "DES";
private static final String AES = "AES";
private String charset = "utf-8";
private int keySizeDes = 0;
private int keySizeAes = 128;
private static EncryptTool self;
private EncryptTool() {
}
public static EncryptTool getInstance() {
if (self == null) {
synchronized (EncryptTool.class) {
if (self == null) {
self = new EncryptTool();
}
}
}
return self;
}
private String messageDigest(String res, String algorithm) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
byte[] resBytes = charset == null ? res.getBytes() : res.getBytes(charset);
return base64(md.digest(resBytes));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String keyGeneratorMac(String res, String algorithm, String key) {
try {
SecretKey sk = null;
if (key == null) {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
sk = kg.generateKey();
} else {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
sk = new SecretKeySpec(keyBytes, algorithm);
}
Mac mac = Mac.getInstance(algorithm);
mac.init(sk);
byte[] result = mac.doFinal(res.getBytes());
return base64(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String keyGeneratorES(String res, String algorithm, String key, int keySize, boolean isEncode) {
try {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);
if (keySize == 0) {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
random.setSeed(keyBytes);
keyGen.init(random);
} else if (key == null) {
keyGen.init(keySize);
} else {
byte[] keyBytes = charset == null ? key.getBytes() : key.getBytes(charset);
random.setSeed(keyBytes);
keyGen.init(keySize, random);
}
SecretKey sk = keyGen.generateKey();
SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
if (isEncode) {
cipher.init(Cipher.ENCRYPT_MODE, sks);
byte[] resBytes = charset == null ? res.getBytes() : res.getBytes(charset);
return parseByte2HexStr(cipher.doFinal(resBytes));
} else {
cipher.init(Cipher.DECRYPT_MODE, sks);
return new String(cipher.doFinal(parseHexStr2Byte(res)));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String base64(byte[] res) {
return Base64.getEncoder().encodeToString(res);
}
public static String parseByte2HexStr(byte[] buf) {
StringBuffer sb = new StringBuffer();
for (byte b : buf) {
String hex = Integer.toHexString(b & 0xFF);
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 null;
}
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 String md5(String res) {
return messageDigest(res, MD5);
}
public String md5(String res, String key) {
return keyGeneratorMac(res, HmacMD5, key);
}
public String sha1(String res) {
return messageDigest(res, SHA1);
}
public String sha1(String res, String key) {
return keyGeneratorMac(res, HmacSHA1, key);
}
public String desEncode(String res, String key) {
return changePosition(keyGeneratorES(res, DES, key, keySizeDes, true));
}
public String desDecode(String res, String key) {
res = changePosition(res);
return keyGeneratorES(res, DES, key, keySizeDes, false);
}
public String aesEncode(String res, String key) {
return keyGeneratorES(res, AES, key, keySizeAes, true);
}
public String aesDecode(String res, String key) {
return keyGeneratorES(res, AES, key, keySizeAes, false);
}
public String xorEncode(String res, String key) {
byte[] bs = res.getBytes();
for (int i = 0; i < bs.length; i++) {
bs[i] = (byte) ((bs[i]) ^ key.hashCode());
}
return parseByte2HexStr(bs);
}
public String xorDecode(String res, String key) {
byte[] bs = parseHexStr2Byte(res);
for (int i = 0; i < bs.length; i++) {
bs[i] = (byte) ((bs[i]) ^ key.hashCode());
}
return new String(bs);
}
public int xor(int res, String key) {
return res ^ key.hashCode();
}
public String base64Encode(String res) {
return Base64.getEncoder().encodeToString(res.getBytes());
}
public String base64Decode(String res) {
return new String(Base64.getDecoder().decode(res));
}
private String changePosition(String src) {
final int minLength = 4;
char[] chars = src.toCharArray();
int len = src.length();
if (len < minLength) {
throw new InvalidParameterException("The string length must greater than 4.");
}
char temp = chars[1];
chars[1] = chars[len - 2];
chars[len - 2] = temp;
temp = chars[3];
chars[3] = chars[len - 4];
chars[len - 4] = temp;
return new String(chars);
}
public static void main(String[] args) {
String src = "123456";
String key = "be5e0323a9195ade5f56695ed9f2eb6b036f3e6417115d0cbe2fb9d74d8740406838dc84f152014b39a241" +
"4fb3530a40bc028a9e87642bd03cf5c36a1f70801e";
EncryptTool et = EncryptTool.getInstance();
String out = et.desEncode(src, key);
System.out.println(out);
out = "61A61413CEF9C9F4";
System.out.println(et.desDecode(out, key));
}
}