标签:com text 字节 instance 判断 pre etc md5算法 out
一.Md5加密
1 实现代码: 2 public class MD5 { 3 /** 4 * MD5方法 5 * 6 * @param text 明文 7 * @param key 密钥 8 * @return 密文 9 * @throws Exception 10 */ 11 public static String md5(String text, String key) throws Exception { 12 //加密后的字符串 13 String encodeStr=DigestUtils.md5Hex(text + key); 14 System.out.println("MD5加密后的字符串为:encodeStr="+encodeStr); 15 return encodeStr; 16 } 17 18 }
二.Hash加密
1 public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { 2 3 4 String ps = "111111ieou"; 5 6 MessageDigest messageDigest; 7 messageDigest = MessageDigest.getInstance("SHA-256"); 8 messageDigest.update(ps.getBytes("UTF-8")); 9 String encodeStr = byte2Hex(messageDigest.digest()); 10 11 System.out.println(encodeStr); 12 13 } 14 15 private static String byte2Hex(byte[] bytes) { 16 StringBuilder stringBuffer = new StringBuilder(); 17 String temp = null; 18 for (int i = 0; i < bytes.length; i++) { 19 temp = Integer.toHexString(bytes[i] & 0xFF); 20 if (temp.length() == 1) { 21 //1得到一位的进行补0操作 22 stringBuffer.append("0"); 23 } 24 stringBuffer.append(temp); 25 } 26 return stringBuffer.toString(); 27 }
标签:com text 字节 instance 判断 pre etc md5算法 out
原文地址:https://www.cnblogs.com/wang-yaz/p/10364984.html