MD5 AES Des 加密解密
阿新 • • 發佈:2019-04-26
fin static 解密 mes long 不能 tostring eset ++
MD5加密不可逆 只能加密,不能解密,代碼如下:
package com.ruoyi.common.utils.security;
import java.security.MessageDigest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Md5加密方法
*
* @author ruoyi
*/
public class Md5Utils
{
private static final Logger log = LoggerFactory.getLogger(Md5Utils.class);
private static byte[] md5(String s){
MessageDigest algorithm;
try
{
algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(s.getBytes("UTF-8"));
byte[] messageDigest = algorithm.digest();
return messageDigest;
}
catch (Exception e){
log.error("MD5 Error...", e);
}
return null;
}
private static final String toHex(byte hash[])
{
if (hash == null)
{
return null;
}
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++){
if ((hash[i] & 0xff) < 0x10)
{
buf.append("0");
}
buf.append(Long.toString(hash[i] & 0xff, 16));
}
return buf.toString();
}
public static String hash(String s)
{
try
{
return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8");
}
catch (Exception e)
{
log.error("not supported charset...{}", e);
return s;
}
}
}
AES加密 做AES加密時有些問題暫未解決,僅做參考,只能加密,解密時有錯: 更多內容參考如下網址 加密時的秘鑰只能是16位的
https://www.cnblogs.com/codermen/p/7802969.html
Des加密,加密秘鑰必須是8位,參考網址如下:
https://www.cnblogs.com/james0/p/7063941.html
MD5 AES Des 加密解密