Pappa S
Pappa S

Reputation: 343

How to decrypting string in angular 8

Tried to decript the mail as a string in angular 8 but not working.How to do it in angular 8. I have encripted email in node js and passing the encripted data by url and tying to decript in angular 8.

Demo: https://stackblitz.com/edit/angular-ivy-sha3s1

Encripted in nodeJs:

function encrypt(mailid) {
    var cipher = crypto.createCipher('aes-256-cbc', 'd6f3Efeq');
    var crypted = cipher.update(mailid, 'utf8', 'hex')
    crypted += cipher.final('hex');
    return crypted;
}

app.component.ts:

import * as CryptoJS from 'crypto-js'; 
ngOnInit(){ 

  this._Activatedroute.paramMap.subscribe(params => { 
// Encrypt
var encriptdata = params.get('mailid'); //[email protected] 
// Decrypt
var bytes  = CryptoJS.AES.decrypt(encriptdata, 'secret key 123');
var decryptedData = bytes.toString(CryptoJS.enc.Utf8); 
console.log(decryptedData);  

 });
}

Upvotes: 0

Views: 3788

Answers (1)

Diako Sharifi
Diako Sharifi

Reputation: 463

Here is a sample how you can encrypt and decrypt with CryptoJS

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase", {
  format: JsonFormatter
});
​
encrypted
> {
    ct: "tZ4MsEnfbcDOwqau68aOrQ==",
    iv: "8a8c8fd8fe33743d3638737ea4a00698",
    s: "ba06373c8f57179c"
  };
​
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase", {
  format: JsonFormatter
});
​
decrypted.toString(CryptoJS.enc.Utf8)
> "Message";

Reference: https://cryptojs.gitbook.io/docs/#ciphers

Upvotes: 1

Related Questions