码迷,mamicode.com
首页 > 其他好文 > 详细

md5的一些用法

时间:2016-01-19 21:10:15      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

package md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
 * MD5 算法
*/
public class MD5 {
    
    // 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f","+","-" };

    public MD5() {
    }

    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 返回形式只为数字
    private static String byteToNum(byte bByte) {
        int iRet = bByte;
        System.out.println("iRet1=" + iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        return String.valueOf(iRet);
    }

    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }

    public static String GetMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            // md.digest() 该函数返回值为存放哈希值结果的byte数组
            resultString = byteToString(md.digest(strObj.getBytes()));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }
    // 可逆的加密算法   
    public static String KL(String inStr) {   
     // String s = new String(inStr);   
     char[] a = inStr.toCharArray();   
     for (int i = 0; i < a.length; i++) {   
      a[i] = (char) (a[i] ^ ‘t‘);   
     }   
     String s = new String(a);   
     return s;   
    }   
     
    // 加密后解密   
    public static String JM(String inStr) {   
     char[] a = inStr.toCharArray();   
     for (int i = 0; i < a.length; i++) {   
      a[i] = (char) (a[i] ^ ‘t‘);   
     }   
     String k = new String(a);   
     return k;   
    }   
    public static void main(String[] args) {
        MD5 getMD5 = new MD5();
        String str="a";
        System.out.println(getMD5.GetMD5Code(str));
        System.out.println(getMD5.KL(str));
        System.out.println(getMD5.JM(KL(str)));
    }
}

 

 

 

每个用户注册时,给该用户一个guid,然后把这个guid根据自己的算法做些操作,如取其中几位,或把guid做md5,sha之类加密后,再取出6位,把这6位做为密钥,把用户的密码做des加密.
也可以把guid加密后的某些位置的字符拿出来当盐,加到用户密码中,再做md5,sha之类的加密.也可以考虑在全局数组中添加特殊的符号

 

登录验证可以考虑添加用户时生成的md5码进行2次操作,然后保存到数据库, 前端验证时传过md5码,然后自己后台2次操作进行数据库的验证。

md5的一些用法

标签:

原文地址:http://www.cnblogs.com/liangbo-/p/5143314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!