Java,Android,MD5加密與SHA加密算法(含工具類)

根據不同數據執行加密,先貼出常用的加密算法

/**
  * MD5加密,32/16位小寫
  * @param sSecret
  * @return
  */
public static String getMd5small(String sSecret) {
        try {
            MessageDigest bmd5 = MessageDigest.getInstance("MD5");
            bmd5.update(sSecret.getBytes());
            int i;
            StringBuffer buf = new StringBuffer();
            byte[] b = bmd5.digest();
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
                return buf.toString();        //或者 return buf.toString().substring(8,24);   //16位  
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

下面是EncryptUtils加密工具類代碼

public class EncryptUtils {

    /**
     * MD5加密
     *
     * @param data 明文字符串
     * @return 密文
     */
    public static String getMD5(String data) {
        return getMD5(data.getBytes());
    }



    /**
     * MD5加密
     *
     * @param data 明文字符串
     * @param salt 鹽
     * @return 密文
     */
    public static String getMD5(String data, String salt) {
        return bytes2Hex(encryptMD5((data + salt).getBytes()));
    }

    /**
     * MD5加密
     *
     * @param data 明文字節數組
     * @return 密文
     */
    public static String getMD5(byte[] data) {
        return bytes2Hex(encryptMD5(data));
    }

    /**
     * MD5加密
     *
     * @param data 明文字節數組
     * @param salt 鹽字節數組
     * @return 密文
     */
    public static String getMD5(byte[] data, byte[] salt) {
        byte[] dataSalt = new byte[data.length + salt.length];
        System.arraycopy(data, 0, dataSalt, 0, data.length);
        System.arraycopy(salt, 0, dataSalt, data.length, salt.length);
        return bytes2Hex(encryptMD5(dataSalt));
    }

    /**
     * MD5加密
     *
     * @param data 明文字節數組
     * @return 密文字節數組
     */
    public static byte[] encryptMD5(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(data);
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    /**
     * 獲取文件的MD5校驗碼
     *
     * @param filePath 文件路徑
     * @return 文件的MD5校驗碼
     */
    public static String getMD5File(String filePath) {
        FileInputStream in = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            in = new FileInputStream(filePath);
            int len;
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            return bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            }
        }
        return "";
    }


    /**
     * SHA加密
     *
     * @param data 明文字符串
     * @return 密文
     */
    public static String getSHA(String data) {
        return getSHA(data.getBytes());
    }

    /**
     * SHA加密
     *
     * @param data 明文字節數組
     * @return 密文
     */
    public static String getSHA(byte[] data) {
        return bytes2Hex(encryptSHA(data));
    }

    /**
     * SHA加密
     *
     * @param data 明文字節數組
     * @return 密文字節數組
     */
    public static byte[] encryptSHA(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(data);
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    /**
     * 一個byte轉爲2個hex字符
     */
    public static String bytes2Hex(byte[] src) {
        char[] res = new char[src.length * 2];
        final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        for (int i = 0, j = 0; i < src.length; i++) {
            res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
            res[j++] = hexDigits[src[i] & 0x0f];
        }
        return new String(res);
    }


}

這裏有源文件大家可以去下載哦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章