這篇文章將為大家詳細(xì)講解有關(guān)ASP.NET中怎么實(shí)現(xiàn)數(shù)據(jù)加密,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)公司是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶(hù)提供優(yōu)質(zhì)的資陽(yáng)托管服務(wù)器服務(wù)
文中以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ò),可以把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:ASP.NET中怎么實(shí)現(xiàn)數(shù)據(jù)加密
鏈接地址:http://aaarwkj.com/article12/ipdddc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、定制開(kāi)發(fā)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)站營(yíng)銷(xiāo)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)