在項目中遇到要對用戶輸入的密碼進行RSA加密的需求,總結一下實現(xiàn)過程:
<html> <head> <meta charset="utf-8" /> <title>JS rsa加密</title> </head> <body> <p> <input type="text" id="pwd" placeholder="請輸入密碼"/><br /> <input type="text" id="key1" placeholder="請輸入modulus參數(shù)"/><br /> <input type="text" id="key2" placeholder="請輸入exponent參數(shù)"/> <button id="btn">加密</button><br /> <input type="text" id="pwd1" placeholder="加密后"/> </p> <script type="text/javascript" src="../RSA加密/security.js"> //引入security.js文件 </script> <script> var btn = document.getElementById('btn'); btn.onclick = function(){ var pwd = document.getElementById('pwd').value; var modulus = document.getElementById('key1').value; var exponent = document.getElementById('key2').value; //加密 var key = RSAUtils.getKeyPair(exponent, "", modulus); var apwd = RSAUtils.encryptedString(key, pwd); //加密后的密碼; document.getElementById('pwd1').value = apwd; } </script> </body> </html>
這里的exponent參數(shù)和modulus參數(shù)講道理是要從后臺獲取的,這里寫做輸入框獲取是作測試用。
相信看了本文案例你已經掌握了方法,更多精彩請關注Gxl網(wǎng)其它相關文章!
推薦閱讀:
vue做出可搜索下拉框
字符串+數(shù)組去重實戰(zhàn)案例解析
聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com