node-rsa

时间:2021-11-22 22:39:26

node-rsa

  引用  

var NodeRSA = require('node-rsa')

  生成一个私钥长度为512的key(同时生成公钥)  

var key = new NodeRSA({b: 512})

  使用公钥加密(当然,加密都是指用公钥加密)

var text = 'Hello RSA!';
var encrypted = key.encrypt(text, 'base64');
console.log('encrypted: ', encrypted);

  使用私钥解密(当然,解密都是指用私钥解密)

var decrypted = key.decrypt(encrypted, 'utf8');
console.log('decrypted: ', decrypted)

  导出公钥、私钥

var private_key = key.exportKey('pkcs1');
console.log(private_key)
var public_key = key.exportKey('pkcs1-public');
console.log(public_key)

  导入公钥、私钥

var key2 = new NodeRSA()
key2.importKey(private_key2,'pkcs1-private')
key2.importKey(public_key2, 'pkcs1-public')

参考:https://www.npmjs.com/package/node-rsa

相关文章