Struggling with implementing encryption in TypeScript and decryption in C#. After searching online, I found some resources related to JavaScript, not TypeScript.
Encrypt in JavaScript and decrypt in C# with AES algorithm
Encrypt text using CryptoJS library in Angular2
How to import non-core npm modules in Angular 2 for encryption?
I followed the above links to implement encryption/decryption in my application.
This is the code written in myservice.ts:
import * as CryptoJS from 'crypto-js';
var key = CryptoJS.enc.Utf8.parse('7061737323313233');
var iv = CryptoJS.enc.Utf8.parse('7061737323313233');
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("It works"), key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
console.log('Encrypted :' + encrypted);
console.log('Key :' + encrypted.key);
console.log('Salt :' + encrypted.salt);
console.log('iv :' + encrypted.iv);
console.log('Decrypted : ' + decrypted);
console.log('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8));
Before adding this code in myservice.ts, I added the dependency "crypto-js": "^3.1.9-1" in package.json.
Even after successfully restoring the packages, CryptoJS still shows an error in myservice.ts stating cannot find name as CryptoJS.
Could you guide me on how to import CryptoJS from node modules and explain how to encrypt a string in TypeScript and decrypt it in C# using Advanced Security Algorithm (AES)?
Pradeep