Firebase Cloud Functions使用SHA256 RSA崩溃进行加密和解密

时间:2019-04-19 21:14:48

标签: node.js google-cloud-functions

我目前正在尝试使用SHA256 RSA对Firebase Cloud Functions中的函数内部的消息进行加密和解密。它在本地完美运行,但是当我部署该功能时,它在Firebase服务器上不起作用。我在服务器上遇到以下错误

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
    at Error (native)
    at Object.privateDecrypt (crypto.js:375:12)
    at decrypt (/user_code/lib/index.js:58:30)
    at exports.generateTransactionToken.functions.https.onRequest (/user_code/lib/index.js:75:30)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /var/tmp/worker/worker.js:783:7
    at /var/tmp/worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

我的功能结构如下

import * as functions from 'firebase-functions';
import * as crypto from 'crypto';

function encrypt(message: string, publicKey: string) {
    const buffer = Buffer.from(message, 'utf8')
    const encrypted = crypto.publicEncrypt(publicKey, buffer)
    return encrypted.toString('base64')
  }

  function decrypt(encryptedMessage: string, privateKey: string) {
    const buffer = Buffer.from(encryptedMessage, 'base64')
    const decrypted = crypto.privateDecrypt(
      {
        key: privateKey,
        passphrase: '',
      },
      buffer,
    )
    return decrypted.toString('utf8')
  }

export const generateTransactionToken = functions.https.onRequest((request, response) => {

    const publicKey:string = functions.config().test.publickey
    const privateKey:string = functions.config().test.privatekey

    const fixedPublicKey:string = publicKey.replace(/\\n/g, '\n')
    const fixedPrivateKey:string = privateKey.replace(/\\n/g, '\n')

    const message = "hello people from the world";
    const encryptedMessage = encrypt(message, fixedPublicKey);
    const decryptedMessage = decrypt(encryptedMessage, fixedPrivateKey);


    let obj = {
        'public': publicKey,
        'private': privateKey,
        'message': message,
        'encrypted': encryptedMessage,
        'decrypted': decryptedMessage
    }

    response.status(200).send(obj);
});

我将环境变量设置如下

私钥

firebase functions:config:set test.privatekey="-----BEGIN RSA PRIVATE KEY-----\nProc-Type: 4,ENCRYPTED\nDEK-Info: AES-256-CBC,73B2FEB41FE8BD5AA9ECCD5F0AFBE37A\n\nE5lYh9m3kmOKYkXx213W3oK+EV7HCypSpgGSRrCTngutaT+1jNTGT8Je4Z0OLqAA\nJc3BucEDnpfMn7mh70XP13wInviOoGLRBp5Kqc8LnCBerF+1mdP/kEBXtQYN36Ql\nX9QFpPQKmnThAxKtFJ0HAzn7xtKnlRLics7gD01b1rw+qLlR32VnH00jFT7rIayj\nqQLM7ZTElbsr4yNdHZ/4kzStVQPM+SLqYXjpbdeK10UJ7K6+HyUdY/s53Y1fFsH9\nFufzP5bQ0qSkZxuA85Df8w6rZEP0Kbg1ljKYrjKt94Gs/+hD5QCvLQ5xHLa4t0Nv\ntjLubR7MqjY4CidFwsSqWQv/32rmTI3JC6l/gmcHmSsL1SozMI5PrrLAuD+JmRXR\n5pb4Ih7GXu7qGeatRUdqcLV6t3O2jrNG9O/miUBKPqblMuI4....\n-----END RSA PRIVATE KEY-----"

公钥

firebase functions:config:set test.publickey="-----BEGIN RSA PUBLIC KEY-----\nMIICCgKCAgEAv9YlZ2msmHzpJ9y2qFVRUtjPKAsy5/Ig21DrKofCvq6/W3AxfWrQ\nlGmrJF/OJGr6NRo8dIlGaAyZ4DRbuJctCP4Ij2ibLH....==\n-----END RSA PUBLIC KEY-----"

0 个答案:

没有答案