﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-叮当小马的JavaBlog-文章分类-Encryption</title><link>http://www.blogjava.net/dingdangxiaoma/category/30901.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 18 Apr 2008 08:14:28 GMT</lastBuildDate><pubDate>Fri, 18 Apr 2008 08:14:28 GMT</pubDate><ttl>60</ttl><item><title>java MD5加密简单实现</title><link>http://www.blogjava.net/dingdangxiaoma/articles/194028.html</link><dc:creator>叮当小马</dc:creator><author>叮当小马</author><pubDate>Fri, 18 Apr 2008 06:17:00 GMT</pubDate><guid>http://www.blogjava.net/dingdangxiaoma/articles/194028.html</guid><wfw:comment>http://www.blogjava.net/dingdangxiaoma/comments/194028.html</wfw:comment><comments>http://www.blogjava.net/dingdangxiaoma/articles/194028.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dingdangxiaoma/comments/commentRss/194028.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dingdangxiaoma/services/trackbacks/194028.html</trackback:ping><description><![CDATA[<span style="color: red;">Edited by DingDangXiaoMa</span><br />
java MD5加密简单实现:<br />
<br />
源码如下,并有注释:<br />
<br />
package com.zh.test.util;<br />
<br />
import
java.security.MessageDigest;<br />
import
java.security.NoSuchAlgorithmException;<br />
<br />
/**<br />
* 加密类．对输入的字符串进行加密．<br />
*
<br />
* @author Administrator<br />
* <br />
*/<br />
public class MyEncrypt {<br />
&nbsp; &nbsp; &nbsp; &nbsp;
/**<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 对字符串进行加密处理.用到的算法是JDK 1.5中的 MD5算法 . MD5是一个不可逆的算法．<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*
<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param toEncrypt<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @throws
Exception<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
&nbsp; &nbsp; &nbsp; &nbsp; public static String encrypt(String
toEncrypt) throws Exception {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String inStr = toEncrypt;<br />
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageDigest md = null;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String out =
null;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("转变之前的长度: " +
inStr.getBytes().length);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; md
= MessageDigest.getInstance("MD5");<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] digest =
md.digest(inStr.getBytes());<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out =
byte2hex(digest);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchAlgorithmException e) {<br />
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw
e;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return out;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp;
&nbsp; &nbsp; &nbsp; /**<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* 把二进制数组转换成十六进制.<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param
b<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @return<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<br />
&nbsp; &nbsp; &nbsp; &nbsp; private static String
byte2hex(byte[] b) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("数组长度; " +
b.length);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String hs = "";<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String stmp =
"";<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int n = 0; n &lt; b.length; n++) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmp = (java.lang.Integer.toHexString(b[n] &amp; 0XFF));<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stmp.length() == 1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hs =
hs + "0" + stmp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; hs = hs + stmp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return hs.toUpperCase();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
&nbsp; &nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;* 对加密算法进行测试.<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param args<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;参数<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 异常处理.<br />
&nbsp; &nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;*/<br />
&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) throws Exception {<br />
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String toEncrypt = "123456";<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String toEncrypt2
= "aaa1112您好好`11887444....---00022";<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String outString =
MyEncrypt.encrypt(toEncrypt2);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
System.out.println(outString);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
System.out.println("2C1835B7872CAB44D5D87312B85D829A".length());<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;
}<br />
}<br />
<br />
以上部分是对MD5算法的简单实现.网上也有很多这样的资料,谁有兴趣,可以找更多的资料,<br />
从网上看,说MD5是一个不可逆的运算,也不知道是不是这样?<br />
可以参考网站:<a href="http://www.moon-soft.com/doc/2624.htm" target="_blank">http://www.moon-soft.com/doc/2624.htm</a><br />
<img src ="http://www.blogjava.net/dingdangxiaoma/aggbug/194028.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dingdangxiaoma/" target="_blank">叮当小马</a> 2008-04-18 14:17 <a href="http://www.blogjava.net/dingdangxiaoma/articles/194028.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>