Nana Darko
Nana Darko

Reputation: 104

Node-forge AES Data Decryption Not Returning Original Data. Angular/Typescript

Trying to decrypt a cipher to get the original data but its not working as expected. Kindly assist:

Code:

general() {
    const foo = {
      pass: "Werwerw",
      username: "qqwewdxas"
    };
    var key = "d7rccozrm8q1iuvi";
    var iv = "ttmw0lipoht7kz18";
    console.log("Key", key);
    console.log("IV", iv);

    var cipher = forge.cipher.createCipher('AES-CBC', key);
    cipher.start({ iv: iv });
    cipher.update(forge.util.createBuffer(JSON.stringify(foo)));
    cipher.finish();
    var encrypted = cipher.output;
    // outputs encrypted hex
    console.log("Encrypted", encrypted.toHex());

    var decipher = forge.cipher.createDecipher('AES-CBC', key);
    decipher.start({ iv: iv });
    decipher.update(encrypted);
    var result = decipher.finish(); // check 'result' for true/false
    console.log("Decrypted", decipher.output.toHex());
  }

Response: Encrypted: 8e3cb8653b17fc0f5e50916d8d22ec666207babdb4b0b945ca06fde4cc31a6bc40222de1210d830f3d672f4001b69a30 Decrypted: 7b2270617373223a2257657277657277222c22757365726e616d65223a22717177657764786173227d

Upvotes: 0

Views: 892

Answers (1)

Nana Darko
Nana Darko

Reputation: 104

I finally found the answer after days of struggling. I passed the encrypted data as a buffer and it worked:

decipher.update(forge.util.createBuffer(encrypted)); #instead of decipher.update(encrypted);

Upvotes: 0

Related Questions