I should do Encrypt and Decrypt this json object i dont know how to do what should i take pls help me out...
i should take two textboxes one for encrypt and another for decrypt in ecrypt txtbox i should enter this json object how to do it..
how to enter json object into a textbox pls help me
Bash
{
id:,
mobile:,
name :"",
lname:"",
date:"",
gender:"",
salary:"",
location:""
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 15, 2023, 1:02 AM EDT
Hello Jaya,
First, you need to convert the
jsonObjectto a JSON string. Use Newtonsoft.Json package for that.using Newtonsoft.Json; // ... var jsonObject = new { id = 1, mobile = "1234567890", name = "John", lname = "Doe", date = "2023-03-15", gender = "Male", salary = 50000, location = "New York" }; string jsonString = JsonConvert.SerializeObject(jsonObject);Then you can use the
AesCryptoServiceProviderclass to encrypt and decrypt the string.example of how you can encrypt the JSON string:
using System.Security.Cryptography; using System.Text; // ... string key = "mySecretKey1234567890"; // the encryption key byte[] keyBytes = Encoding.UTF8.GetBytes(key); using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { aes.Key = keyBytes; aes.GenerateIV(); byte[] inputBytes = Encoding.UTF8.GetBytes(jsonString); byte[] encryptedBytes; using (ICryptoTransform encryptor = aes.CreateEncryptor()) { encryptedBytes = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length); } string encryptedString = Convert.ToBase64String(aes.IV.Concat(encryptedBytes).ToArray()); // Now you can display the encrypted string in the encrypt textbox encryptTextBox.Text = encryptedString; }To decrypt the encrypted string, you can use the following code:
using System.Security.Cryptography; // ... string encryptedString = encryptTextBox.Text; byte[] encryptedBytes = Convert.FromBase64String(encryptedString); byte[] ivBytes = encryptedBytes.Take(16).ToArray(); // first 16 bytes are the IV byte[] encryptedDataBytes = encryptedBytes.Skip(16).ToArray(); // the rest is the encrypted data string key = "mySecretKey1234567890"; // the encryption key byte[] keyBytes = Encoding.UTF8.GetBytes(key); using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { aes.Key = keyBytes; aes.IV = ivBytes; byte[] decryptedBytes; using (ICryptoTransform decryptor = aes.CreateDecryptor()) { decryptedBytes = decryptor.TransformFinalBlock(encryptedDataBytes, 0, encryptedDataBytes.Length); } string decryptedString = Encoding.UTF8.GetString(decryptedBytes); // Now you can display the decrypted string in the decrypt textbox decryptTextBox.Text = decryptedString; }Thanks
Naimish Makwana
Amit MohantyRank #112 · 20K reputationPosted Mar 15, 2023, 1:09 AM EDT
Check this
public static class StringCipher { private const int Keysize = 256; // This constant determines the number of iterations for the password bytes generation function. private const int DerivationIterations = 1000; public static string passPhrase = "secretkey"; public static string Encrypt(string plainText) { // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text // so that the same Salt and IV values can be used when decrypting. var saltStringBytes = Generate256BitsOfRandomEntropy(); var ivStringBytes = Generate256BitsOfRandomEntropy(); var plainTextBytes = Encoding.UTF8.GetBytes(plainText); using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) { var keyBytes = password.GetBytes(Keysize / 8); using (var symmetricKey = new RijndaelManaged()) { symmetricKey.BlockSize = 256; symmetricKey.Mode = CipherMode.CBC; symmetricKey.Padding = PaddingMode.PKCS7; using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes)) { using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes. var cipherTextBytes = saltStringBytes; cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray(); cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray(); memoryStream.Close(); cryptoStream.Close(); return Convert.ToBase64String(cipherTextBytes); } } } } } } public static string Decrypt(string cipherText) { if (cipherText == null) { return null; } else { // Get the complete stream of bytes that represent: // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText] var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText); // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes. var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray(); // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes. var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray(); // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string. var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray(); using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) { var keyBytes = password.GetBytes(Keysize / 8); using (var symmetricKey = new RijndaelManaged()) { symmetricKey.BlockSize = 256; symmetricKey.Mode = CipherMode.CBC; symmetricKey.Padding = PaddingMode.PKCS7; using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)) { using (var memoryStream = new MemoryStream(cipherTextBytes)) { using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { var plainTextBytes = new byte[cipherTextBytes.Length]; var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); } } } } } } } private static byte[] Generate256BitsOfRandomEntropy() { var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits. using (var rngCsp = new RNGCryptoServiceProvider()) { // Fill the array with cryptographically secure random bytes. rngCsp.GetBytes(randomBytes); } return randomBytes; } }How to use
var person = new { Name = "John Doe", Age = 30, Email = "[email protected]" }; string jsonData = JsonConvert.SerializeObject(person); string encryptedJson = StringCipher.Encrypt(person.ToString()); var decryptedPerson = StringCipher.Decrypt(encryptedJson);Jaya PrakashRank #1,293 · 1.3K reputationPosted Mar 15, 2023, 5:00 AM EDT
getting error like this i tried but none worked out...