MD5加密
Maven写入以下坐标
1 <dependency> 2 <groupId>commons-codec</groupId> 3 <artifactId>commons-codec</artifactId> 4 <version>1.6</version> 5 </dependency>
方法一:
使用工具DigestUtils
1 String s = DigestUtils.md5Hex(password.getBytes());
方法二:
1 import java.math.BigInteger;
2 import java.security.MessageDigest;
3 import java.security.NoSuchAlgorithmException;
4 /**
5 *
6 * @author wjxing
7 *
8 */
9 public class MD5{
10 /**
11 * 使用md5的算法进行加密
12 */
13 public static String md5(String plainText) {
14 byte[] secretBytes = null;
15 try {
16 secretBytes = MessageDigest.getInstance("md5").digest(
17 plainText.getBytes());
18 } catch (NoSuchAlgorithmException e) {
19 throw new RuntimeException("没有md5这个算法!");
20 }
21 String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
22 // 如果生成数字未满32位,需要前面补0
23 for (int i = 0; i < 32 - md5code.length(); i++) {
24 md5code = "0" + md5code;
25 }
26 return md5code;
27 }
28
29 /**
30 * 可逆的的加密解密方法;两次是解密,一次是加密
31 * @param inStr
32 * @return
33 */
34 public static String convertMD5(String inStr){
35
36 char[] a = inStr.toCharArray();
37 for (int i = 0; i < a.length; i++){
38 a[i] = (char) (a[i] ^ 't');
39 }
40 String s = new String(a);
41 return s;
42
43 }
44 public static void main(String[] args) {
45 String s = md5("1234");
46 System.out.println("MD5后:"+s);
47 System.out.println("MD5后再加密:"+convertMD5(s));
48 System.out.println("MD5加密后解密:"+convertMD5(convertMD5(s)));
49 String s2 = convertMD5("12345");
50 System.out.println("可逆的加密解密方法之加密:"+s2);
51 System.out.println("可逆的加密解密方法之解密:"+convertMD5(s2));
52 }
53 }

方法三:
这个是在建民老师的电商项目找到
1 mport java.security.MessageDigest;
2 import java.security.NoSuchAlgorithmException;
3 /**
4 *
5 * @author wjxing
6 *
7 */
8 public class MD5 {
9
10 // 全局数组
11 private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
12
13 public MD5() {
14 }
15
16 // 返回形式为数字跟字符串
17 private static String byteToArrayString(byte bByte) {
18 int iRet = bByte;
19 // System.out.println("iRet="+iRet);
20 if (iRet < 0) {
21 iRet += 256;
22 }
23 int iD1 = iRet / 16;
24 int iD2 = iRet % 16;
25 return strDigits[iD1] + strDigits[iD2];
26 }
27
28 // 返回形式只为数字
29 @SuppressWarnings("unused")
30 private static String byteToNum(byte bByte) {
31 int iRet = bByte;
32 System.out.println("iRet1=" + iRet);
33 if (iRet < 0) {
34 iRet += 256;
35 }
36 return String.valueOf(iRet);
37 }
38
39 // 转换字节数组为16进制字串
40 private static String byteToString(byte[] bByte) {
41 StringBuffer sBuffer = new StringBuffer();
42 for (int i = 0; i < bByte.length; i++) {
43 sBuffer.append(byteToArrayString(bByte[i]));
44 }
45 return sBuffer.toString();
46 }
47
48 public static String GetMD5Code(String strObj) {
49 String resultString = null;
50 try {
51 resultString = new String(strObj);
52 MessageDigest md = MessageDigest.getInstance("MD5");
53 // md.digest() 该函数返回值为存放哈希值结果的byte数组
54 resultString = byteToString(md.digest(strObj.getBytes()));
55 } catch (NoSuchAlgorithmException ex) {
56 ex.printStackTrace();
57 }
58 return resultString;
59 }
60
61 }
最后,你加密后,数据库存的密码是加密过的,但是你不用写代码去解密后台验证登录,你的登陆密码和加密密码可以自动匹配,就算不一致,他也认为一致,在数据库中找到并允许登录。

浙公网安备 33010602011771号