How to encrypt using a RSA certificate in nodejs?

2k Views Asked by At

I am going through some docs where I am required to encrypt some data using a certificate. The docs say

Encrypt the array with the M-Pesa public key certificate. Use the RSA algorithm, and use PKCS #1.5 padding (not OAEP), and add the result to the encrypted stream.

Then it goes on to say:

Convert the resulting encrypted byte array into a string using base64 encoding. The resulting base64 encoded string is the security credential.

So my questions are:

  1. How do I achieve this in nodejs?
  2. I have read that:

A certificate contains a public key. The certificate, in addition to containing the public key, contains additional information such as issuer, what the certificate is supposed to be used for, and other types of metadata.

. What is the advantage of encrypting with a certificate as opposed to just using a public key? Or is it the same thing?

1

There are 1 best solutions below

0
On

Since you're using Mpesa, you can either use their website to generate the SecurityCredential, or refer to the following implementation through Node:

https://github.com/Wanyeki/node-mpesa-encryption/blob/main/src/password%20encrypt.js