PHP中的md5與Java中的md5加密結果不一致問題


/**
     * 獲取MD5加密后的字符串
     * @param str 明文
     * @return 加密后的字符串
     * @throws Exception
     */
    public static String getMD5(String str) throws Exception {
        /** 創建MD5加密對象 */
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        /** 進行加密 */
        md5.update(str.getBytes());
        /** 獲取加密后的字節數組 */
        byte[] md5Bytes = md5.digest();
        String res = "";
        for (int i = 0; i < md5Bytes.length; i++){
            int temp = md5Bytes[i] & 0xFF;
            if (temp <= 0XF){ // 轉化成十六進制不夠兩位,前面加零
                res += "0";
            }
            res += Integer.toHexString(temp);
        }
        return res;
    }

轉自:https://zhidao.baidu.com/question/680167183405269052.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM