JAVA中实现MD5的算法
2007-04-24 01:16
477 查看
package com.ctgusec.bean;
import java.security.MessageDigest;
/** *//** */
/** *//**
* @author zhupan
*/
public class MD5 ...{
private String inStr;
private MessageDigest md5;
/** *//** */
/** *//**
* Constructs the MD5 object and sets the string whose MD5 is to be
* computed.
*
* @param inStr
* the String whose MD5 is to be computed
*/
public MD5(String inStr) ...{
this.inStr = inStr;
try ...{
this.md5 = MessageDigest.getInstance(" MD5 ");
} catch (Exception e) ...{
System.out.println(e.toString());
e.printStackTrace();
}
}
/** *//** */
/** *//**
* Computes the MD5 fingerprint of a string.
*
* @return the MD5 digest of the input String
*/
public String compute() ...{
// convert input String to a char[]
// convert that char[] to byte[]
// get the md5 digest as byte[]
// bit-wise AND that byte[] with 0xff
// prepend "0" to the output StringBuffer to make sure that we don't end
// up with
// something like "e21ff" instead of "e201ff"
char[] charArray = this.inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = this.md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) ...{
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append(" 0 ");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
public static void main(String[] args) ...{
MD5 md5 = new MD5(" zhupan ");
String postString = md5.compute();
System.out.println(postString);
if (postString.equals(" 11c454577d6f32b0f3b43fdbf83510a2 ")) ...{
System.out.println(" true ");
} else
System.out.println(" false ");
}
}
相关文章推荐
- 算法--MD5--MD5加密算法的java实现
- MD5 算法的java实现
- 【JAVA】常用加解密算法总结及JAVA实现【BASE64,MD5,SHA,DES,3DES,AES,RSA】
- JAVA MD5 算法实现
- 加密解密算法java实现(4)—MD5
- 在Java中利用Apache Commons Codec API实现常见的加密解密算法,如:md5,sha256
- java实现的MD5摘要算法完整实例
- 机器学习入门算法及其java实现-朴素贝叶斯
- 中国农历算法java实现
- Java实现算法之Brackets Sequence问题
- 笔试面试算法经典--全排列算法-递归&字典序实现(Java)
- java实现图的最短路径(SP)的迪杰斯特拉(Dijkstra)算法
- java实现图的最短路径(SP)的贝尔曼福特(Bellman-Ford)算法
- PHP实现的MD5结合RSA签名算法实例
- 算法java实现--贪心算法--最优装载问题
- 算法学习(java实现之字符串篇)·····判断字符串是否没有重复字符
- 【LeetCode-面试算法经典-Java实现】【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】
- 算法(第四版)学习笔记之java实现快速排序
- 蓝桥杯算法训练单词接龙(DFS)java实现
- Java实现 Base64、MD5、MAC、HMAC加密