在云端使用node.js对加密文件进行GPG解密

时间:2019-05-07 03:47:00

标签: node.js gnupg

如何使用node.js在云中解密GPG加密文件而又不将私钥保存在云中

从onPrem迁移到Azure云。当前,对于服务器上已安装onPrem GPG库的服务器,公钥/私钥已导入GPG库中,解密代码使用指纹来解密文件,但这意味着服务器上物理上存在私钥。随着迁移到云,安全团队不想在云PODS上维护私钥。 可以通过API调用获得私钥文件的内容。 尝试通过将私钥API的响应对象作为“收件人”和“ key”作为参数传递给cryptoReader.js来解密加密的文件,后者使用crpto,gpg模块执行解密。没运气。 如果我在将私钥导入GPG保管库的机器上尝试相同的代码,则它可以正常工作。

这是否意味着必须将私钥物理导入要执行解密的服务器上的GPG库中? 其他对安全敏感的公司如何在云中实现这一目标?

//KeyBuffer contains contents of private key .asc file as response from API call

Decryption call:

stream.decryptFilewithPGP(<encryptedFileName>, { skipEmptyLines: true, encoding: 'utf8' },
 {
  mode: 'PGP',
  // key: keyBuffer,
  recipient: keyBuffer,
  passphrase: config.asymmetric.passphrase
  // fingerprint: config.asymmetric.fingerprint //for onPrem, for cloud no fingerprint
  });

Actual decryption implementation:
CryptoReader.prototype.decryptFilewithPGP = function (filepath, options, cipher) {
    var self = this;

    this._filepath = path.normalize(filepath);
    this._encoding = options && options.encoding || 'utf8';
    this._skipEmptyLines = options && options.skipEmptyLines || false;

    this._streamOptions = { encoding: this._encoding };

    if (options && options.start) {
        this._streamOptions.start = options.start;
    }

    if (options && options.end) {
        this._streamOptions.end = options.end;
    }
    let args = [
        '--passphrase', self.decode(cipher.passphrase),
        //'--recipient', cipher.key,
        '--recipient', cipher.recipient,
        '--trust-model', 'always',
        '--decrypt'
    ];

    console.log(`args: ${JSON.stringify(args)}`);

    readStream = this._readStream ? this._readStream : fs.createReadStream(this._filepath);
    outStream = this._outStream ? this._outStream : new Stream.PassThrough;
    //var outStream = new Stream.PassThrough;

    gpg.callStreaming(readStream, outStream, args, function (err) {
        if (err) {
            self.emit('error', err);
        }
        else {
            var _decData='', lastLine;
            outStream.on('data', function (data) {
                self._readStream.pause();
                self._outStream.pause();

                let _data = self._lineFragment.concat(data.toString('utf8'));
                self._lines = self._lines.concat(_data.split(/(?:\n|\r\n|\r)/g));
                self._lineFragment = self._lines.pop() || ''; //hold last line
                //console.log('data chunk size: ' + data.length + ' no. or rows: ' + self._lines.length);

                setImmediate(function () {
                    self._nextLine();
                });
            });
            outStream.on('end', function () {
                self._end = true;
                setImmediate(function () {
                    self._nextLine();
                });
            });
            outStream.on('error', function (error) {
                self.emit('error', err);
            });
        }
    });

    this._readStream = readStream;
    this._outStream = outStream;
};

预期:应解密加密的文件并返回明文(如在导入了私钥的onPrem服务器或DEV计算机中发生的情况

实际:不执行解密。

0 个答案:

没有答案
相关问题