I am having struggle to implement the encryption in typescript and decryption in C#. Before posting question here, I did Google it and find some links but those links are related to JavaScript not a typescript.
Encrypt in javascript and decrypt in C# with AES algorithm
encrypt the text using cryptojs library in angular2
How to import non-core npm modules in Angular 2 e.g. (to use an encryption library)?
I followed the above links, for implementing the encryption/decryption concept in my current application.
This is the code I wrote in myservice.ts
//import { CryptoJS } from 'node_modules/crypto-js/crypto-js.js';
//import 'crypto-js';
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 I added the above lines of code in myservice.ts, I added the dependency as "crypto-js": "^3.1.9-1" in package.json file.
After added the above dependency in package.json, then I was restored the packages successfully. But still also CryptoJS shows error in myservice.ts like can not found name as CryptoJS.
Can you please tell me how to import the CryptoJS from node modules and also tell me how to encrypt the string in typescript and decrypt the same string in C# using Advanced Security Algorithm (AES)?
Pradeep