学习所得

一般的用户数据库是不用明文存入数据库的,像password这样的,都用md5加密一次之后才存入数据库的。
md5的转行代码:
 1  import java.security.MessageDigest;
 2  import java.security.NoSuchAlgorithmException;
 3 
 4  public class MD5 {
 5     private static String byte2Hex(byte b) {
 6          int value = (b & 0x7F+ (b < 0 ? 0x80 : 0);
 7          return (value < 0x10 ? "0" : "")
 8                  + Integer.toHexString(value).toLowerCase();
 9      }
10 
11      public static String MD5_32(String passwd) throws NoSuchAlgorithmException {
12          MessageDigest md5 = MessageDigest.getInstance("MD5");
13          StringBuffer strbuf = new StringBuffer();
14 
15          md5.update(passwd.getBytes(), 0, passwd.length());
16          byte[] digest = md5.digest();
17         for (int i = 0; i < digest.length; i++) {
18              strbuf.append(byte2Hex(digest[i]));
19          }
20          return strbuf.toString();
21      }
22 
23  public static void main(String[] args) {
24         try {
25              System.out.println(MD5_32("000"));
26          } catch (NoSuchAlgorithmException e) {
27              e.printStackTrace();
28          }
29      }
30 }

posted on 2009-02-03 22:34 duduli 阅读(143) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

导航

统计

公告

welcome to my place.

常用链接

留言簿(5)

我参与的团队

随笔分类

随笔档案

新闻分类

石头JAVA摆地摊儿

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

@duduli