Using encryption in cloud functions

I am using cloud functions to send data to Moralis so that it can be stored on IPFS.
I have one cloud function for storing the data and one for retrieving the data.
However, I would like to encrypt this data before it is sent to IPFS and decrypt it when I retrieve it.

How can this be done?

Thanks

You can use crypto module that is already included

Thank you for your reply.

How would I go about doing that, I gave that a go and I couldn’t get it to work.

What it didn’t work?

I have the following code:

Moralis.Cloud.define("testEncrypt", async (request) => {
  const encrypted = await crypto.subtle.encrypt(
    {
      name: "RSA-OAEP"
    },
    "thisIsTheKey",
    "string to encrypt"
  );
  return encrypted;
});

But I get the error:

TypeError: Cannot read properties of undefined (reading ‘encrypt’)

That means that crypto doesn’t have that subtle property

Why does crypto not have the encrypt property? It should do according to the documentation?

this seems to work:

var mykey = crypto.createCipher('aes-128-cbc', 'fasfasdfasfdsdf');
var mystr = mykey.update('abc', 'utf8', 'hex')
mystr += mykey.final('hex');

logger.info("password" + mystr);

1 Like