Encrypt Decrypt string with Private key from AppSettings value using C# and VB.Net in ASP.Net

Last Reply 16 days ago By dharmendr

Posted 16 days ago

hello,

i used this code to encrypt my text, i want to ask i lost the key now is there any way i can dycrypt value without private key

        private string Encrypt(string clearText)
        {

            //Set your own encryption key
            string EncryptionKey = "MAKV2SPBNI99";
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }

 

 

You are viewing reply posted by: dharmendr 16 days ago.
Posted 16 days ago

Hi nauna,

Without key it is not possible.

But you can save the key in web config app setting and use that in code behind.

Refer below link.

Read AppSettings value from App.Config file using C# and VB.Net