/* * Copyright (C) 2012 www.amsoft.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ab.util; import java.security.MessageDigest; // TODO: Auto-generated Javadoc /** * © 2012 amsoft.cn * 名称:AbMd5.java * 描述:MD5加密. * * @author 还如一梦中 * @version v1.0 * @date:2013-01-17 下午11:52:13 */ public class AbMd5 { /** * 描述:MD5加密. * @param str 要加密的字符串 * @return String 加密的字符串 */ public final static String MD5(String str) { char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { byte[] strTemp = str.getBytes(); MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(strTemp); byte tmp[] = mdTemp.digest(); // MD5 的计算结果是一个 128 位的长整数, // 用字节表示就是 16 个字节 char strs[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符, // 所以表示成 16 进制需要 32 个字符 int k = 0; // 表示转换结果中对应的字符位置 for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节 // 转换成 16 进制字符的转换 byte byte0 = tmp[i]; // 取第 i 个字节 strs[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换, // >>> 为逻辑右移,将符号位一起右移 strs[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换 } return new String(strs).toUpperCase(); // 换后的结果转换为字符串 } catch (Exception e) { return null; } } /** * The main method. * * @param args the arguments */ public static void main(String[] args) { System.out.println(AbMd5.MD5("2011123456").toLowerCase()); } }