blog/_layouts/encrypt.html

56 lines
1.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
layout: post
---
<script src="/assets/js/aes.js"></script>
<script>
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (val[i].match(/[^\x00-\xff]/ig) != null) len += 3;
else len += 1;
}
return len;
}
function onbtnDecrypto() {
var encryptedData = "{{ content | strip_html | strip_newlines }}";
var keyword = document.getElementById("inputkey").value;
if (keyword.replace(/(^\s*)|(\s*$)/g, "") == '') {
alert("请输入密码!");
return;
}
while (getByteLen(keyword) % 8 != 0) {
keyword = keyword + "\0";
}
var key = CryptoJS.enc.Utf8.parse(keyword);
var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
if (decryptedData.sigBytes < 0) {
document.getElementById("output").innerHTML = "解密失败密文或者key错误!";
return;
}
try {
decryptedData.toString(CryptoJS.enc.Utf8)
} catch(e) {
document.getElementById("output").innerHTML = "解密失败密文或者key错误!";
return;
}
var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
document.getElementById("output").innerHTML = decryptedStr;
document.getElementById("pwinput").style.display = "none";
}
</script>
<div id="pwinput">{{ page.tips | default: "请输入密码:" }}<br />
<input id="inputkey" type="password" /> <button onclick="onbtnDecrypto()">解密</button>
</div>
<div id="output"></div> <br />