JAVA MD5 算法实现
2007-03-13 10:51
609 查看
import java.security.MessageDigest;
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114244680.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114259030.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114257460.gif)
/** *//**
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
*
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114256345.gif)
*/
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114259030.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114257460.gif)
public class MD5 ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
/** *//**
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* Constructs the MD5 object and sets the string whose MD5 is to be
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* computed.
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
*
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* @param inStr
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* the <code>String</code> whose MD5 is to be computed
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
*/
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
public MD5() ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
/** *//**
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* Computes the MD5 fingerprint of a string.
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
*
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* @return the MD5 digest of the input <code>String</code>
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
*/
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
public static String compute(String inStr) ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// convert input String to a char[]
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// convert that char[] to byte[]
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// get the md5 digest as byte[]
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// bit-wise AND that byte[] with 0xff
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// prepend "0" to the output StringBuffer to make sure that we don't end
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// up with
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// something like "e21ff" instead of "e201ff"
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
MessageDigest md5=null;
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
try ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
md5 = MessageDigest.getInstance("MD5");
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
} catch (Exception e) ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
System.out.println(e.toString());
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
e.printStackTrace();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
return "";
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
char[] charArray = inStr.toCharArray();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
byte[] byteArray = new byte[charArray.length];
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
for (int i = 0; i < charArray.length; i++)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
byteArray[i] = (byte) charArray[i];
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
byte[] md5Bytes = md5.digest(byteArray);
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
StringBuffer hexValue = new StringBuffer();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
for (int i = 0; i < md5Bytes.length; i++) ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
int val = ((int) md5Bytes[i]) & 0xff;
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
if (val < 16)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
hexValue.append("0");
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
hexValue.append(Integer.toHexString(val));
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
return hexValue.toString();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114256345.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114244680.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114259030.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114257460.gif)
/** *//**
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
*
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114256345.gif)
*/
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114259030.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114257460.gif)
public class MD5 ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
/** *//**
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* Constructs the MD5 object and sets the string whose MD5 is to be
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* computed.
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
*
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* @param inStr
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* the <code>String</code> whose MD5 is to be computed
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
*/
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
public MD5() ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
/** *//**
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* Computes the MD5 fingerprint of a string.
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
*
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
* @return the MD5 digest of the input <code>String</code>
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
*/
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
public static String compute(String inStr) ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// convert input String to a char[]
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// convert that char[] to byte[]
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// get the md5 digest as byte[]
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// bit-wise AND that byte[] with 0xff
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// prepend "0" to the output StringBuffer to make sure that we don't end
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// up with
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
// something like "e21ff" instead of "e201ff"
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
MessageDigest md5=null;
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
try ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
md5 = MessageDigest.getInstance("MD5");
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
} catch (Exception e) ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
System.out.println(e.toString());
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
e.printStackTrace();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
return "";
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
char[] charArray = inStr.toCharArray();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
byte[] byteArray = new byte[charArray.length];
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
for (int i = 0; i < charArray.length; i++)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
byteArray[i] = (byte) charArray[i];
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
byte[] md5Bytes = md5.digest(byteArray);
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
StringBuffer hexValue = new StringBuffer();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114269853.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114268025.gif)
for (int i = 0; i < md5Bytes.length; i++) ...{
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
int val = ((int) md5Bytes[i]) & 0xff;
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
if (val < 16)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
hexValue.append("0");
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
hexValue.append(Integer.toHexString(val));
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
![](http://www.newbooks.com.cn/infos/pic/200611/61877/20061120211425885.gif)
return hexValue.toString();
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114274334.gif)
}
![](http://www.newbooks.com.cn/infos/pic/200611/61877/200611202114256345.gif)
}
相关文章推荐
- MD5 算法的java实现
- 算法--MD5--MD5加密算法的java实现
- 【JAVA】常用加解密算法总结及JAVA实现【BASE64,MD5,SHA,DES,3DES,AES,RSA】
- 加密解密算法java实现(4)—MD5
- 在Java中利用Apache Commons Codec API实现常见的加密解密算法,如:md5,sha256
- java实现的MD5摘要算法完整实例
- JAVA中实现MD5的算法
- 机器学习入门算法及其java实现-朴素贝叶斯
- 中国农历算法java实现
- Java实现算法之Brackets Sequence问题
- 笔试面试算法经典--全排列算法-递归&字典序实现(Java)
- 算法java实现--分支限界法--最优装载问题
- java实现图的最短路径(SP)的迪杰斯特拉(Dijkstra)算法
- PHP实现的MD5结合RSA签名算法实例
- java实现图的最短路径(SP)的贝尔曼福特(Bellman-Ford)算法
- java 语言实现的随机数生成算法
- 算法java实现--分支限界法--布线问题
- 算法学习(java实现之字符串篇)·····判断字符串是否没有重复字符
- 【LeetCode-面试算法经典-Java实现】【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】
- 算法(第四版)学习笔记之java实现快速排序