Loading

[笔记] javascript RSA 加密,解密,签名,验签

使用的库:
travist/jsencrypt: A Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
brix/crypto-js: JavaScript library of crypto standards.

测试地址:
Online RSA Key Generator

如果是在 HTML 中使用,引用的库:

// 具体地址,需要另行处理

    <script src="./jsencrypt.min.js"></script> 
    <script src="./crypto-js.js"></script> 

加密与解密

// Encrypt with the public key...
var encrypt = new JSEncrypt();
encrypt.setPublicKey($('#pubkey').val());
var encrypted = encrypt.encrypt($('#input').val());

// Decrypt with the private key...
var decrypt = new JSEncrypt();
decrypt.setPrivateKey($('#privkey').val());
var uncrypted = decrypt.decrypt(encrypted);

签名与验签

这里的 hash 算法是 SHA256

// Sign with the private key...
var sign = new JSEncrypt();
sign.setPrivateKey($('#privkey').val());
var signature = sign.sign($('#input').val(), CryptoJS.SHA256, "sha256");

// Verify with the public key...
var verify = new JSEncrypt();
verify.setPublicKey($('#pubkey').val());
var verified = verify.verify($('#input').val(), signature, CryptoJS.SHA256);

END


原文链接:https://www.cnblogs.com/jasongrass/p/12872023.html

posted @ 2020-05-11 21:26  J.晒太阳的猫  阅读(2588)  评论(0编辑  收藏  举报