Java学习笔记25:Java中MD5使用

来源:互联网 发布:昆士兰会计硕士知乎 编辑:程序博客网 时间:2024/05/21 20:24
import java.security.MessageDigest;/** * MD5工具类 * @author zhuli 2012-12-19 下午6:22:42 */public class Md5Util {         /**     * 字符串MD5加密后返回字符串格式     * @param md5Str     * @return     */    public static String MD5(String md5Str) {         byte[] btInput = md5Str.getBytes();        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',                              'a', 'b', 'c', 'd', 'e', 'f'                            };         try {             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) {             return null;         }     } }


一个MD5的工具类,使用包:java.security.MessageDigest; 是二进制的,所以需要进行处理之后,才能传递进去一个字符串,获得到一个字符串


备注:http://propedit.sourceforge.jp/eclipse/updates/ 国际化语言 插件


原创粉丝点击