打赏

md5,原理待续

以前项目中copy出来的

import java.security.MessageDigest;

public class MD5Util {
     
    /**
     * @todo MD5加码 生成32位md5码 
     * @author zhangyanan
     * @datetime 2018年1月17日下午4:59:33
     * @param inStr
     * @return
     */
    public static String string2MD5(String inStr){  
        MessageDigest md5 = null;  
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){  
            System.out.println(e.toString());  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }  
  
      
    /**
     * @todo  加密解密算法 执行一次加密,两次解密 
     * @author zhangyanan
     * @datetime 2018年1月17日下午4:59:44
     * @param inStr
     * @return
     */
    public static String convertMD5(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;  
  
    }

   /**
 * @todo 字节数组转换16进制表示的字符串
 * @author zhangyanan
 * @datetime 2018年1月17日下午4:59:54
 * @param b
 * @return
 */
private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

  /**
 * @todo  byte[]数组转换为16进制的字符串
 * @author zhangyanan
 * @datetime 2018年1月17日下午5:00:06
 * @param b
 * @return
 */
private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    /**
     * @todo 生成MD5加密字符串
     * @author zhangyanan
     * @datetime 2018年1月17日下午5:00:18
     * @param origin
     * @param charsetname
     * @return
     */
    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString;
    }

    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    // 测试主函数  
    public static void main(String args[]) {  
        String s = new String("Qyry+2018");  
        System.out.println("原始:" + s);  
        System.out.println("MD5后:" + string2MD5(s));  
        System.out.println("两次MD5后:" + string2MD5(string2MD5("Qyry+2018")));  //aab1df30f62dfa293b94785e76295f05
        System.out.println("加密的:" + convertMD5(s));  
        System.out.println("解密的:" + convertMD5("BBBBBB"));  //VVhseWVTc3lNREU0
    }

}
View Code

 

posted @ 2018-04-18 10:03  每天都要学一点  阅读(130)  评论(0编辑  收藏  举报