Jaya Prakash

Jaya Prakash

  • 548
  • 2.3k
  • 63.3k

How to Decrypt

Mar 15 2023 10:03 AM

i can able to Encrypt the json object

My object

{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

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

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

Answers (7)