C# 字符串解密加密的小程序

加密方法 ToEncrypt() 

解密方法 ToDecrypt()

借鉴于 C#对字符串进行加密解密 - 明志德道 - 博客园 (cnblogs.com)

成果展示

主要代码展示

using System.Security.Cryptography;
using System.Text;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            if (textBox2.Text.Length == 4)
            {
                try
                {
                    textBox3.Text = new Encrypt().ToEncrypt(textBox2.Text, textBox1.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
            }
        }

        internal class Encrypt
        {
            public string ToEncrypt(string encryptKey, string str)
            {
                try
                {
                    byte[] P_byte_key = Encoding.Unicode.GetBytes(encryptKey);
                    byte[] P_byte_data = Encoding.Unicode.GetBytes(str);
                    using (MemoryStream P_Stream_MS = new MemoryStream())
                    {
                        using (CryptoStream P_CryptStream_Stream = new CryptoStream(P_Stream_MS, new DESCryptoServiceProvider().CreateEncryptor(P_byte_key, P_byte_key), CryptoStreamMode.Write))
                        {
                            P_CryptStream_Stream.Write(P_byte_data, 0, P_byte_data.Length);
                            P_CryptStream_Stream.FlushFinalBlock();
                            byte[] P_bt_temp = P_Stream_MS.ToArray();
                            return Convert.ToBase64String(P_bt_temp);
                        }
                    }
                }
                catch (CryptographicException ce)
                {
                    throw new Exception(ce.Message);
                }
            }

            public string ToDecrypt(string encryptKey, string str)
            {
                try
                {
                    byte[] P_byte_key = Encoding.Unicode.GetBytes(encryptKey);
                    byte[] P_byte_data = Convert.FromBase64String(str);
                    using (MemoryStream P_Stream_MS = new MemoryStream(P_byte_data))
                    {
                        using (CryptoStream P_CryptStream_Stream = new CryptoStream(P_Stream_MS, new DESCryptoServiceProvider().CreateDecryptor(P_byte_key, P_byte_key), CryptoStreamMode.Read))
                        {
                            byte[] P_bt_temp = new byte[200];
                            using (MemoryStream P_MemoryStream_temp = new MemoryStream())
                            {
                                int i = 0;
                                while ((i = P_CryptStream_Stream.Read(P_bt_temp, 0, P_bt_temp.Length)) > 0)
                                {
                                    P_MemoryStream_temp.Write(P_bt_temp, 0, i);
                                }
                                return Encoding.Unicode.GetString(P_MemoryStream_temp.ToArray());
                            }
                        }
                    }
                }
                catch (CryptographicException ce)
                {
                    throw new Exception(ce.Message);
                }
            }
        }


    

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox5.Text.Length == 4)
            {
                try
                {
                    textBox4.Text = new Encrypt().ToDecrypt(textBox5.Text, textBox6.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

     
    }
}

猜你喜欢

转载自blog.csdn.net/qq_62238325/article/details/133917387
今日推荐