-
-
Save thelebster/4898070ce59263c8487b5daf5aadf161 to your computer and use it in GitHub Desktop.
RSA encryption / decryption example (nodejs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openssl key pair generate | |
//client - using meteor.js | |
const nodersa = Npm.require('node-rsa'); | |
import { HTTP } from 'meteor/http'; | |
const syncPost = Meteor.wrapAsync( HTTP.post, HTTP ); | |
encryptStringWithRsaPublicKey( data ) { | |
const absolutePath = Assets.absoluteFilePath( "public.key" ); //public key file path | |
const publicKey = fs.readFileSync( absolutePath, "utf8" ); | |
const key = new nodersa( publicKey ); | |
const encrypted = key.encrypt( data, 'base64' ); | |
const remote = 'url:8000'; //example | |
return syncPost( remote, { data : { 'value' : encrypted } } ); | |
} | |
... | |
//example server | |
'use strict'; | |
const express = require('express'); | |
const fs = require('fs'); | |
const nodersa = require('node-rsa'); | |
const bodyParser = require('body-parser'); | |
let app = express(); | |
app.use(bodyParser.urlencoded({ extended : false })); | |
app.use(bodyParser.json()); | |
app.post('/', (req, res)=>{ | |
let value = req.body.value; | |
const privateKey = fs.readFileSync('./store/privateKey.pem', 'utf8'); | |
const original = new nodersa(privateKey).decrypt(value, 'utf8'); | |
res.end(original); | |
}); | |
app.listen(8000, ()=>{ | |
console.log('on 8000'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment