您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)ASP.NET中怎么實(shí)現(xiàn)數(shù)據(jù)加密,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
文中以Rijndael算法為例:
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace DataCrypto { /// ﹤summary﹥ /// ASP.NET數(shù)據(jù)加密之對(duì)稱(chēng)加密算法類(lèi) /// ﹤/summary﹥ public class SymmetricMethod { private SymmetricAlgorithm mobjCryptoService; private string Key; /// ﹤summary﹥ /// ASP.NET數(shù)據(jù)加密之對(duì)稱(chēng)加密類(lèi)的構(gòu)造函數(shù) /// ﹤/summary﹥ public SymmetricMethod() { mobjCryptoService = new RijndaelManaged(); Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7"; } /// ﹤summary﹥ ///ASP.NET數(shù)據(jù)加密之獲得密鑰 /// ﹤/summary﹥ /// ﹤returns﹥密鑰﹤/returns﹥ private byte[] GetLegalKey() { string sTemp = Key; mobjCryptoService.GenerateKey(); byte[] bytTemp = mobjCryptoService.Key; int KeyLength = bytTemp.Length; if (sTemp.Length ﹥ KeyLength) sTemp = sTemp.Substring(0, KeyLength); else if (sTemp.Length ﹤ KeyLength) sTemp = sTemp.PadRight(KeyLength, ' '); return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// ﹤summary﹥ /// ASP.NET數(shù)據(jù)加密之獲得初始向量IV /// ﹤/summary﹥ /// ﹤returns﹥初試向量IV﹤/returns﹥ private byte[] GetLegalIV() { string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk"; mobjCryptoService.GenerateIV(); byte[] bytTemp = mobjCryptoService.IV; int IVLength = bytTemp.Length; if (sTemp.Length ﹥ IVLength) sTemp = sTemp.Substring(0, IVLength); else if (sTemp.Length ﹤ IVLength) sTemp = sTemp.PadRight(IVLength, ' '); return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// ﹤summary﹥ /// ASP.NET數(shù)據(jù)加密之加密方法 /// ﹤/summary﹥ /// ﹤param name="Source"﹥待加密的串﹤/param﹥ /// ﹤returns﹥經(jīng)過(guò)加密的串﹤/returns﹥ public string Encrypto(string Source) { byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source); MemoryStream ms = new MemoryStream(); mobjCryptoService.Key = GetLegalKey(); mobjCryptoService.IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return Convert.ToBase64String(bytOut); } /// ﹤summary﹥ /// ASP.NET數(shù)據(jù)加密之解密方法 /// ﹤/summary﹥ /// ﹤param name="Source"﹥待解密的串﹤/param﹥ /// ﹤returns﹥經(jīng)過(guò)解密的串﹤/returns﹥ public string Decrypto(string Source) { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); mobjCryptoService.Key = GetLegalKey(); mobjCryptoService.IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } } }
關(guān)于ASP.NET中怎么實(shí)現(xiàn)數(shù)據(jù)加密就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。