Tarun

Tarun

  • 1.3k
  • 447
  • 17.1k

Encrypted And Decrypted

Mar 9 2023 6:30 AM
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Input;

namespace EncryptedandDecrypted
{
    public partial class EncryptedAndDecrypted : System.Web.UI.Page
    {
     

        public class EncryptionUtility
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               //rijndaelCipher.Key = Convert.FromBase64String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678912");
               //rijndaelCipher.IV = Convert.FromBase64String("1234567890123456789012345678901234567890123456789012345678901234");
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        { 
         try
        {
               string encrypted = Convert.ToString(TextBox1.Text);
                TextBox2.Text = "";
                string publickey = "santhosh";
                string secretkey = "engineer";
                RijndaelManaged aesAlg = null;
                byte[] secretkeyByte = { };
                secretkeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey);
                byte[] publickeybyte = { };
                publickeybyte = System.Text.Encoding.UTF8.GetBytes(publickey);
                MemoryStream ms = null;
                CryptoStream cs = null;
                byte[] inputbyteArray = System.Text.Encoding.UTF8.GetBytes(encrypted);
                aesAlg = new RijndaelManaged();
                RijndaelManaged aes = new RijndaelManaged();
                aes.KeySize = 256;
                aes.BlockSize = 256;
                aes.Padding = PaddingMode.Zeros;
                aes.Mode = CipherMode.CBC;
                var toDecodeAsString = System.Convert.FromBase64String(encrypted);
                string decodedValue = System.Text.ASCIIEncoding.ASCII.GetString(toDecodeAsString);
                
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                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(inputbyteArray, 0, inputbyteArray.Length);
                    cs.FlushFinalBlock();
                    TextBox2.Text = Convert.ToBase64String(ms.ToArray());
                  //  TextBox2.Text = TextBox1.Text;
                }
               // TextBox1.Text = "";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        } 
        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
              
                string Decrypt = Convert.ToString(TextBox2.Text);
                TextBox1.Text = "";
                string publickey = "santhosh";
                string privatekey = "engineer";
                RijndaelManaged aesAlg = null;
                byte[] privatekeyByte = { };
                privatekeyByte = System.Text.Encoding.UTF8.GetBytes(privatekey);
                byte[] publickeybyte = { };
                publickeybyte = System.Text.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[] inputbyteArray = new byte[Decrypt.Replace(" ", "+").Length];
                inputbyteArray = Convert.FromBase64String(Decrypt.Replace(" ", "+"));
                using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider())
                {
                    ms = new MemoryStream();
                    cs = new CryptoStream(ms, DES.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write);
                    cs.Write(inputbyteArray, 0, inputbyteArray.Length);
                    cs.FlushFinalBlock();
                    Encoding encoding = Encoding.UTF8;
                    TextBox1.Text = encoding.GetString(ms.ToArray());                   
                }
            }
            catch (Exception ae)
            {
                throw new Exception(ae.Message, ae.InnerException);
            }
        }
    } 
}

When I enter to Encrypted value in the TextBox when I Click On  Decrypted  buttion t is Chainging so My Requriment is when i Try to check theEncrypted data It Should not be change


Answers (3)