How to Encrypt value in Typescript and Decrypt in c#
Loading
How to Encrypt value in Typescript and Decrypt in c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 21, 2023, 1:45 AM EST
You'll need to install CryptoJS in your TypeScript project. You can do this via npm:
//Inside imports of your TS file include import * as CryptoJS from 'crypto-js'; // Declare this key and iv values in declaration private key = CryptoJS.enc.Utf8.parse('4512631236589784'); private iv = CryptoJS.enc.Utf8.parse('4512631236589784'); // Methods for the encrypt and decrypt Using AES encryptUsingAES256() { var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(JSON.stringify("Your Json Object data or string")), this.key, { keySize: 128 / 8, iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); console.log('Encrypted :' + encrypted); this.decryptUsingAES256(encrypted); return encrypted; } decryptUsingAES256(decString) { var decrypted = CryptoJS.AES.decrypt(decString, this.key, { keySize: 128 / 8, iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); console.log('Decrypted : ' + decrypted); console.log('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8)); }The C# code to encode or decode is here.
public class encryption { public static string DecryptStringAES(string cipherText) { var keybytes = Encoding.UTF8.GetBytes("4512631236589784"); var iv = Encoding.UTF8.GetBytes("4512631236589784"); var encrypted = Convert.FromBase64String(cipherText); var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keybytes, iv); return decriptedFromJavascript; } private static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv) { if (cipherText == null || cipherText.Length <= 0) { throw new ArgumentNullException("cipherText"); } if (key == null || key.Length <= 0) { throw new ArgumentNullException("key"); } if (iv == null || iv.Length <= 0) { throw new ArgumentNullException("key"); } string plaintext = null; using (var rijndaelManaged = new RijndaelManaged()) { rijndaelManaged.Mode = CipherMode.CBC; rijndaelManaged.Padding = PaddingMode.PKCS7; rijndaelManaged.FeedbackSize = 128; rijndaelManaged.Key = key; rijndaelManaged.IV = iv; var decryptor = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV); try { using (var memoryStream = new MemoryStream(cipherText)) { using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { using (var streamReader = new StreamReader(cryptoStream)) { plaintext = streamReader.ReadToEnd(); } } } } catch { plaintext = "keyError"; } } return plaintext; } public static string EncryptStringAES(string plainText) { var keybytes = Encoding.UTF8.GetBytes("4512631236589784"); var iv = Encoding.UTF8.GetBytes("4512631236589784"); var encryoFromJavascript = EncryptStringToBytes(plainText, keybytes, iv); return Convert.ToBase64String(encryoFromJavascript); } private static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv) { if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException("plainText"); } if (key == null || key.Length <= 0) { throw new ArgumentNullException("key"); } if (iv == null || iv.Length <= 0) { throw new ArgumentNullException("key"); } byte[] encrypted; using (var rijndaelManaged = new RijndaelManaged()) { rijndaelManaged.Mode = CipherMode.CBC; rijndaelManaged.Padding = PaddingMode.PKCS7; rijndaelManaged.FeedbackSize = 128; rijndaelManaged.Key = key; rijndaelManaged.IV = iv; var encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV); using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { using (var streamWriter = new StreamWriter(cryptoStream)) { streamWriter.Write(plainText); } encrypted = memoryStream.ToArray(); } } } return encrypted; } }Dhiraj PoojaryRank #1,039 · 1.9K reputationPosted Feb 7, 2024, 4:54 AM EST
You can find assistance on encrypting a string in TypeScript and decrypting it in C# using advanced encryption in the following Stack Overflow thread:
https://stackoverflow.com/questions/41671267/encrypt-the-string-in-typescript-and-decrypt-in-c-sharp-using-advanced-encryptio#:~:text=npm%20instal%20crypto%2Djs
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 4, 2024, 12:43 PM EST
Encrypting a value in TypeScript and decrypting it in C# involves using a common encryption algorithm that is supported in both environments. One such algorithm is AES (Advanced Encryption Standard). I'll provide a basic example using AES encryption with CryptoJS in TypeScript and decryption with C#.
import * as CryptoJS from 'crypto-js'; // Encrypt function function encryptValue(value: string, secretKey: string): string { const ciphertext = CryptoJS.AES.encrypt(value, secretKey).toString(); return ciphertext; } // Example usage const valueToEncrypt = 'Hello, World!'; const secretKey = 'YourSecretKey'; const encryptedValue = encryptValue(valueToEncrypt, secretKey); console.log('Encrypted Value:', encryptedValue);using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string encryptedValue = "YourEncryptedValue"; string secretKey = "YourSecretKey"; string decryptedValue = Decrypt(encryptedValue, secretKey); Console.WriteLine("Decrypted Value: " + decryptedValue); } // Decrypt function static string Decrypt(string encryptedText, string key) { using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Encoding.UTF8.GetBytes(key); aesAlg.IV = new byte[16]; // Initialization Vector, you may need to handle this more securely in production. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encryptedText))) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } } }In C#, the
Aesclass fromSystem.Security.Cryptographyis used for decryption. Ensure you handle the secret key securely in your application. The Initialization Vector (IV) should also be handled securely in production but is kept simple in this example.Rajanikant HawaldarRank #110 · 20.2K reputationPosted Mar 30, 2024, 10:30 AM EDT
Please check below article
https://www.c-sharpcorner.com/UploadFile/4d9083/encrypt-in-javascript-and-decrypt-in-C-Sharp-with-aes-algorithm-i/
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Mar 30, 2024, 8:44 AM EDT
Hello Piyush,
You need to install the cryptojs in your typescript side and do the encryption when you required to encrypt your confidentials data.
When the request is send to the server side then you need to decrypt that value in server side and use in the application side.
For how to implement this mechanisum so you need to refer Amit or Tauhin Reply comments.
Thanks