i can able to Encrypt the json object
My object
Bash
{id:487,mobile:9966368646,name :"jaya sai prakash",lname:"koyya",date:"2023-03-15",gender:"male",salary:"10000",location:"Nidadavolu"}
but when i try to Decrypt the Encrypted data it is displaying
like this It is Showing Some symbols how to get back the original json data from this

This is For Encryption
C#
protected void Button1_Click(object sender, EventArgs e)
{
var encrypted = Convert.ToString(TextEncrypt.Text);
string jsonString=JsonConvert.SerializeObject(encrypted);
TextDecrypt.Text = "";
string publickey = "santhosh";
string secretkey = "engineer";
byte[] secretkeyByte = { };
secretkeyByte = Encoding.UTF8.GetBytes(secretkey);
byte[] publickeybyte = { };
publickeybyte = Encoding.UTF8.GetBytes(publickey);
MemoryStream ms = null;
CryptoStream cs = null;
byte[] encryptedResult = Encoding.UTF8.GetBytes(encrypted);
RijndaelManaged aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Padding = PaddingMode.Zeros;
aes.Mode = CipherMode.CBC;
byte[] encodedTextBytes = Encoding.UTF8.GetBytes(TextEncrypt.Text);
TextDecrypt.Text = Convert.ToBase64String(encodedTextBytes);
// Response.Write(TextDecrypt.Text);
// var toDecodeAsString = System.Convert.FromBase64String(encrypted);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateEncryptor(publickeybyte, secretkeyByte), CryptoStreamMode.Write);
cs.Write(encryptedResult, 0, encryptedResult.Length);
cs.FlushFinalBlock();
// Label1.Text = encryptedResult;
TextDecrypt.Text =Convert.ToBase64String(encodedTextBytes);
Label1.Text =Convert.ToBase64String(encodedTextBytes);
}
}This is my code for Decryption
C#
protected void Button2_Click(object sender, EventArgs e)
{
string Decrypt = Convert.ToString(TextDecrypt.Text);
// TextEncrypt.Text = "";
string publickey = "santhosh";
string privatekey = "engineer";
RijndaelManaged aesAlg = null;
byte[] privatekeyByte = { };
privatekeyByte = Encoding.UTF8.GetBytes(privatekey);
byte[] publickeybyte = { };
publickeybyte = Encoding.UTF8.GetBytes(publickey);
MemoryStream ms = null;
CryptoStream cs = null;
aesAlg = new RijndaelManaged();
RijndaelManaged aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Padding = PaddingMode.Zeros;
aes.Mode = CipherMode.CBC;
byte[] bytes = Convert.FromBase64String(TextDecrypt.Text);
TextEncrypt.Text = Convert.ToBase64String(bytes);
byte[] inputbyteArray = new byte[Decrypt.Replace(" ", "+").Length];
inputbyteArray = Convert.FromBase64String(Decrypt.Replace(" ", "+"));
using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider())
{
ms = new MemoryStream(bytes);
cs = new CryptoStream(ms, DES.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
Encoding encoding = Encoding.UTF8;
TextEncrypt.Text = encoding.GetString(ms.ToArray());
Label2.Text = TextEncrypt.Text;
//TextEncrypt.Text = "";
Label1.Text = "";
}
}
Vishal JoshiRank #377 · 6.7K reputationPosted Mar 15, 2023, 7:18 AM EDT
Hello Jaya
Please replce the below code with your code. it should work.
protected void Button1_Click(object sender, EventArgs e) { var encrypted = string.Empty; string jsonString = JsonConvert.SerializeObject(encrypted); string publickey = "santhosh"; string secretkey = "engineer"; byte[] secretkeyByte = Encoding.UTF8.GetBytes(secretkey); byte[] publickeybyte = Encoding.UTF8.GetBytes(publickey); MemoryStream ms = null; CryptoStream cs = null; byte[] encryptedResult = Encoding.UTF8.GetBytes(TextEncrypt.Text); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { RijndaelManaged AES = new RijndaelManaged(); AES.KeySize = 256; AES.BlockSize = 128; ms = new MemoryStream(); cs = new CryptoStream(ms, des.CreateEncryptor(publickeybyte, secretkeyByte), CryptoStreamMode.Write); cs.Write(encryptedResult, 0, encryptedResult.Length); cs.FlushFinalBlock(); // Label1.Text = encryptedResult; TextEncrypt.Text = Convert.ToBase64String(encryptedResult); Label1.Text = Convert.ToBase64String(encryptedResult); } } protected void Button2_Click(object sender, EventArgs e) { string Decrypt = string.Empty; string publickey = "santhosh"; string privatekey = "engineer"; byte[] privatekeyByte = Encoding.UTF8.GetBytes(privatekey); byte[] publickeybyte = Encoding.UTF8.GetBytes(publickey); MemoryStream ms = null; CryptoStream cs = null; byte[] bytes = Convert.FromBase64String(TextDecrypt.Text); using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider()) { ms = new MemoryStream(bytes); cs = new CryptoStream(ms, DES.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write); cs.Write(bytes, 0, bytes.Length); Encoding encoding = Encoding.UTF8; TextEncrypt.Text = encoding.GetString(ms.ToArray()); Label2.Text = TextEncrypt.Text; Label1.Text = ""; } return Decrypt; }Thanks
Vishal
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 15, 2023, 7:24 AM EDT
Based on the code you provided, it looks like you are converting the JSON object to a string and then encrypting it. However, the decryption code doesn't include any JSON parsing or deserialization step to convert the decrypted string back to a JSON object.
To fix this, you need to add a step in the decryption code to deserialize the decrypted JSON string into a JSON object. You can use the JsonConvert.DeserializeObject method from the Newtonsoft.Json library to do this.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 15, 2023, 7:25 AM EDT
you can modify your decryption code to deserialize the decrypted JSON string
protected void Button2_Click(object sender, EventArgs e) { string encrypted = Convert.ToString(TextDecrypt.Text); string publickey = "santhosh"; string privatekey = "engineer"; byte[] privatekeyByte = Encoding.UTF8.GetBytes(privatekey); byte[] publickeybyte = Encoding.UTF8.GetBytes(publickey); using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider()) { byte[] bytes = Convert.FromBase64String(encrypted.Replace(" ", "+")); ms = new MemoryStream(bytes); cs = new CryptoStream(ms, DES.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write); cs.Write(bytes, 0, bytes.Length); cs.FlushFinalBlock(); string decrypted = Encoding.UTF8.GetString(ms.ToArray()); // Deserialize the decrypted JSON string into a JSON object JObject obj = JsonConvert.DeserializeObject(decrypted); // Access the individual properties of the JSON object int id = obj.Value("id"); string mobile = obj.Value("mobile"); string name = obj.Value("name"); string lname = obj.Value("lname"); string date = obj.Value("date"); string gender = obj.Value("gender"); string salary = obj.Value("salary"); string location = obj.Value("location"); // Do something with the individual properties // ... // Update the UI with the decrypted JSON string TextEncrypt.Text = decrypted; Label2.Text = decrypted; Label1.Text = ""; } }this code assumes that the encrypted JSON string was created using the same encryption algorithm and keys as the encryption code you provided. If the encryption method has been changed, you may need to modify the decryption code accordingly.
Vishal JoshiRank #377 · 6.7K reputationPosted Mar 15, 2023, 7:26 AM EDT
Replace with below code.
protected void Button2_Click(object sender, EventArgs e) { string Decrypt = string.Empty; string publickey = "santhosh"; string privatekey = "engineer"; byte[] privatekeyByte = Encoding.UTF8.GetBytes(privatekey); byte[] publickeybyte = Encoding.UTF8.GetBytes(publickey); MemoryStream ms = null; CryptoStream cs = null; byte[] bytes = Convert.FromBase64String(TextDecrypt.Text); using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider()) { ms = new MemoryStream(bytes); cs = new CryptoStream(ms, DES.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write); cs.Write(bytes, 0, bytes.Length); Encoding encoding = Encoding.UTF8; TextEncrypt.Text = encoding.GetString(ms.ToArray()); Label2.Text = TextEncrypt.Text; Label1.Text = ""; } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 12:42 AM EDT
protected void Button2_Click(object sender, EventArgs e) { string Decrypt = string.Empty; string publickey = "santhosh"; string privatekey = "engineer"; byte[] privatekeyByte = Encoding.UTF8.GetBytes(privatekey); byte[] publickeybyte = Encoding.UTF8.GetBytes(publickey); MemoryStream ms = null; CryptoStream cs = null; byte[] bytes = Convert.FromBase64String(TextDecrypt.Text); using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider()) { ms = new MemoryStream(bytes); cs = new CryptoStream(ms, DES.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write); cs.Write(bytes, 0, bytes.Length); Encoding encoding = Encoding.UTF8; TextEncrypt.Text = encoding.GetString(ms.ToArray()); Label2.Text = TextEncrypt.Text; Label1.Text = ""; } }here are a few improvements that can be made to this code done by @Vishal:
1) Instead of using the obsolete DESCryptoServiceProvider class, you can use the AesCryptoServiceProvider class which is more secure and provides better performance.
2) The CryptoStreamMode.Write mode should be changed to CryptoStreamMode.Read, since we want to read from the CryptoStream and not write to it.
3) The code should ensure that the CryptoStream is properly flushed and closed after use, even in case of exceptions, by using the "using" statement for both the MemoryStream and the CryptoStream.
improved version of the code that incorporates these changes:
protected void Button2_Click(object sender, EventArgs e) { string publickey = "santhosh"; string privatekey = "engineer"; byte[] privatekeyByte = Encoding.UTF8.GetBytes(privatekey); byte[] publickeybyte = Encoding.UTF8.GetBytes(publickey); byte[] bytes = Convert.FromBase64String(TextDecrypt.Text); using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { aes.Key = privatekeyByte; aes.IV = publickeybyte; using (MemoryStream ms = new MemoryStream(bytes)) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) { using (StreamReader reader = new StreamReader(cs)) { string decryptedText = reader.ReadToEnd(); TextEncrypt.Text = decryptedText; Label2.Text = decryptedText; Label1.Text = ""; } } } } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 12:43 AM EDT
we use the AesCryptoServiceProvider class and set the Key and IV properties directly from the private and public keys, respectively. We then use the "using" statement to ensure that the MemoryStream, CryptoStream, and StreamReader are all properly disposed of after use. We use the Read mode for the CryptoStream and the ReadToEnd method of the StreamReader to read the decrypted text.
Jaya PrakashRank #1,293 · 1.3K reputationPosted Mar 15, 2023, 7:24 AM EDT
hi sir @ Vishal
getting error at return Decrypt
if i change return type void to string it will give us an error