旅行青蛙
Published on 2023-06-17 / 2,865 Visits
0
0

JSEncrypt

1.优点:
支持rsa加密解密,支持标准的rsa密钥;比jsrsasign加密速度稍微快几毫秒;加密内容可以用nodejs标准的加密模块crypto解密

2.缺点:
不能生产rsa密钥;

3.安装:

Web端

html文件引入jsencrypt.min.js(https://github.com/travist/jsencrypt)

Nodejs端:

npm i node-jsencrypto

nodejs端node-jsencrypt


var crypto = require('crypto');

date1 = new Date().getTime()
var { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
    modulusLength: 512,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
    },
    privateKeyEncoding: {
        type: 'pkcs1',
        format: 'pem',
        // cipher: 'aes-256-cbc',
        // passphrase: 'top secret'
    }
});
console.log(new Date().getTime() - date1)
// 引入加密模块
JSEncrypt = require('node-jsencrypt')
// 加密
date1 = new Date().getTime()
const jsEncrypt = new JSEncrypt()
jsEncrypt.setPublicKey(publicKey)
enc = jsEncrypt.encrypt('src')
console.log(new Date().getTime() - date1)
//解密
date1 = new Date().getTime()
var decrypt = new JSEncrypt();
decrypt.setPrivateKey(privateKey);
var uncrypted = decrypt.decrypt(enc);
console.log(new Date().getTime() - date1)
// 使用crypto模块解密
const dec3 = crypto.privateDecrypt({
    key: privateKey,
    padding: crypto.constants.RSA_PKCS1_PADDING
}, Buffer.from(enc.toString('base64'), 'base64')).toString();
console.log("jsrsasign decrypt: " + dec2);

web端JSEncrypt


<!doctype html>
<html>

<head>
  <title>JavaScript RSA Encryption</title>
  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  <script src="http://passport.cnblogs.com/scripts/jsencrypt.min.js"></script>
  <script src="./jsrsasign-all-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 () {
        // Encrypt with the public key...
        var date1 = new Date().getTime()
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey($('#pubkey').val());
        var encrypted = encrypt.encrypt('123');
        console.log(new Date().getTime() - date1)
        // jsrsasign加密
        var date1 = new Date().getTime()
        var pub = new KEYUTIL.getKey($('#pubkey').val());
        var enc = KJUR.crypto.Cipher.encrypt('src', pub);
        //console.log(rs.hextob64(enc));
        console.log(new Date().getTime() - date1)
        // Decrypt with the private key...
        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('It works!!!');
        }
        else {
          alert('Something went wrong....');
        }
      });
    });
  </script>
</head>
<body>
  <label for="privkey">Private Key</label><br />
  <textarea id="privkey" rows="15" cols="65">-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAL1o9pLm8ErFu4rzJIizJY+4W3aTZlrEv0WhRKxVXkCt5Lwm2+aE
/YpwtEkIdCpwr//PTLN7y554MhR7tFNu748CAwEAAQJABOkKs2Y/RoD3yrNg+BZE
3AP4gwtxNNsy5jg3EoyoA98yxQQAe3/093Qll19zQVCwHqM9Ijy7EmK6k8NcE5TF
oQIhAOrEV8k3LiYCxnn2ogbRkgRQH1xUju01pvX5fpRBpy4RAiEAzopzsBEP9Bi/
6yNh9n+aMqTI1ceqhj71jbK62gN9I58CIQCAYB9U43yzwl7AALK3IdBD1YBgn8iM
RANpjCXAcmo10QIgHbczv9AkoHTzH8x+aq2fLMwijQdmFFx4jcN6OKWp2ncCIFZX
fwHIo+ZpL/avDe1P5I8FY9KlKnTXDCM9GnI9Unwb
-----END RSA PRIVATE KEY-----</textarea><br />
  <label for="pubkey">Public Key</label><br />
  <textarea id="pubkey" rows="15" cols="65">-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAL1o9pLm8ErFu4rzJIizJY+4W3aTZlrE
v0WhRKxVXkCt5Lwm2+aE/YpwtEkIdCpwr//PTLN7y554MhR7tFNu748CAwEAAQ==
-----END PUBLIC KEY-----</textarea><br />
  <label for="input">Text to encrypt:</label><br />
  <textarea id="input" name="input" type="text" rows=4 cols=70>This is a test!</textarea><br />
  <input id="testme" type="button" value="Test Me!!!" /><br />
</body>
</html>


Comment