/*编码格式;默认使用uft-8/
public String charset = “utf-8”;
/*DES/
public int keysizeDES = 0;
/*AES/
public int keysizeAES = 128;
public static EncryptUtil me;
private EncryptUtil(){
//单例
}
//双重锁
public static EncryptUtil getInstance(){
if (me==null) {
synchronized (EncryptUtil.class) {
if(me == null){
me = new EncryptUtil();
}
}
}
return me;
}
/**
-
使用MessageDigest进行单向加密(无密码)
-
@param res 被加密的文本
-
@param algorithm 加密算法名称
-
@return
*/
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;
}
/**
-
使用KeyGenerator进行单向/双向加密(可设密码)
-
@param res 被加密的原文
-
@param algorithm 加密使用的算法名称
-
@param key 加密使用的秘钥
-
@return
*/
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();
}
r