1

I would like to encrypt file saved in Buffer type and return its Base 64 format. This is my code:

encrypt(fileInBuffer: Buffer, publicKey: string): string {
        const encrypted = publicEncrypt(publicKey, fileInBuffer)
        return encrypted.toString("base64")
}

But i'm getting error when publicEncrypt method is invoking:

Error: error:0409A06E:rsa routines:RSA_padding_add_PKCS1_OAEP_mgf1:data too large for key size
server_1  |     at internal/crypto/cipher.js:63:12

I was trying to pass readable stream as a second parameter of this method, but Readable type not passing there. I have to also add (i'm not sure it matters) that i'm not storing file locally, it's saved in memory.

Topaco
  • 40,594
  • 4
  • 35
  • 62
Marcin Warzybok
  • 357
  • 4
  • 16
  • 3
    RSA can only be used to encrypt plaintexts of limited length (key size minus the space required by padding). For longer plaintexts, symmetric encryption (e.g. AES) is applied, alone or in combination with asymmetric encryption ([hybrid encryption](https://en.wikipedia.org/wiki/Hybrid_cryptosystem)). – Topaco Sep 05 '21 at 15:08

0 Answers0