Search code examples
node.jsgoogle-cloud-functionsaescryptojs

TypeError: Data must be a buffer


I have an http request in which I want to pass some sensitive data so I tried to encrypted these data. In my React Native app I generated a pair of keys with react-native-rsa-native and I crypte my string with the public key via the function RSA.encrypt(my string, my public key).

After this I send the generated crypt data in my http request and I try to decrypt it in my node.js environment (Google Cloud Functions). For this I use the Crypto module.

I import it with:

const crypto = require('crypto');

And I try to decrypt my data with the RSA private key generated in my react-native module :

crypto.privateDecrypt(rsaPrivateKey, myCryptedString)

But I obtain the error:

TypeError: Data must be a buffer at TypeError (native) at Object.privateDecrypt (crypto.js:375:12) at exports.createPaymentMethod.functions.https.onRequest (/user_code/index.js:928:10) at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:37:41) 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)

Does someone have a solution to my issue ?


Solution

  • According to the documentation, the ciphertext should be an instance of Buffer rather than a String, therefore you can try to wrap the ciphertext into a buffer:

    crypto.privateDecrypt(rsaPrivateKey, Buffer.from(myCryptedString))