hapiman / jsrice

A common utils using in javascript
0 stars 0 forks source link

RSA加密 #31

Open hapiman opened 6 years ago

hapiman commented 6 years ago
'use strict'

const nodeRsa = require('node-rsa')

let privatePem = '私钥'
let publicPem = '公钥'
let text = '欲加密文本'

let publicKey = new nodeRsa(publicPem);
let privateKey = new nodeRsa(privatePem);

publicKey.setOptions({encryptionScheme: 'pkcs1'});
privateKey.setOptions({encryptionScheme: 'pkcs1'});

let encrypted = publicKey.encrypt(text, 'base64')
// iTGJGi3YWjPqrlTASjA+dB7oRwSYO50nToxkuSe+7dzZ6eC2uQE0tGk2TV+ouj9FDwdUmc/5yMBKP4Bvyan0+08yGlnl6yQMwvlQ0CagTwdzxdstMjfAReLC4JANcodW/+zB6H3K64WpLCRuQB0m0Ss1IxygM3I2Ut27gnlFhjY=
var decrypted = privateKey.decrypt(encrypted, 'utf8')