Issues while encrypting password with public key, anyone?

Hello,

I have the following code working in an Ionic v3 app.
It takes a public key and a password and encrypts it.

import NodeRSA from 'node-rsa';
import Buffer from 'buffer';


let keyBuf = new Buffer(key, 'hex');
let rsa = new NodeRSA(keyBuf.slice(22), 'pkcs1-public-der', {
	encryptionScheme: 'pkcs1'
});
console.log("encrypted password: " + rsa.encrypt(password, 'hex'));

Now, I am building an Ionic 4 app and trying to use the same code.
I have been unsuccessful so far.
There seems to be all sort of issues with Buffer, or I am getting into all sorts of issues with it due to the troubleshooting I am doing.
Errors may be due to new Buffer being deprecated.
I’m also getting:

TypeError: Buffer is not a constructor
Buffer is not a constructor
process is undefined

Some of them I fixed by adding this to index.html:

  <script>
    var global = global || window;
    var Buffer = Buffer || [];
    var process = process || {
      env: { DEBUG: undefined },
      version: []
    };    
  </script>

and this to pollyfils.ts

(window as any).process.version = '';
(window as any).process.versions = {node: false};

I am getting into similar issues with Buffer when I use crypto.

I switched the new Buffer() into Buffer.from as recommended in NodeJS, but I see a lot of code under node_modules still uses new Buffer().

Can someone tell me how are you using encryption with an Ionic Capacitor app, if possible?
Thanks!

All modern browsers have WebCrypto built in, and I would recommend using that over trying to adapt something that is not designed to run in a browser environment (things that rely on buffer really want to be running under node).

There are examples of how to use it here, and I think the RSA-OAEP encrypt one provides basically the operation you describe here, with the added benefit of dealing with all the details of padding that are a PITA to do right, but matter a lot for security.

alright, thank you for pointing me in one direction. I’ll give WebCrypto a try.
Gustavo.