Jaya Prakash

Jaya Prakash

  • 548
  • 2.3k
  • 63.3k

Decrypting the Encrypted json object

Mar 18 2023 5:35 PM

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

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

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 = "";
                }
            }
        }
    }
}


Answers (7)