HTML5 JS RSA加密解密和AES加密解密
懒人专用,直接下载后解压
https://pan.baidu.com/s/1pUO5tyjpNlOjD48b3n8abg?pwd=s0p9
如下图
一.RSA加密需要的js,点击下面链接可复制
二.AES加密需要的js,点击下面链接可复制
三.加密方式
1.单独使用RSA加密;(不推荐,加密后数据太大了)
2.单独使用AES加密;(不推荐,安全性不好)
3.RSA加密和AES加密混合使用;使用AES加密需要传的参数,使用RSA加密AES的Key;(推荐,安全性好,传输数据又不是很大)
四.使用案例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="js/jsencrypt.js"></script> <script type="text/javascript" src="js/crypto-js.js"></script> </head> <body> <div style="border: 1px solid #55ffff;padding: 10px;"> <div>RSA加密解密</div> <input id="input1" /> <button onclick="RSAJiaMiJieMi1();"> 加密解密 </button> </div> <div style="border: 1px solid #55ffff;padding: 10px;margin-top: 10px;"> <div>AES加密解密</div> <input id="input2" /> <button onclick="AESJiaMiJieMi1()"> 加密解密 </button> </div> <div style="border: 1px solid #55ffff;padding: 10px;margin-top: 10px;"> <div>RSA AES 混合加密解密</div> <input id="input3" /> <button onclick="HunHeJiaMiJieMi1()"> 加密解密 </button> </div> </body> <script type="text/javascript"> //-------------------- RSA 加密解密------------------------------------------------------------ /** * rsa.setPublicKey(key) 配置公钥 * rsa.setPrivateKey(key) 配置私钥 * rsa.encrypt(str) 利用公钥加密 * rsa.decrypt(str) 利用私钥解密 */ //RSA 加密函数 function RSAJiaMi1(str) { // 注册加密方法 // 设置公钥 const publicKey = `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAry69uGIFHZbYFKf+Sich tElKe3kI0xfXqHx641yKIF9AgKM9nrPWGla8Gj8lClC8o+BtVZRmPTXca5pGw5/i T/OubJsglRRZIHJFcMB46W/yoLRcn9Hc02fINIeIdLEDZFlRT37sdPn+9olGL/Qu 83O17nO7yMrryN5adldXpo7Xo1bT7vvS5HKu4bwjtGDwQu4YsLJB+cgdTjNbS//P xzp9MLihkss4fz+r+DHOAHy+GjkhWG4qs+a/pahk7+HJPEOGBM5Hafi0G/is3Nab f4bOljQod5emo+ce3/E4d6ptrROrRoL/ktYnxRXkrFA4QmLKDjll3S7CtEBNV8tv CwIDAQAB` rsa.setPublicKey(publicKey); // 加密 str = rsa.encrypt(str); return str }; //RSA 解密函数 function RSAJieMi1(str) { // 注册加密方法 // 设置私钥 const publicKey = `MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCvLr24YgUdltgU p/5KJyG0SUp7eQjTF9eofHrjXIogX0CAoz2es9YaVrwaPyUKULyj4G1VlGY9Ndxr mkbDn+JP865smyCVFFkgckVwwHjpb/KgtFyf0dzTZ8g0h4h0sQNkWVFPfux0+f72 iUYv9C7zc7Xuc7vIyuvI3lp2V1emjtejVtPu+9Lkcq7hvCO0YPBC7hiwskH5yB1O M1tL/8/HOn0wuKGSyzh/P6v4Mc4AfL4aOSFYbiqz5r+lqGTv4ck8Q4YEzkdp+LQb +Kzc1pt/hs6WNCh3l6aj5x7f8Th3qm2tE6tGgv+S1ifFFeSsUDhCYsoOOWXdLsK0 QE1Xy28LAgMBAAECggEBAKbFQoujlOckUTTo8OY69pUhKKz+/m2nRwD48Cw+Tqie xVMxFFpfLbYJWC1Ma8C0N8Fyx+Mu0V34RoyRZEibjuE6RJTT4WLK6kY/Qugh83mX v01KJttm+GixZiDL3hXo5ifD9+pMFGMQE68z5AOG3KObRllpCB4RQAIL2+bJI4JE 6E0IeYwd5HRBEWvtPxkuvbc43OOQGDtIXVR2kQH4z/vPRfazmAz9w6pij2dsbbNY d3JhihUJ5b8jgU9vpiWgQ9Sj60eXkqPfCKwkauAnMUlS6SB523leB/c6UG2fgBTd 44HHUAvaE5l3IEzURUGELiMg6ajJUxWWL2LK/T9RD6ECgYEA7H4dYq+cCt7kboXC hq2U4s/EXpgnQTVDHQ1spfh0JxPs+KsH43p4x0JCrr5kt3zJ3PO8DN9P292GY+mi MSo/pU0bQrW2dNyGcjt96UaAus6J8ngl31Lsm6Sk9ncA9VUYiMk3Wyrse8TmWjZY hW98DbsVJBqhwEiZjANBPaTCG7kCgYEAvaH5vdgU+i3LN+kn7STwpDG0vl6jbRyW sQqB5kJYd6XfNSQzHYAu/ODi6wxvxJL0LuDYPDkGsd5W9+QZvlUjQ0VZv4P2s0ex YQpFPuDmH+ljypli60Gp3SzksBhzNJmUcdyaTQ7zE9y631MgM62+WLvP4Zr7vccH 5yRvUS1kquMCgYEA3E0XHqksGUxNS0+7SC5G9YpKCTx/xNTS8ithPLLXsNd09qiM LrIV9Lia2aFuvSa3/OXoFa2o/M29tTEc7+PFaj0L2fbgQrhzXdJhln8gx+RCyOZ3 kTP0SYD0UsvEzsU4l6Opt3mi1iEz3HGnwJV74GaYaQuOBksbDjyx97SVRbECgYEA u4R+VzJjm/jIqaMPbjtHErZsSot/A5hvkuYXCcC1hqspi30eZp+o1zryaQfFRhcj C3Afo32olx6L1Vlj84hw9EaRtwYhX6sXRiRH0SPHq+q4WVFQNd6awDFyeofS9EKi Pe2tT2HnEM8Go2upvjCit1ARAsClC0bTE8g9o4JFt40CgYEAydf036LJx/KmGrky +OYww4ZOPNrZDVscJuZOSTTd69WlEbspC3PK+EGthLmZlyXFf4/Ycq4B7+uBlfls JokYmKgFzNB/tRauXHk4ny/CDNoDR6L78v7GOin5eaB1Gyv9vPnU1h0oYbgTx3GL rBTc/4ZFikWLdz9IvsccD/DLyKQ=`; rsa.setPrivateKey(publicKey); // 解密 str = rsa.decrypt(str); return str }; //调用 RSA 加密解密函数 function RSAJiaMiJieMi1() { var a1 = document.getElementById('input1').value; //获取input1需要解密的数据 var a2 = RSAJiaMi1(a1); //加密 console.log('RSA加密后数据:', a2); var a3 = RSAJieMi1(a2); //解密 console.log('RSA解密后数据:', a3); }; //-------------------- AES 加密解密------------------------------------------------------------ //AES 加密函数 function AESJiaMi1(word, keyStr) { keyStr = keyStr ? keyStr : "1234567890123456"; var ivStr = "1234567890123456"; return CryptoJS.AES.encrypt(word, CryptoJS.enc.Utf8.parse(keyStr), { iv: CryptoJS.enc.Utf8.parse(ivStr), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }).toString() }; //AES 解密函数 function AESJieMi1(word, keyStr) { keyStr = keyStr ? keyStr : "1234567890123456"; var ivStr = "1234567890123456"; let decrypted = CryptoJS.AES.decrypt(word, CryptoJS.enc.Utf8.parse(keyStr), { iv: CryptoJS.enc.Utf8.parse(ivStr), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8) }; //调用 AES 加密解密函数 function AESJiaMiJieMi1(word, keyStr) { var a1 = document.getElementById('input2').value; //获取input2需要解密的数据 var a2 = AESJiaMi1(a1); //加密 console.log('AES加密后数据:', a2); var a3 = AESJieMi1(a2); //解密 console.log('AES解密后数据:', a3); }; //-------------------- RSA AES 混合加密解密------------------------------------------------------------ //用于获取 16 位AES加密的 keyStr function JiaMiSuiJiShu1(e) { e = e || 16; var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678", a = t.length, n = ""; for (var i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a)); return n }; //混合加密解密 function HunHeJiaMiJieMi1() { var a1 = JiaMiSuiJiShu1(); //获取 16 位AES加密的 keyStr console.log('AES加密的16位keyStr:', a1); var a2 = document.getElementById('input3').value; //获取input3需要解密的数据 var a3 = AESJiaMi1(a2, a1); //使用随机获取的 keyStr 加密数据 console.log('AES加密后数据:', a3); var a4 = RSAJiaMi1(a1); //使用 RSA 加密随机获取的 AES 加密的 keyStr console.log('RSA加密AES的16位keyStr:', a4); var data = { data: a3, // keyStr: a4, // } HunHeJiaMiJieMi2(data) }; function HunHeJiaMiJieMi2(data) { console.log('传过来需要解密的参数',data); var a1 = RSAJieMi1(data.keyStr); //使用RSA解密出AES的keyStr console.log('使用RSA解密出AES的keyStr:', a1); var a2 = AESJieMi1(data.data, a1); //使用解密出AES的keyStr解密参数 console.log('AES解密出来的数据:', a2); }; </script> </html>