TypeError: Data must be a buffer

TypeError: Data must be a buffer

我有一个 http 请求,我想在其中传递一些敏感数据,所以我尝试加密这些数据。 在我的 React Native 应用程序中,我使用 react-native-rsa-native 生成​​了一对密钥,并通过函数 RSA.encrypt(my string, my public 用 public 密钥加密了我的字符串钥匙)。

在此之后,我在我的 http 请求中发送生成的加密数据,并尝试在我的 node.js 环境(Google 云函数)中解密它。为此,我使用了 Crypto 模块。

我导入它:

const crypto = require('crypto');

然后我尝试使用在我的 react-native 模块中生成的 RSA 私钥来解密我的数据:

crypto.privateDecrypt(rsaPrivateKey, myCryptedString)

但是我得到错误:

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)

有人能解决我的问题吗?

根据documentation,密文应该是Buffer的实例而不是String,因此您可以尝试将密文包装到缓冲区中:

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