Hello,
I have an application where I generate a URL with token in C# and encode it using H t t pUtility .UrlEncode(token); Now I would like to include email id as well in the URL.
I wish to encrypt email id and send it as part of URL. Then when my react app reads the URL param (useParams();) I would like to decrypt it so that react will have the actual email id.
Any suggestions on how I can do the same?
Thanks.

Tahir AnsariRank #366 · 6.8K reputationPosted Jul 30, 2023, 2:49 AM EDT
Hi Dave bell Please try the below sample
Encrypting Data in C#
using System; using System.Security.Cryptography; using System.Text; public class AESEncryption { public static string EncryptData(string dataToEncrypt, string key) { byte[] dataBytes = Encoding.UTF8.GetBytes(dataToEncrypt); byte[] keyBytes = Encoding.UTF8.GetBytes(key); using (Aes aesAlg = Aes.Create()) { aesAlg.Key = keyBytes; aesAlg.GenerateIV(); // Generate a new IV (Initialization Vector) for each encryption ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Encrypt the data byte[] encryptedData; using (var ms = new System.IO.MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { cs.Write(dataBytes, 0, dataBytes.Length); } encryptedData = ms.ToArray(); } // Combine IV and encrypted data into a single byte array byte[] combinedData = new byte[aesAlg.IV.Length + encryptedData.Length]; Buffer.BlockCopy(aesAlg.IV, 0, combinedData, 0, aesAlg.IV.Length); Buffer.BlockCopy(encryptedData, 0, combinedData, aesAlg.IV.Length, encryptedData.Length); // Convert the combined data to Base64 string for easy transport return Convert.ToBase64String(combinedData); } } }Decrypting Data in React:
npm install crypto-js
Below is a React js code
import CryptoJS from 'crypto-js'; const DecryptData = (encryptedData, key) => { const combinedData = Buffer.from(encryptedData, 'base64'); // Extract IV and encrypted data from the combined data const iv = combinedData.slice(0, 16); const encryptedBytes = combinedData.slice(16); const keyBytes = CryptoJS.enc.Utf8.parse(key); const ivBytes = CryptoJS.lib.WordArray.create(iv); const decryptedBytes = CryptoJS.AES.decrypt( { ciphertext: CryptoJS.lib.WordArray.create(encryptedBytes) }, keyBytes, { iv: ivBytes } ); // Convert the decrypted bytes to a UTF-8 string const decryptedData = decryptedBytes.toString(CryptoJS.enc.Utf8); return decryptedData; };According to your need adjust the code
Cr BhargaviRank #420 · 5.9K reputationPosted Jul 29, 2023, 1:00 PM EDT
you can use a symmetric encryption algorithm. Symmetric encryption uses a single secret key to both encrypt and decrypt data. This way, you can encrypt the email ID in C# before generating the URL and then decrypt it in your React app after reading the URL parameter.A gist of the process on how you can implement in C# and React In C#, Use a symmetric encryption algorithm, such as AES (Advanced Encryption Standard), to encrypt the email ID along with the token. and then generate the URL with the encrypted email ID and the token. Use
HttpUtility.UrlEncodeto encode the complete URL (including the encrypted data) before sending it to the React app.In the React App:
useParams()or any URL parsing library to retrieve the URL parameter containing the encrypted data.decodeURIComponent()to get the encrypted data.Hope this solves your question.