Search code examples
node.jsangularnpmangular9cryptojs

Use node crypto in angular 9


My project was in Angular 6 and it had following lines of code

const crypto = require('crypto-js');
const Buffer = require('buffer').Buffer;
const decrypt = new Buffer(data.result.encr, 'base64');
const privatekey = Buffer.from(data.result.pk, 'base64');
this.decrypted = crypto.privateDecrypt(privatekey, decrypt).toString('utf-8');
return this.decrypted;

Which was working fine.

Now I migrated my code to Angular 9. And I find out that crypto has no longer support from NPM

https://www.npmjs.com/package/crypto

It says that I have to use inbuild library of crypto. But I have no idea how to use it.

I thought crypto-js would help me. But it didn't.

If someone knows how to use crypto in Angular 9 or how to convert upper lines for crypto-js then it would be great.

Note: Encryption is happening on server side using crypto only as they have nodejs.

Thanks in advance.


Solution

  • After 3-4 days I am finally able to resolve this.

    1. I installed crypto-browserify.
    2. Delete node_modules folder and then again installed all dependencies by using npm-install

    crypto-browserify provides same features as crypto