This issue is still unsolved for me
when decrypting the encrypted data i was trying different codes provided by our seniors but getting errors idk what to do
Few seniors helped me a lot but i am unable to solve it
Encrypted code
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);
}
}Code for Deryption provided by seniors
C#
protected void Button2_Click(object sender, EventArgs e)
{
string publickey = "santhoshsanthosh";
string privatekey = "mysecretkey1234567890engineer123";
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 18, 2023, 2:23 PM EDT
1. Check that the encrypted data is being correctly encoded and stored.
2. Check if the encryption and decryption algorithms are compatible
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 18, 2023, 2:24 PM EDT
Before attempting to decrypt the data, check that it has been correctly encoded and stored. Ensure that the data has not been altered or corrupted during transmission. Also check that the keys used for encryption and decryption are correct and match. A mismatched key can cause the decryption process to fail.
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 19, 2023, 3:50 AM EDT
Hello Jayay,
It seems you key is not same in encryption and decryption. .
Button1_Click
string publickey = "santhosh"; string secretkey = "engineer";Button2_Click
string publickey = "santhoshsanthosh"; string privatekey = "mysecretkey1234567890engineer123";Make this same and test.
Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 21, 2023, 5:44 AM EDT
This process is a valid way to encode and decode JSON strings using Base64. One thing to remeber that the code does not perform any validation or verification of the JSON string before encoding it, which could lead to encoding errors if the input is not a valid JSON string.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 21, 2023, 5:44 AM EDT
Try my version:
protected void Button1_Click(object sender, EventArgs e) { try { string json = inputTextBox.Text; if (IsValidJson(json)) { byte[] bytesToEncode = Encoding.UTF8.GetBytes(json); string encodedJson = Convert.ToBase64String(bytesToEncode); outputTextBox.Text = encodedJson; } else { Label1.Text = "Error: Input is not valid JSON"; } } catch (Exception ex) { Label1.Text = "Error: " + ex.Message; } } protected void Button2_Click(object sender, EventArgs e) { try { string encodedJson = outputTextBox.Text; byte[] bytesToDecode = Convert.FromBase64String(encodedJson); string decodedJson = Encoding.UTF8.GetString(bytesToDecode); if (IsValidJson(decodedJson)) { decryptedLabel.Text = decodedJson; } else { Label1.Text = "Error: Decoded output is not valid JSON"; } } catch (Exception ex) { Label1.Text = "Error: " + ex.Message; } } private bool IsValidJson(string json) { try { JToken.Parse(json); return true; } catch (JsonException) { return false; } }The IsValidJson method is added to validate that the input is a valid JSON string using the JToken.Parse method from the Newtonsoft.Json package. If the input is not valid JSON, the method returns false. In the Button1_Click and Button2_Click methods, the IsValidJson method is called to validate the input before encoding or decoding. If the input is not valid JSON, an error message is displayed instead of trying to encode or decode it. Exception handling is added to catch any exceptions that may occur during the encoding or decoding process. If an exception occurs, an error message is displayed with the exception message.
Jaya PrakashRank #1,293 · 1.3K reputationPosted Mar 19, 2023, 2:41 AM EDT
the data is correctly encoded and stored but the decryption is not
Jaya PrakashRank #1,293 · 1.3K reputationPosted Mar 20, 2023, 5:04 AM EDT
@Naimish Makwana and @Tuhin Paul
this code is giving correct output for my requirement is this correct process or wrong pls can u check it sir
protected void Button1_Click(object sender, EventArgs e) { try { string json = inputTextBox.Text; byte[] bytesToEncode = Encoding.UTF8.GetBytes(json); string encodedJson = Convert.ToBase64String(bytesToEncode); outputTextBox.Text = encodedJson; } catch (Exception ex) { Label1.Text = "Error: " + ex.Message; } } protected void Button2_Click(object sender, EventArgs e) { try { string encodedJson = outputTextBox.Text; byte[] bytesToDecode = Convert.FromBase64String(encodedJson); string decodedJson = Encoding.UTF8.GetString(bytesToDecode); decryptedLabel.Text = decodedJson; } catch (Exception ex) { Label1.Text = "Error: " + ex.Message; } }