<head>
<!-- hexo-ignore-start -->
<style>
.custom-input {
margin-bottom: 20px;
}
.custom-textarea {
width: 100%;
margin-bottom: 20px;
}
</style>
<script src="/tools/aes/crypto-js.min.js"></script>
<!-- hexo-ignore-end -->
</head>
<div class="tools wrap">
<!-- hexo-ignore-start -->
<div class="breadcrumb" itemscope="" itemtype="https://schema.org/BreadcrumbList">
<i class="ic i-home"></i>
<span><a href="/" data-pjax-state=""> 首页 </a></span>
<i class="ic i-angle-right"></i>
<span itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a href="/tools/" itemprop="item" rel="index" title="工具" data-pjax-state=""><span itemprop="name"> 工具 </span></a>
<meta itemprop="position" content="1">
</span>
<i class="ic i-angle-right"></i>
<span class="current" itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem">
<a href="/tools/aes/" itemprop="item" rel="index" title="AES 加解密" data-pjax-state=""><span itemprop="name">AES 加解密 </span></a>
<meta itemprop="position" content="2">
</span>
</div>
<!-- hexo-ignore-end -->
<div class="tools cloud">
<!-- hexo-ignore-start -->
信息:<textarea class="custom-textarea" rows="4" cols="50" id="content"></textarea><br>
秘钥:<textarea class="custom-textarea" rows="4" cols="50" id="encryptionKey"></textarea><br>
是否使用 IV 向量:<input type="radio" id="useIv" value=0 checked onclick="selectUseIv (this.value)" /> 否
<input class="custom-input" type="radio" id="useIv" value=1 onclick="selectUseIv (this.value)" /> 是 < br>
IV 向量:<textarea class="custom-textarea" rows="4" cols="50" id="iv"></textarea><br>
NoPadding:<input type="radio" id="useNoPadding" value=0 checked onclick="selectUseNoPadding (this.value)" /> 否
<input class="custom-input" type="radio" id="useNoPadding" value=1 onclick="selectUseNoPadding (this.value)" /> 是 < br>
加密结果:<textarea class="custom-textarea" rows="4" cols="50" id="value1"></textarea><br>
解密结果:<textarea class="custom-textarea" rows="4" cols="50" id="value2"></textarea><br>
<input type="button" value="加密" onclick="submit (1)" />
<input type="button" value="解密" onclick="submit (0)" />
<script>
// 默认信息
document.getElementById ("content").value = '{"endTime": "","keyword":"", "magnitude": "","pageNum": 1,"pageSize": 10,"startTime":"", "xzqh": "420"}';
// 默认 aes 秘钥
document.getElementById ("encryptionKey").value = "l5gzm2n78wriiqux";
// 加密函数
function encryptAES(text, key, conf) {
// 使用 CryptoJS.AES 对象进行加密
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), CryptoJS.enc.Utf8.parse(key), conf);
return encrypted.toString();
}
// 解密函数
function decryptAES(encryptedText, key, conf) {
// 使用 CryptoJS.AES 对象进行解密
var decrypted = CryptoJS.AES.decrypt(encryptedText, CryptoJS.enc.Utf8.parse(key), conf);
return decrypted.toString(CryptoJS.enc.Utf8);
}
// 提交
function submit(type) {
var encryptionKey = document.getElementById("encryptionKey").value;
// 模式ECB、CBC
// 补码Pkcs7、NoPadding
var conf = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}
// 是否使用NoPadding
var useNoPadding = false;
if (document.querySelector('input[id="useNoPadding"]:checked').value == 1) {
useNoPadding = true;
}
// 判断是否使用iv向量
if (document.querySelector('input[id="useIv"]:checked').value == 1) {
conf.mode = CryptoJS.mode.CBC;
if (useNoPadding) {
conf.padding = CryptoJS.pad.NoPadding;
}
var iv = document.getElementById("iv").value;
if (!iv) {
selectUseIv(1);
iv = document.getElementById("iv").value;
}
if (iv.length != 16) {
alert("iv的长度必须是16位");
return;
}
conf.iv = CryptoJS.enc.Utf8.parse(document.getElementById("iv").value);
}
if (type == 1) {
document.getElementById("value1").value = "";
// 加密
var text = document.getElementById("content").value;
if (useNoPadding) {
const blockSize = 16; // AES块大小为16字节
text = padToBlockSize(text, blockSize);
}
if (!text) {
alert("信息不能为空");
return;
}
document.getElementById("value1").value = encryptAES(text, encryptionKey, conf);
} else {
document.getElementById("value2").value = "";
// 解密
var encryptionText = document.getElementById("value1").value;
if (!encryptionText) {
alert("加密结果不能为空");
return;
}
var decryptText = decryptAES(encryptionText, encryptionKey, conf);
if (useNoPadding) {
decryptText = removePadding(decryptText);
}
document.getElementById("value2").value = decryptText;
}
}
// 选择是否使用iv向量
function selectUseIv(value) {
if (value == 1) {
document.querySelector('input[id="useIv"][value="0"]').checked = false;
if (!document.getElementById("iv").value) {
var randomBytes = CryptoJS.lib.WordArray.random(128 / 8);
var randomHexString = randomBytes.toString(CryptoJS.enc.Hex);
document.getElementById("iv").value = randomHexString.substring(0, 16);
}
} else {
document.querySelector('input[id="useIv"][value="1"]').checked = false;
document.getElementById("iv").value = "";
}
}
// 选择是否使用NoPadding
function selectUseNoPadding(value) {
if (value == 1) {
document.querySelector('input[id="useNoPadding"][value="0"]').checked = false;
} else {
document.querySelector('input[id="useNoPadding"][value="1"]').checked = false;
}
}
// 确保明文长度是块大小的整数倍
function padToBlockSize(data, blockSize) {
const paddingLength = blockSize - (data.length % blockSize);
const padding = String.fromCharCode(paddingLength).repeat(paddingLength);
return data + padding;
}
// 去除填充
function removePadding(plaintext) {
if (plaintext) {
const paddingLength = plaintext.charCodeAt(plaintext.length - 1);
if (paddingLength > 0) {
plaintext = plaintext.slice(0, -paddingLength);
}
}
return plaintext;
}
</script>
<!-- hexo-ignore-end -->
</div>
</div>