本文主要是介绍JavaScript 示例: jsencrypt 私钥 16位密码短语 + cryptojs AES加解密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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>// 第一步:获取“私有秘钥”并保存留用// 以下秘钥通过 new JSEncrypt().getPrivateKeyB64() 获取并将获取的秘钥通过字面量赋值给 privateKeyconst 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==`// 第二步:获取密码短语(16位的字符串)生成的“秘钥”// 以下秘钥通过第一步获取的 privateKey,作为JSEncrypt实例的私有秘钥,之后加密一个16位的字符串(1234567890123456)获得 // t = new JSEncrypt(), t.setPrivateKey(privateKey), t.encrypt('1234567890123456')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='// 通过第一步获取的“私有秘钥”对第二步获取的密码短语“秘钥”进行解密,得到密码短语明文(16位的字符串)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
这篇关于JavaScript 示例: jsencrypt 私钥 16位密码短语 + cryptojs AES加解密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!