MD5加密工具代碼

找到一個開源的MD5加密工具代碼,收藏起來數組

/**
 * MD5加密工具
 */
public class MD5Utils {

    /**
     * byte[]字節數組 轉換成 十六進制字符串
     * @param arr 要轉換的byte[]字節數組
     * @return String 返回十六進制字符串
     */
    private static String hex(byte[] arr) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; ++i) {
            sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }

    /**
     * MD5加密,並把結果由字節數組轉換成十六進制字符串
     * @param str 要加密的內容
     * @return String 返回加密後的十六進制字符串
     */
    private static String md5Hex(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(str.getBytes());
            return hex(digest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 生成含有隨機鹽的密碼
     * @param password 要加密的密碼
     * @return String 含有隨機鹽的密碼
     */
    public static String getSaltMD5(String password) {
        // 生成一個16位的隨機數
        Random random = new Random();
        StringBuilder sBuilder = new StringBuilder(16);
        sBuilder.append(random.nextInt(99999999)).append(random.nextInt(99999999));
        int len = sBuilder.length();
        if (len < 16) {
            for (int i = 0; i < 16 - len; i++) {
                sBuilder.append("0");
            }
        }
        // 生成最終的加密鹽
        String salt = sBuilder.toString();
        password = md5Hex(password + salt);
        char[] cs = new char[48];
        for (int i = 0; i < 48; i += 3) {
            cs[i] = password.charAt(i / 3 * 2);
            char c = salt.charAt(i / 3);
            cs[i + 1] = c;
            cs[i + 2] = password.charAt(i / 3 * 2 + 1);
        }
        return String.valueOf(cs);
    }

    /**
     * 驗證加鹽後是否和原密碼一致
     * @param password 原密碼
     * @param password 加密以後的密碼
     * @return boolean true表示和原密碼一致 false表示和原密碼不一致
     */
    public static boolean getSaltverifyMD5(String password, String md5str) {
        char[] cs1 = new char[32];
        char[] cs2 = new char[16];
        for (int i = 0; i < 48; i += 3) {
            cs1[i / 3 * 2] = md5str.charAt(i);
            cs1[i / 3 * 2 + 1] = md5str.charAt(i + 2);
            cs2[i / 3] = md5str.charAt(i + 1);
        }
        String Salt = new String(cs2);
        return md5Hex(password + Salt).equals(String.valueOf(cs1));
    }

    public static void main(String[] args) {
        // 原密碼
        String plaintext = "admin";
        // 獲取加鹽後的MD5值
        String ciphertext = MD5Utils.getSaltMD5(plaintext);
        System.out.println("加鹽後MD5:" + ciphertext);
        System.out.println("是不是同一字符串:" + MD5Utils.getSaltverifyMD5(plaintext, ciphertext));
    }
}
相關文章
相關標籤/搜索