PHP菜鸟博客_共同学习分享PHP技术心得【PHP爱好者】
微信小程序md5和后端md5值不一致问题
2025-11-20 菜鸟站长
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>方案 2:crypto-js + UTF-8(.NET 用 UTF-8 时)</title>
<script src="https://cdn.jsdelivr.net/npm/crypto-js@4.2.0/crypto-js.min.js"></script>
</head>
<body>
<h3>方案 2:crypto-js + UTF-8</h3>
<div id="log"></div>

<script>
const targetStr = "";

// 1. UTF-8 编码(对齐 .NET Encoding.UTF8)
const utf8Bytes = CryptoJS.enc.Utf8.parse(targetStr);
const bytesHex = utf8Bytes.toString(CryptoJS.enc.Hex).replace(/(..)/g, "$1-").slice(0, -1);

// 2. 计算 MD5(多格式输出)
const md5Hash = CryptoJS.MD5(utf8Bytes);
const md5_32_lower = md5Hash.toString(CryptoJS.enc.Hex);
const md5_32_upper = md5_32_lower.toUpperCase();
const md5_16_lower = md5_32_lower.slice(0, 16);
const md5_16_upper = md5_16_lower.toUpperCase();

// 输出
const log = `
<p>目标字符:${targetStr}</p>
<p>UTF-8 编码字节:<strong>${bytesHex}</strong>(与 .NET UTF-8 字节对比)</p>
<p>MD5 结果:</p>
<ul>
<li>32位小写:<span style="color:red;">${md5_32_lower}</span></li>
<li>32位大写:${md5_32_upper}</li>
<li>16位小写:${md5_16_lower}</li>
<li>16位大写:${md5_16_upper}</li>
</ul>
`;
document.getElementById("log").innerHTML = log;
console.log("方案2 完整结果:", { bytesHex, md5_32_lower, md5_32_upper, md5_16_lower, md5_16_upper });
</script>
</body>
</html>
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容