Hello I am trying to implement encryption in my application. I am using angular (angular-4) for the frontend and node js for the backend. Communication is done through socket.io through custom commands. But basically what I am stuck at is finding an appropriate library for RSA encryption in client side. client will first request server for an RSA public key. Server responds with key but now I cannot find any library suitable to encrypt data with RSA using this public key. I have tried node-rsa. Following is a code sn
import * as NodeRSA from 'node-rsa';
@Injectable()
export class SecurityService {
RSA: any
initializeRSA(key: string) {
this.RSA = new NodeRSA();
this.RSA.importKey(key)
console.log(this.RSA.encrypt('Hello World'));
}
But I am receiving this error.
Error during encryption. Original error: TypeError: crypt.createHash is not a function
at NodeRSA.webpackJsonp.../../../../node-rsa/src/NodeRSA.js.module.exports.NodeRSA.$$encrypt
Help would be very much appreciated.
JSEncrypt with angular
https://plnkr.co/edit/sEPK1DcynMphJnGziUVX
I have used JSEncrypt v2.3.0 Lib for same.
Implementation
Add JSEncrypt Lib javascript file in Asset Folder of Angular Project.
Add script in index.html
<script src="jsencrypt.js"></script>
So, It will available at all Component.
declare JSEncrypt at your component file where you want to use it.
declare var JSEncrypt: any;
Inside class declare variable
decrypt = new JSEncrypt();
const privatekey = Private Key goes here;
const publickey = Public key goes here;
const decryptDataRow = Decrypted data string;
this.decrypt.setPrivateKey(privatekey);
this.decryptdata = this.decrypt.decrypt(decryptDataRow);
decryptdata
contain result string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With