1 <html> 2 <head> 3 <title>JavaScript RSA Encryption</title> 4 <meta charset
="UTF-8"> 5 <script src="js/jquery-1.11.3.min.js"></script> 6 <script src
="js/jsencrypt.min.js"></script> 7 </head> 8 <body style="text-align: center;"
> 9 <label for="privkey">私钥</label> 10 <br/> 11 <textarea id="privkey" rows
="15" cols="65">
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALwlTWcENnk+BE0DGSxML8yVoFffu+NMfzimnOa4Skft7objdBd3tmh/xqkgnh5+TpSLDb84l+TIwYOwAlO//Nae4qbpR7IFjG2A/kkUa1MwpMFW4bMfxTNhZ0/PJsg4KaPnlCS7edgRnKujfx0aFIUtI9pTd6hjW1uWblkUn7x7AgMBAAECgYEAsoOaK1Jd9S+YmRGFgvhOqgiV+Ip6FTJxy2iamnblU9Y3aZjKf7HASuphrfsay8f/+wTs6DBkPlkU80O/EYOp+r46UIKq5KChW2ErUVaMyxO4f8l0PW2RFHFYWq54SbRa73DepPeC3o9LRinEaJ7/rk13EHiS6RAPkFr/ZyTOYdECQQD0BNI4Wg4gJqZHC1QCHOQUaF2cDWh5cadWT9FOXKdgRUrP+2W67BJP/RjNPT++/cyP3pdNc8vMCkF8IGgJ7Pk/AkEAxWIwCK3prAxupNrlUhPNT+nfG05Bs0SYIQHSiwUykXkcUZAcAveNk2g8XlhAQ4fEZG9BQ0+MlA246FV90NlRxQJAJEm7QWoTA7D7tUD4A0BsoqRKl3Re/wBp1CoWhEK/GU/0qi1GmZ+VsZN/5rWfDzYK/7Ioafo4amV0C5N+JUu6XQJANbDuxf2guYcsNbXBeWEfkSRZ5xCTeJMpGplnTx+b0Mxz8o/Tvs7d+QH6oeM5n+T3fCZzRz8FS2SsLq7KqZvjzQJADLfcfxaYd3V7e0/Op08ifDyCjpLa3rviNFss/msOR3Mzfg7J05wD3oVHut+g+RVF7+Gnia07pV5YnFVd0WA1UA==
</textarea> 12 <!--<br/>--> 13 <label for="pubkey">公钥</label> 14 <!--<br/>--> 15
<textarea id="pubkey" rows="15" cols="65">
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8JU1nBDZ5PgRNAxksTC/MlaBX37vjTH84ppzmuEpH7e6G43QXd7Zof8apIJ4efk6Uiw2/OJfkyMGDsAJTv/zWnuKm6UeyBYxtgP5JFGtTMKTBVuGzH8UzYWdPzybIOCmj55Qku3nYEZyro38dGhSFLSPaU3eoY1tblm5ZFJ+8ewIDAQAB
</textarea> 16 <br/> 17 <label for="input">要加密的字符串:</label> 18 <br/> 19 <
textareaid="input" name="input" type="text" rows='4' cols='70'>123456789</
textarea> 20 <!--<br/>--> 21 <input id="testme" type="button" value="Test Me!!!"
/> 22 <br/> 23 <label for="input">加密后的字符串:</label> 24 <br/> 25 <textarea id
="jiami" name="jiami" type="text" rows='4' cols='70'></textarea> 26 <br/> 27 <
labelfor="pubkey">解密后的密文</label> 28 <br/> 29 <textarea id="jiemi" name="jiemi"
type="text" rows='4' cols='70'></textarea> 30 </body> 31 </html>
js部分:
1 <script type="text/javascript"> 2 $(function() { 3 $('#testme').click(
function() { 4 // 加密前的原文 5 console.log('这是原文' + $('#input').val()) 6 //
进行RSA加密 7 var encrypt = new JSEncrypt(); 8 encrypt.setPublicKey($('#pubkey'
).val()); 9 var encrypted = encrypt.encrypt($('#input').val()); 10 // 加密后的密文 11
console.log('这是加密之后的' + encrypted); 12 $('#jiami').val(encrypted); 13 14 //解密 15
var decrypt = new JSEncrypt(); 16 decrypt.setPrivateKey($('#privkey').val()); 17
var uncrypted = decrypt.decrypt(encrypted); 18 console.log('这是解密后的密文' +
uncrypted);19 $('#jiemi').val(uncrypted); 20 }); 21 }); 22 </script>
jsencrypt <https://github.com/travist/jsencrypt>.min.js下载地址:

[github主页](https://github.com/travist/jsencrypt
<https://github.com/travist/jsencrypt>  )    
[官方网站](http://travistidwell.com/jsencrypt/
<http://travistidwell.com/jsencrypt/>  )   jq库: <script src="
http://code.jquery.com/jquery-1.8.3.min.js
<http://code.jquery.com/jquery-1.8.3.min.js>"></script>
(注!jquery-2.0以上版本不再支持IE 6/7/8) 并不是最新的版本就最好的,而是根据您项目需求所适合的版本!

 

最终效果: