How to encrypt json object using aes256 with cbc and pkcs5 padding.
i did with base64 i dont have idea about aes256 pls help me
Thanks in Advance
How to encrypt json object using aes256 with cbc and pkcs5 padding.
i did with base64 i dont have idea about aes256 pls help me
Thanks in Advance
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
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 3, 2023, 8:21 AM EDT
Sure, I can help you with that. Here's an example code snippet in C# that uses the built-in System.Security.Cryptography library to encrypt a JSON object using AES-256 with CBC mode and PKCS5 padding in ASP.NET:
using System; using System.IO; using System.Text; using System.Security.Cryptography; using Newtonsoft.Json; // Define the JSON object to encrypt var data = new { name = "John Doe", age = 30, email = "[email protected]" }; // Convert the JSON object to a string var json_data = JsonConvert.SerializeObject(data); // Define the encryption key and initialization vector (IV) var key = Encoding.UTF8.GetBytes("secretkey1234567"); // 16-byte key for AES-256 var iv = Encoding.UTF8.GetBytes("initialvector123"); // 16-byte IV for CBC mode // Create a new AES cipher with CBC mode and PKCS5 padding var aes = Aes.Create(); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.KeySize = 256; aes.BlockSize = 128; aes.Key = key; aes.IV = iv; // Convert the data to a byte array var json_bytes = Encoding.UTF8.GetBytes(json_data); // Pad the data to a multiple of 16 bytes using PKCS5 padding var block_size = aes.BlockSize / 8; var padding = block_size - (json_bytes.Length % block_size); var padded_data = new byte[json_bytes.Length + padding]; Array.Copy(json_bytes, padded_data, json_bytes.Length); for (int i = json_bytes.Length; i < padded_data.Length; i++) { padded_data[i] = (byte)padding; } // Encrypt the padded data using the AES cipher var encryptor = aes.CreateEncryptor(); var encrypted_data = encryptor.TransformFinalBlock(padded_data, 0, padded_data.Length); // Convert the encrypted data to a base64-encoded string var encrypted_string = Convert.ToBase64String(encrypted_data);Jaya PrakashRank #1,293 · 1.3K reputationPosted Apr 3, 2023, 11:57 PM EDT
@Rajkiran Swain sir,
Pls provide code also for Decryption thank you.
Amit MohantyRank #112 · 20K reputationPosted Apr 4, 2023, 12:28 AM EDT
Try this
using System; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; public class Program { public static void Main() { // Sample JSON object var jsonObject = new { Name = "Amit", Age = 35, Address = "Hyderabad" }; // Generate a random 256-bit encryption key and a 128-bit initialization vector (IV) byte[] key = new byte[32]; using (var rng = new RNGCryptoServiceProvider()) { rng.GetBytes(key); } byte[] iv = new byte[16]; using (var rng = new RNGCryptoServiceProvider()) { rng.GetBytes(iv); } // Convert the JSON object to a string and encode it in UTF-8 format string jsonString = JsonConvert.SerializeObject(jsonObject); byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString); // Encrypt the JSON bytes using the AES cipher with CBC mode and PKCS5 padding byte[] encryptedBytes; using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using (var encryptor = aes.CreateEncryptor()) { encryptedBytes = encryptor.TransformFinalBlock(jsonBytes, 0, jsonBytes.Length); } } string encryptedString = Encoding.UTF8.GetString(encryptedBytes); // Decrypt the encrypted bytes using the same AES cipher with CBC mode and PKCS5 padding byte[] decryptedBytes; using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using (var decryptor = aes.CreateDecryptor()) { decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); } } // Convert the decrypted bytes back to a JSON object string decryptedString = Encoding.UTF8.GetString(decryptedBytes); var decryptedJsonObject = JsonConvert.DeserializeObject(decryptedString); // Print the original and decrypted JSON objects Console.WriteLine("Original JSON object: " + jsonString); Console.WriteLine("Encrypted JSON object: " + encryptedString); Console.WriteLine("Decrypted JSON object: " + decryptedString); } }Amit MohantyRank #112 · 20K reputationPosted Apr 4, 2023, 1:47 AM EDT
Hey Jaya Prakash, can you please provide sample of your json data ?
Jaya PrakashRank #1,293 · 1.3K reputationPosted Apr 4, 2023, 1:26 AM EDT
@Amit Not Working sir,
throwing exception the input data is not a complete block.