91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中如何實現AES算法加密

發布時間:2023-02-27 09:25:13 來源:億速云 閱讀:113 作者:iii 欄目:開發技術

這篇文章主要介紹了C#中如何實現AES算法加密的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#中如何實現AES算法加密文章都會有所收獲,下面我們一起來看看吧。

先上效果圖

文件和加密文件之間的轉換。

C#中如何實現AES算法加密

先添加輔助類

 public class AES_EnorDecrypt
    {
        //定義默認密鑰
        private static byte[] _aesKeyByte = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        private static string _aesKeyStr = Encoding.UTF8.GetString(_aesKeyByte);

        /// <summary>
        /// 隨機生成密鑰,默認密鑰長度為32,不足在加密時自動填充空格
        /// </summary>
        /// <param name="n">密鑰長度</param>
        /// <returns></returns>
        public static string GetIv(int n)
        {
            string s = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char[] arrChar = new char[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                arrChar[i] = Convert.ToChar(s.Substring(i, 1));
            }
            StringBuilder num = new StringBuilder();
            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < n; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
            }
            _aesKeyByte = Encoding.UTF8.GetBytes(num.ToString());
            return _aesKeyStr = Encoding.UTF8.GetString(_aesKeyByte);
        }

        /// <summary>
        /// AES加密,針對文本類文件
        /// </summary>
        /// <param name="Data">被加密的明文</param>
        /// <param name="Key">密鑰</param>
        /// <param name="Vector">密鑰向量</param>
        /// <returns>密文</returns>
        public static string AESEncrypt(string Data, string Key, string Vector)
        {
            byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] Cryptograph = null;//加密后的密文
            Rijndael Aes = Rijndael.Create();
            try
            {
                using (MemoryStream Memory = new MemoryStream())
                {
                    //把內存流對象包裝成加密流對象
                    using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                    {
                        Encryptor.Write(plainBytes, 0, plainBytes.Length);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
            catch
            {
                Cryptograph = null;
            }
            return Convert.ToBase64String(Cryptograph);
        }

        /// <summary>
        /// AES加密,任意文件
        /// </summary>
        /// <param name="Data">被加密的明文</param>
        /// <param name="Key">密鑰</param>
        /// <param name="Vector">密鑰向量</param>
        /// <returns>密文</returns>
        public static byte[] AESEncrypt(byte[] Data, string Key, string Vector)
        {
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] Cryptograph = null;//加密后的密文
            Rijndael Aes = Rijndael.Create();
            try
            {
                using (MemoryStream Memory = new MemoryStream())
                {
                    //把內存流對象包裝成加密流對象
                    using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                    {
                        Encryptor.Write(Data, 0, Data.Length);
                        Encryptor.FlushFinalBlock();
                        Cryptograph = Memory.ToArray();
                    }
                }
            }
            catch
            {
                Cryptograph = null;
            }
            return Cryptograph;
        }

        /// <summary>
        /// AES解密,針對文本文件
        /// </summary>
        /// <param name="Data">被解密的密文</param>
        /// <param name="Key">密鑰</param>
        /// <param name="Vector">密鑰向量</param>
        /// <returns>明文</returns>
        public static string AESDecrypt(string Data, string Key, string Vector)
        {
            byte[] encryptedBytes = Convert.FromBase64String(Data);
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] original = null;//解密后的明文
            Rijndael Aes = Rijndael.Create();
            try
            {
                using (MemoryStream Memory = new MemoryStream(encryptedBytes))
                {
                    //把內存流對象包裝成加密對象
                    using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                    {
                        //明文存儲區
                        using (MemoryStream originalMemory = new MemoryStream())
                        {
                            byte[] Buffer = new byte[1024];
                            int readBytes = 0;
                            while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
                            {
                                originalMemory.Write(Buffer, 0, readBytes);
                            }
                            original = originalMemory.ToArray();
                        }
                    }
                }
            }
            catch
            {
                original = null;
            }
            return Encoding.UTF8.GetString(original);
        }

        /// <summary>
        /// AES解密,任意文件
        /// </summary>
        /// <param name="Data">被解密的密文</param>
        /// <param name="Key">密鑰</param>
        /// <param name="Vector">密鑰向量</param>
        /// <returns>明文</returns>
        public static byte[] AESDecrypt(byte[] Data, string Key, string Vector)
        {
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] original = null;//解密后的明文
            Rijndael Aes = Rijndael.Create();
            try
            {
                using (MemoryStream Memory = new MemoryStream(Data))
                {
                    //把內存流對象包裝成加密對象
                    using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                    {
                        //明文存儲區
                        using (MemoryStream originalMemory = new MemoryStream())
                        {
                            byte[] Buffer = new byte[1024];
                            int readBytes = 0;
                            while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
                            {
                                originalMemory.Write(Buffer, 0, readBytes);
                            }
                            original = originalMemory.ToArray();
                        }
                    }
                }
            }
            catch
            {
                original = null;
            }
            return original;
        }
    }

AES是塊加密,塊的長度是16字節,如果原文不到16的字節,就會進行填充至16個字節。

開始實現

界面布局

C#中如何實現AES算法加密

textbox1為文件位置,textbox2為密碼。

C#中如何實現AES算法加密

C#中如何實現AES算法加密

C#中如何實現AES算法加密

設置好后

public partial class FormAes : Form
    {
        #region 屬性
        private static string _aesKeyVector = "q2T_=R/*33vc";
        #endregion

        public FormAes()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 選擇文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//該值確定是否可以選擇多個文件
            dialog.Title = "請選擇文件夾";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()) || string.IsNullOrWhiteSpace(textBox2.Text.Trim()))
                return;
            backgroundWorker1.RunWorkerAsync();
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()) || string.IsNullOrWhiteSpace(textBox2.Text.Trim()))
                return;
            backgroundWorker2.RunWorkerAsync();
        }


        /// <summary>
        /// 后臺線程執行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           
            string KeyVector = _aesKeyVector;//密鑰向量
            string path = Path.GetDirectoryName(textBox1.Text);
            string name = Path.GetFileName(textBox1.Text);
            name += ".En";
            #region   加密
            FileStream sr = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
            FileStream sw = new FileStream(path + "\\" + name, FileMode.Create, FileAccess.Write);

            if (sr.Length > 50 * 1024 * 1024)//如果文件大于50M,采取分塊加密,按50MB讀寫
            {
                byte[] mybyte = new byte[52428800];//每50MB加密一次                  
                int numBytesRead = 52428800;//每次加密的流大小
                long leftBytes = sr.Length;//剩余需要加密的流大小
                long readBytes = 0;//已經讀取的流大小
                //每50MB加密后會變成50MB+16B
                byte[] encrpy = new byte[52428816];
                while (true)
                {
                    if (leftBytes > numBytesRead)
                    {
                        sr.Read(mybyte, 0, mybyte.Length);
                        encrpy = AES_EnorDecrypt.AESEncrypt(mybyte, textBox2.Text, KeyVector);
                        sw.Write(encrpy, 0, encrpy.Length);
                        leftBytes -= numBytesRead;
                        readBytes += numBytesRead;
                        backgroundWorker1.ReportProgress((int)(readBytes * 100 / sr.Length));
                    }
                    else//重新設定讀取流大小,避免最后多余空值
                    {
                        byte[] newByte = new byte[leftBytes];
                        sr.Read(newByte, 0, newByte.Length);
                        byte[] newWriteByte;
                        newWriteByte = AES_EnorDecrypt.AESEncrypt(newByte, textBox2.Text, KeyVector);
                        sw.Write(newWriteByte, 0, newWriteByte.Length);
                        readBytes += leftBytes;
                        backgroundWorker1.ReportProgress((int)(readBytes * 100 / sr.Length));
                        break;
                    }
                }
            }
            else
            {
                byte[] mybyte = new byte[sr.Length];
                sr.Read(mybyte, 0, (int)sr.Length);
                mybyte = AES_EnorDecrypt.AESEncrypt(mybyte, textBox2.Text, KeyVector);
                sw.Write(mybyte, 0, mybyte.Length);
                backgroundWorker1.ReportProgress(100);
            }

            sr.Close();
            sw.Close();

            #endregion
        }
       
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        /// <summary>
        /// 執行完成時觸發
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("加密成功!");
        }

        /// <summary>
        /// 后臺線程執行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                string KeyVector = _aesKeyVector;//密鑰向量
                string path = Path.GetDirectoryName(textBox1.Text);
                string name = Path.GetFileName(textBox1.Text);
                if (name.EndsWith(".En"))
                {
                    name = name.Remove(name.Length - 3, 3);
                }

                #region  解密
                FileStream sr = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
                FileStream sw = new FileStream(path + "\\" + name, FileMode.Create, FileAccess.Write);

                if (sr.Length > 50 * 1024 * 1024)//如果文件大于50M,采取分塊解密,按50MB讀寫
                {
                    byte[] mybyte = new byte[52428816];//解密緩沖區50MB+16B
                    byte[] decrpt = new byte[52428800];//解密后的50MB
                    int numBytesRead = 52428816;//每次解密的流大小
                    long leftBytes = sr.Length;//剩余需要解密的流大小
                    long readBytes = 0;//已經讀取的流大小
                    try
                    {
                        while (true)
                        {
                            if (leftBytes > numBytesRead)
                            {
                                sr.Read(mybyte, 0, mybyte.Length);
                                decrpt = AES_EnorDecrypt.AESDecrypt(mybyte, textBox2.Text, KeyVector);
                                sw.Write(decrpt, 0, decrpt.Length);
                                leftBytes -= numBytesRead;
                                readBytes += numBytesRead;
                                backgroundWorker2.ReportProgress((int)(readBytes * 100 / sr.Length));
                            }
                            else//重新設定讀取流大小,避免最后多余空值
                            {
                                byte[] newByte = new byte[leftBytes];
                                sr.Read(newByte, 0, newByte.Length);
                                byte[] newWriteByte;
                                newWriteByte = AES_EnorDecrypt.AESDecrypt(newByte, textBox2.Text, KeyVector);
                                sw.Write(newWriteByte, 0, newWriteByte.Length);
                                readBytes += leftBytes;
                                backgroundWorker2.ReportProgress((int)(readBytes * 100 / sr.Length));
                                break;
                            }
                        }
                    }
                    catch
                    {
                        sr.Close();
                        sw.Close();
                        File.Delete(path + "\\" + name);
                        e.Cancel = true;
                    }
                    sr.Close();
                    sw.Close();
                }
                else
                {
                    byte[] mybyte = new byte[(int)sr.Length];
                    sr.Read(mybyte, 0, (int)sr.Length);
                    try
                    {
                        mybyte = AES_EnorDecrypt.AESDecrypt(mybyte, textBox2.Text, KeyVector);
                        sw.Write(mybyte, 0, mybyte.Length);
                        backgroundWorker2.ReportProgress(100);
                    }
                    catch
                    {
                        sr.Close();
                        sw.Close();
                        File.Delete(path + "\\" + name);
                        e.Cancel = true;
                    }
                    sr.Close();
                    sw.Close();
                }
                #endregion
            }
            catch
            {
                e.Cancel = true;
            }
        }

        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
           progressBar1.Value = e.ProgressPercentage;
        }

        /// <summary>
        /// 執行完成時觸發
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
               MessageBox.Show("解密失敗\n密碼錯誤或加密文件被篡改,無法解密");                
            }
            else
            {
                MessageBox.Show("解密成功!");
            }
        }   
    }

關于“C#中如何實現AES算法加密”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#中如何實現AES算法加密”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

aes
AI

静宁县| 迁西县| 从江县| 古丈县| 大邑县| 中方县| 和静县| 慈利县| 乌鲁木齐市| 庆云县| 会宁县| 商丘市| 甘孜| 东宁县| 九台市| 栾川县| 盐边县| 治多县| 盐池县| 阿荣旗| 桐庐县| 汉川市| 南通市| 三门县| 霍州市| 广西| 永胜县| 延川县| 怀集县| 祁阳县| 齐河县| 轮台县| 陇南市| 肥东县| 靖边县| 漳平市| 元朗区| 林甸县| 蒲江县| 维西| 教育|