js使用RSA加密

为了防止Web页面的敏感信息泄露,我们需要使用RSA加密算法对数据进行加密。
JS中常用的RSA加密库有:jsencrypt,jsrsasign,js-crypto-rsa
jsencrypt库的使用比较简单:

安装库
npm i jsencrypt

使用:
import JSEncrypt from ‘jsencrypt‘
var publicKey = "-----BEGIN PUBLIC KEY-----\n" +
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDTTt5d1LYtIxiW9ekKFBVonFOT\n" +
        "XJHv4PY4xCDLPYbHWRKa/mRO7J11OJX+cR7bqzNq6uxH1W339wV\n" +
        "lLP/x3Rl1RBh4prj0eYOEIsDVTvLTJONKazRtQrZ7yzSZ69o/3CQv\n" +
        "ex6kb4js+9zho4U9fwIDAQAB\n" +
        "-----END PUBLIC KEY-----";
    let jse = new JSEncrypt();
    jse.setPublicKey(publicKey);
    var str = jse.encrypt(en);
    console.log("加密数据:" + str)

注意: jsencrypt 默认使用的PKCS1加密方式

js-crypto-rsa 库使用

  • 导入依赖包
    import rsa from 'js-crypto-rsa'; // for npm
    import rsa from 'path/to/js-crypto-rsa/dist/index.js'; // for github
    
  • 生成key
    rsa.generateKey(2048).then( (key) => {
    	// now you get the JWK public and private keys
    	const publicKey = key.publicKey;
     	const privateKey = key.privateKey;
    })
    
  • 签名和验证
    const publicJwk = {kty: 'RSA', n: '...', e: '...'}; // public key
    const privateJwk = {kty: 'RSA', n: '...', e: '...', p: '...', q: '...', dp: '...', dq: '...', qi: '...'}; // paired private key
    const msg = ...; // Uint8Array
     
    // sign
    rsa.sign(
      msg,
      privateJwk,
      'SHA-256',
      { // optional
        name: 'RSA-PSS', // default. 'RSASSA-PKCS1-v1_5' is also available.
        saltLength: 64
      }
      ).then( (signature) => {
      // now you get the signature in Uint8Array
      return rsa.verify(
        msg,
        sign,
        publicJwk,
        'SHA-256',
          { // optional
            name: 'RSA-PSS', // default. 'RSASSA-PKCS1-v1_5' is also available.
            saltLength: 64 // default is the same as hash length
          } 
        );  
    }).then( (valid) => {
      // now you get the result of verification in boolean
    });
    
  • 加密和解密
    const publicJwk = {kty: 'RSA', n: '...', e: '...'}; // public key
    const privateJwk = {ktyp: 'RSA', n: 'P-256', e: '...', p: '...', q: '...', dp: '...', dq: '...', qi: '...'}; // paired private key
    const msg = ...; // Uint8Array
     
    // sign
    rsa.encrypt(
      msg,
      publicJwk,
      'SHA-256', // optional, for OAEP. default is 'SHA-256'
      { // optional
        name: 'RSA-PSS', // default. 'RSASSA-PKCS1-v1_5' is also available.
        // label: new Uint8Array([...]) // optional
      }).then( (encrypted) => {
      // now you get an encrypted message in Uint8Array
        return rsa.decrypt(
          encrypted,
          privateJwk,
          'SHA-256', // optional, for OAEP. default is 'SHA-256'
          { // optional
            name: 'RSA-PSS', // default. 'RSASSA-PKCS1-v1_5' is also available.
            // label: new Uint8Array([...]) // optional
          }
        );  
    }).then( (decrypted) => {
      // now you get the decrypted message
    });
    

扫码关注微信公众号,更好的交流
扫码关注微信公众号,更好的交流

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值