C#對稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例
更新時(shí)間:2015年07月04日 09:22:45 投稿:junjie
這篇文章主要介紹了C#對稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例,每次解密時(shí)從密文中截取前16位,這就是實(shí)現(xiàn)隨機(jī)的奧秘,本文同時(shí)給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下
思路:使用隨機(jī)向量,把隨機(jī)向量放入密文中,每次解密時(shí)從密文中截取前16位,其實(shí)就是我們之前加密的隨機(jī)向量。
代碼:
public static string Encrypt(string plainText, string AESKey) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字節(jié)數(shù)組 rijndaelCipher.Key = Convert.FromBase64String(AESKey);//加解密雙方約定好密鑰:AESKey rijndaelCipher.GenerateIV(); byte[] keyIv = rijndaelCipher.IV; byte[] cipherBytes = null; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, rijndaelCipher.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cipherBytes = ms.ToArray();//得到加密后的字節(jié)數(shù)組 cs.Close(); ms.Close(); } } var allEncrypt = new byte[keyIv.Length + cipherBytes.Length]; Buffer.BlockCopy(keyIv, 0, allEncrypt, 0, keyIv.Length); Buffer.BlockCopy(cipherBytes, 0, allEncrypt, keyIv.Length * sizeof(byte), cipherBytes.Length); return Convert.ToBase64String(allEncrypt); } public static string Decrypt(string showText, string AESKey) { string result = string.Empty; try { byte[] cipherText = Convert.FromBase64String(showText); int length = cipherText.Length; SymmetricAlgorithm rijndaelCipher = Rijndael.Create(); rijndaelCipher.Key = Convert.FromBase64String(AESKey);//加解密雙方約定好的密鑰 byte[] iv = new byte[16]; Buffer.BlockCopy(cipherText, 0, iv, 0, 16); rijndaelCipher.IV = iv; byte[] decryptBytes = new byte[length - 16]; byte[] passwdText = new byte[length - 16]; Buffer.BlockCopy(cipherText, 16, passwdText, 0, length - 16); using (MemoryStream ms = new MemoryStream(passwdText)) { using (CryptoStream cs = new CryptoStream(ms, rijndaelCipher.CreateDecryptor(), CryptoStreamMode.Read)) { cs.Read(decryptBytes, 0, decryptBytes.Length); cs.Close(); ms.Close(); } } result = Encoding.UTF8.GetString(decryptBytes).Replace("\0", ""); ///將字符串后尾的'\0'去掉 } catch { } return result; }
調(diào)用:
string jiaMi = MyAESTools.Encrypt(textBox1.Text, "abcdefgh12345678abcdefgh12345678"); string jieMi = MyAESTools.Decrypt(textBox3.Text, "abcdefgh12345678abcdefgh12345678");
相關(guān)文章
C# FileStream實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了C# FileStream實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03C#針對xml文件轉(zhuǎn)化Dictionary的方法
這篇文章主要介紹了C#針對xml文件轉(zhuǎn)化Dictionary的方法,是C#操作XML文件的典型應(yīng)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01c#的時(shí)間日期操作示例分享(c#獲取當(dāng)前日期)
這篇文章主要介紹了c#的時(shí)間日期操作示例,在給定時(shí)間戳返回指定的時(shí)間格式和獲取當(dāng)前時(shí)間方法,需要的朋友可以參考下2014-03-03C#實(shí)現(xiàn)在匿名方法中捕獲外部變量的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在匿名方法中捕獲外部變量的方法,本文直接給出代碼實(shí)例,然后分析了代碼中的一些知識點(diǎn),需要的朋友可以參考下2015-03-03C#實(shí)現(xiàn)TCP連接信息統(tǒng)計(jì)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)TCP連接信息統(tǒng)計(jì)的方法,可實(shí)現(xiàn)有效獲取TCP連接信息及連接狀態(tài)的功能,需要的朋友可以參考下2015-07-07C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐
有時(shí)我們可能會遇到需要的資料或教程被分成了幾部分存放在多個PDF文件中,本文主要介紹了C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01