标签:
MD5:
参考: http://baike.baidu.com/link?url=VhzMlvINaFcUPRTZg3ZEp27QL1c1ISmTzW8uGuNnH4MMsp5KVvShh8zEgMu-TnzfG7uWQNJp9QUIzhkMUTvY6q
http://blog.sina.com.cn/s/blog_6b275753010161t3.html
在线工具:http://www.md5.com.cn/
定义:Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。
import java.security.MessageDigest; /** * Created by bochao.lbc on 2015/9/3. * * @describe */ public class MD5Util { public final static String MD5(String s) { char hexDigits[] = {‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘}; try { byte[] btInput = s.getBytes(); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(btInput); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args) { System.out.println(MD5Util.MD5("20121221")); System.out.println(MD5Util.MD5("加密")); }
标签:
原文地址:http://www.cnblogs.com/lance-/p/4780433.html