# RSA 非对称加密

# 生成秘钥对

var NodeRSA = require("node-rsa");
var fs = require("fs");

var publickKeyPath = './pem/public.pem'
var privateKeyPath = './pem/private.pem'

var key = new NodeRSA({b:512});
key.setOptions({encryptionScheme:'pkcs1'})

var pu = key.exportKey("public");
var pr = key.exportKey("private");

fs.writeFile(publickKeyPath, pu, (err) => console.log(err));
fs.writeFile(privateKeyPath,pr,(err)=>console.log(err));

# 加密解密

const str='JodieTestPem'
fs.exists(publicKeyPath,function (exists) {
    if(exists){
        var pem=fs.readFileSync(publicKeyPath,'utf8')
        var key=new NodeRSA(pem)
        encrypted=key.encrypt(str,'base64')
        console.log('encrypted:'+encrypted)

        fs.exists(privateKeyPath,function (exists) {
            if(exists){
                var pem=fs.readFileSync(privateKeyPath,'utf8')
                var key=new NodeRSA(pem)
                var decrypted=key.decrypt(encrypted,'utf8')
                console.log('decrypted:'+decrypted)
            }
        })
    }
})