代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DES 加密解密工具(支持中文)</title>
<style>
body {
font-family: Arial, sans-serif;
}
#container {
width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
label {
display: block;
margin-top: 10px;
}
input[type="text"] {
width: 100%;
padding: 5px;
font-size: 16px;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="container">
<h2>DES 加密解密工具(支持中文)</h2>
<label for="plaintext">明文:</label>
<input type="text" id="plaintext">
<label for="encrypted">密文:</label>
<input type="text" id="encrypted">
<label for="password">密码:</label>
<input type="text" id="password">
<button onclick="encrypt()">加密</button>
<button onclick="decrypt()">解密</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script>
function encrypt() {
let plaintext = document.getElementById('plaintext').value;
let key = CryptoJS.enc.Utf8.parse(document.getElementById('password').value);
let cipher = CryptoJS.DES.encrypt(plaintext, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
document.getElementById('encrypted').value = cipher.ciphertext.toString(CryptoJS.enc.Hex);
}
function decrypt() {
let encryptedText = document.getElementById('encrypted').value;
let key = CryptoJS.enc.Utf8.parse(document.getElementById('password').value);
let decipher = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(encryptedText)
}, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
document.getElementById('plaintext').value = decipher.toString(CryptoJS.enc.Utf8);
}
</script>
</body>
</html>
效果
输入密码, 输入明文,点击加密就会生成密文