How To Decrypt Encrypted json object to its origial state

jaya prakash 0 Reputation points
2023-03-16T14:52:50.62+00:00

I can Able to Encrypted the json object but when i try to decrypt the encrypted data to json

it is showing different response pls help me

When i try to debug im getting different symbols idk how to do it pls help me

Screenshot (22)

This is my encryption 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);
                
            }
        }
this is my json object


{id:487,mobile:9966368646,name :"jaya sai prakash",lname:"koyya",date:"2023-03-15",gender:"male",salary:"10000",location:"Nidadavolu"}





This is My code for decryption



 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 = "";
                        }
                    }
                }
            }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,250 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,229 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,366 Reputation points
    2023-03-16T20:25:06.4966667+00:00

    your encryption and decryption code do not agree on keys or the encryption algorithm.

    0 comments No comments

  2. QiYou-MSFT 4,306 Reputation points Microsoft Vendor
    2023-03-24T07:28:35.9733333+00:00

    Hi @jaya prakash

    I modified your answer code as follows:

     const string KEY_64 = "mySecretKey1234567890engineer123";
            const string IV_64 = "santhosh";
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            public static string EnCrypt(string strContent, string strKey)
            {
                if (string.IsNullOrEmpty(strContent)) return string.Empty;
                if (strKey.Length > 8) strKey = strKey.Substring(0, 8); else strKey = KEY_64;
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strKey);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                int i = cryptoProvider.KeySize;
                MemoryStream ms = new MemoryStream();
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cst);
                sw.Write(strContent);
                sw.Flush();
                cst.FlushFinalBlock();
                sw.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            }
            public static string DeCrypt(string strContent, string strKey)
            {
                if (string.IsNullOrEmpty(strContent)) return string.Empty;
                if (strKey.Length > 8) strKey = strKey.Substring(0, 8); else strKey = KEY_64;
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strKey);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
                byte[] byEnc;
                try
                {
                    byEnc = Convert.FromBase64String(strContent);
                }
                catch
                {
                    return null;
                }
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                TextBox3.Text = EnCrypt(TextBox1.Text, TextBox2.Text);
            }
            protected void Button2_Click(object sender, EventArgs e)
            {
                TextBox6.Text = DeCrypt(TextBox4.Text, TextBox5.Text);
            }
    
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server">Please enter the encrypted string</asp:Label>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
            <div>
                <asp:Label ID="Label2" runat="server">Please enter the key</asp:Label>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </div>
            <div>
                <asp:Button ID="Button1" runat="server" Text="Encrypt" OnClick="Button1_Click" />
            </div>
            <div>
                <asp:Label ID="Label3" runat="server"> The encrypted string</asp:Label>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </div>
            <br />
            <br />
            <br />
            <div>
                <asp:Label ID="Label4" runat="server"> Please enter the decrypted string</asp:Label>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </div>
            <div>
                <asp:Label ID="Label5" runat="server">Please enter the key</asp:Label>
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
            </div>
            <div>
                <asp:Button ID="Button2" runat="server" Text="Decrypt" OnClick="Button2_Click" />
            </div>
            <div>
                <asp:Label ID="Label6" runat="server"> The decrypted string</asp:Label>
                <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
            </div>
        </form>
    </body>
    

    KEY_64 is Key. We use this key every time we encrypt and decrypt.

    It is mainly implemented using the DESCryptoServiceProvider class.

    You can learn more form this document.

    DESCryptoServiceProvider

    Output:

    Test6

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments