[Javascript]实现RSA非对称加密和Java版本算法互通

2021-04-20 2197点热度 0人点赞 0条评论

前言

在前一篇介绍了RSA非对称加密,以及使用Java实现RSA加密:https://blog.terrynow.com/2021/04/14/java-tool-rsa-encrypt-implement/

本篇将介绍Javascript下如何实现(同样加解密结果在Java中互通),使用到的是Github上开源的jsencrypt:https://github.com/travist/jsencrypt

生成密钥对(公钥和私钥)

Javascript下无法生成密钥对,可以使用https://blog.terrynow.com/2021/04/14/java-tool-rsa-encrypt-implement/这里介绍的使用Java代码生成密钥对,或者在Linux环境,使用openssl命令生成:

  • 进入Linux终端,生成私钥
openssl genrsa -out rsa_1024_priv.pem 1024
  • 查看私钥内容
cat rsa_1024_priv.pem
  • 利用私钥生成公钥.
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
  • 查看公钥内容
cat rsa_1024_pub.pem

JavascriptRSA加解密实现

加密方法和测试代码(公钥和私钥也前面Java版本的文章一直,可互通):

<!doctype html>
<html>
  <head>
    <title>JavaScript RSA Encryption</title>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <!--去 https://raw.githubusercontent.com/travist/jsencrypt/master/bin/jsencrypt.min.js 下载-->
    <script src="jsencrypt.min.js"></script>
    <script type="text/javascript">

      // Call this code when the page is done loading.
      $(function() {

        // Run a quick encryption/decryption when they click.
        $('#testme').click(function() {

          // 使用公钥加密...
          var encrypt = new JSEncrypt();
          encrypt.setPublicKey($('#pubkey').val());
          var encrypted = encrypt.encrypt($('#input').val());
          $('#output').val(encrypted)

          // 使用私钥解密...
          var decrypt = new JSEncrypt();
          decrypt.setPrivateKey($('#privkey').val());
          var uncrypted = decrypt.decrypt(encrypted);

          // Now a simple check to see if the round-trip worked.
          if (uncrypted == $('#input').val()) {
            alert('成功!!!');
          }
          else {
            alert('发生错误....');
          }
        });
      });
    </script>
  </head>
  <body>
    <label for="privkey">Private Key</label><br/>
    <textarea id="privkey" rows="15" cols="65">
        -----BEGIN RSA PRIVATE KEY-----
        MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJlTlMmhFxhVZ/U6xd3
        TZTelu0g8a+tZYAqoOegsJXXlelthlLTzx5qkWEf4STjIMaIYtduIO1xVs1L3Gsjyl3
        IhofjuVwI2qk2QVCtuVXkDvO/gPXCzur5iTDCG7Jrh5jJI5VtaVGHVH37TSWUYiowKv
        SzdKViPQ1h1J/BPewI5AgMBAAECgYAwBq4kOgEx7g6SEcbIpN6ff5oaFlL3dRdQP0Fk
        J+/T7f1VgktCItny5meAo5TL5GknjPXpvdJaKb3xztTk/JfS+X4uZBuEumb+UV8hWzJ
        0hvxIMtVf0rwFLbEB6btiNJHDCIv2+eJSEoK2RjYF/qGb0zzFz1YGICY5Vjl2TDnlwQ
        JBAPHbqEWKT1KeEXKzNBcZadglfjF+qXQmWFGN3yUrd8Xxmet+oKfhXgPgybfo8/uwM
        6ijJaEYNWWU6wbvMf9jkZ8CQQCiSrTLyzEe9pObKpwGk3U2jkaYOnSKdMYebxvF9BGN
        CnLsen0rDOefgHGNwUMphzWaqWESnb1TB7Mck/kanU0nAkEA3D1Zg1HKoAI/A0L62Vo
        UD3c87hdYeeN9I27c1/04SXGT52XdKGNKeJ+XMTkaDITM1fETEBp6bXFpgFwLHHRwIw
        JBAKExPFOyNM7BMCiM8L2jVmdidwGaBcjDZUgzKU27Nnd06mKw4oOQ5LnKtH6Mdy3XK
        sm79DuuSq4eVqtK2b+yfz8CQQC7frRjmYPe9+2JrTWMbzfFlLysCn7xmdbW5rVCs8sO
        5jq8YxW5nV99IVGaKjYw6wcjIGut9uZw6Mmec05CODzh
        -----END RSA PRIVATE KEY-----
    </textarea><br/>
    <label for="pubkey">Public Key</label><br/>
    <textarea id="pubkey" rows="15" cols="65">
        -----BEGIN PUBLIC KEY-----
        MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZU5TJoRcYVWf1OsXd02U3pbtIPGv
        rWWAKqDnoLCV15XpbYZS088eapFhH+Ek4yDGiGLXbiDtcVbNS9xrI8pdyIaH47lcCNq
        pNkFQrblV5A7zv4D1ws7q+Ykwwhuya4eYySOVbWlRh1R9+00llGIqMCr0s3SlYj0NYd
        SfwT3sCOQIDAQAB
        -----END PUBLIC KEY-----
    </textarea><br/>
    <label for="input">代加密文本:</label><br/>
    <textarea id="input" name="input" type="text" rows=4 cols=70>This is a test!</textarea><br/>
    <label for="input">加密后文本:</label><br/>
    <textarea id="output" name="output" type="text" rows=4 cols=70></textarea><br/>
    <input id="testme" type="button" value="测试" /><br/>
  </body>
</html>

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论