MD5 加密 java代码实现

 1 package com.company.fjf;
 2 
 3 import java.security.MessageDigest;
 4 import java.security.NoSuchAlgorithmException;
 5 
 6 public class MD5Test {
 7     private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
 8             "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
 9 
10     public MD5Test() {
11     }
12 
13     // return Hexadecimal
14     private static String byteToArrayString(byte bByte) {
15         int iRet = bByte;
16         if (iRet < 0) {
17             iRet += 256;
18         }
19         int iD1 = iRet / 16;
20         int iD2 = iRet % 16;
21         return strDigits[iD1] + strDigits[iD2];
22     }
23 
24     // 转换字节数组为16进制字串
25     private static String byteToString(byte[] bByte) {
26         StringBuffer sBuffer = new StringBuffer();
27         for (int i = 0; i < bByte.length; i++) {
28             sBuffer.append(byteToArrayString(bByte[i]));
29         }
30         return sBuffer.toString().toUpperCase();
31     }
32 
33     public static String GetMD5Code(String strObj) {
34         String resultString = null;
35         try {
36             resultString = new String(strObj);
37             MessageDigest md = MessageDigest.getInstance("MD5");
38             // md.digest() 该函数返回值为存放哈希值结果的byte数组
39             resultString = byteToString(md.digest(strObj.getBytes()));
40         } catch (NoSuchAlgorithmException ex) {
41             ex.printStackTrace();
42         }
43         return resultString;
44     }
45 
46     public static void main(String[] args) {
47         System.out.println(MD5Test.GetMD5Code("aaaa"));
48     }
49 }

 

转载于:https://www.cnblogs.com/zdfjf/p/5920644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值