0

在 Express 应用程序中编写将请求代理到另一台服务器并在将响应发送到客户端(将在其中解密)之前加密响应的最简单方法是什么。是否可以使用流来完成这一切?

4

1 回答 1

2
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);
})
于 2013-10-07T18:54:18.827 回答