JavaScript 示例: jsencrypt 私钥 16位密码短语 + cryptojs AES加解密
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript 示例: jsencrypt 私钥 16位密码短语 + cryptojs AES加解密</title>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.2.1/jsencrypt.min.js"></script>
<script>
const privateKey = `MIICWwIBAAKBgQCF5SRovhqNKrl2YtApM1OH9JcXF99NtjHa2zHIqMpFiHTW8fiplcIJIrMENqhwz/lT1ArfDrU0yvFX1H1xKKkd8qTts7ysEx5sl+gO4v8KNyP5NnMZ5qmpGO2Kwa67tRyl0JiU0hIGueTegyXAzalfD3TZ3wyckY8q0w93oYGf0QIDAQABAoGAN+CWvzeGsR8BYwx9vpPy6iwC2dpWkIHWcHVFY249a6M0z3cPxQYAgft/JeUBioLglEFLgCcNLEj9SyAccZr8aCra0DDI6IrjVBlHd4OYP1j51hpRM4D0A+opI2D08lD40jFHfc+iVBBfEuo/yiN5s8I1Ppcq8RfCstobMUJ2w5kCQQDmbJb9UjzSaS4xSHS1vHKA6gDZqQk91N3q3H+B/mPOvzzUvYs0F9/B37mKWLX7QQNgTTOHKQrpeX+d7kz/CT+nAkEAlMG3mAW+kpKyEWATribNijkBXZA4lqO9BpgnEPW/DKfst7KvZLqsrmuDfzmKnQ5qD+3tegBu57eEyTTnVqJTxwJAG1NtZIVIKTvH+pg5HwRGH/ugNi/RpL+FwcB8bpIYjAAp2qDPzVG3e2pountiuXUwTU9aeQDpBl8eP0BvaR9TqQJABhQcYnPpo6em6xrX8wfW4k0OBzq7dTxfnwNcpU7Fbcj23VLJ4/3UzJPMHcjGJ0DNiLUaPSADaCiRnn8w7NNZRQJAJyEb+SE4Niies7dZt6dKHNCQErSw0rJhVPc4hEV/QdBJ7rUiBPMdHkz2BRfCizcPrCoY7gGQdEHYS6Dn6bThkA==`
let key = "eixa7bmeSU6LTWTSakyT90gaswNxoHBqQBNJHSL04ytasyAoNqc+g5IqvTRsZsiCA4/jawB/ynbH8PDtZi3LwUeHT4bCjo3eFfuG/3umB37Q/JqOK9LI2i8/PfDUSN9At/2g/PpWlfceTUkGX5RbcAaRV00yAJ9aDnkxuay+D2Q="
let json = '{"isHeavyNet":"","linkUsage":"","telephoneNum":"138****7650","licenseOwner":"贾*贵","idNumber":"","orderTempSource":""}'
let cipherText = 'N6SblKJZMlFY43UiNQepQLO1lsya9Nvkh+HAdw3FOpWthGZyQfjIuR2k92lPNuzSsiV/mU6hOC4HK81mLVvj4kUAj0c4e7VIVy1jxcT6TpOXLsLIQFgrnaZDs0dNsDdmUaSrmy6Y0WJFTRGoS2G+dpqDY5Qq87FawHIaGsuUBSI='
function getSecretPassphrase(e) {
var t = new JSEncrypt()
t.setPrivateKey(privateKey)
return t.decrypt(e)
}
function encipher(e, t) {
var a = CryptoJS.enc.Utf8.parse(getSecretPassphrase(t))
var n = CryptoJS.enc.Utf8.parse(e)
var i = CryptoJS.AES.encrypt(n, a, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return CryptoJS.enc.Base64.stringify(i.ciphertext)
}
console.log('加密密文:', encipher(json, key))
function decipher(e, t) {
var a = CryptoJS.enc.Utf8.parse(getSecretPassphrase(t))
var n = CryptoJS.AES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(e)
}, a, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return n.toString(CryptoJS.enc.Utf8)
}
console.log('解密明文:', decipher(cipherText, key))
</script>
</body>
</html>
CryptoJS Doc
crypto-js Npm
JSEncrypt Doc
jsencrypt Npm