Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js proxy request and encrypt it using AES

Node.js proxy request and encrypt it using AES

Problem

What would be the easiest way in an Express app to code a route that proxies the Request to another server and encrypts the response before sending it to the client (where it will be decrypted). Is it possible to do it all using streams?

Problem courtesy of: Nick Dima

Solution

var request = require('request'),
    http = require('http'),
    crypto = require('crypto'),
    acceptor = http.createServer().listen(8089);

acceptor.on('request', function(r, s) {
    var ciph = crypto.createCipher('aes192', 'mypassword');

    // simple stream object to convert binary to string
    var Transform = require('stream').Transform;
    var BtoStr = new Transform({decodeStrings: false});
    BtoStr._transform = function(chunk, encoding, done) {
       done(null, chunk.toString('base64'));
    };

    // get html from Goog, could be made more dynamic
    request('http://google.com').pipe(ciph).pipe(BtoStr).pipe(s);


    //  try encrypt & decrypt to verify it works, will print cleartext to stdout
    //var decrypt = crypto.createDecipher('aes192', 'mypassword');
    //request('http://google.com').pipe(ciph).pipe(decrypt).pipe(process.stdout);
})
Solution courtesy of: John Williams

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js proxy request and encrypt it using AES

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×