您好,登錄后才能下訂單哦!
這篇文章主要介紹“ASP.NET中加密和解密怎么實現”,在日常操作中,相信很多人在ASP.NET中加密和解密怎么實現問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ASP.NET中加密和解密怎么實現”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
mscorlib.dll下的System.Security.Cryptography下:
抽象類HashAlgorithm
SHA256CryptoServiceProvider
SHA256Managed
SHA1CryptoServiceProvider密封類:調用Windows Crypto API
SHA1Managed普通類:用托管代碼寫的
MD5CryptoServiceProvider
抽象類MD5
SHA1
SHA256
SHA384
SHA512
class Program { static void Main(string[] args) { string str = "Hello World"; HashAlgorithm hashAlgorithm = HashAlgorithm.Create(HashAlgorithmType.SHA1); byte[] data = Encoding.Default.GetBytes(str); byte[] digest = hashAlgorithm.ComputeHash(data); foreach (byte b in digest) { Console.Write("{0:X}",b); } Console.ReadKey(); } } public class HashAlgorithmType { public const string SHA1 = "SHA1"; public const string SHA256 = "SHA256"; public const string SHA384 = "SHA384"; public const string SHA512 = "SHA512"; public const string MD5 = "MD5"; }
string key = "secret key"; byte[] data = Encoding.Default.GetBytes(key); KeyedHashAlgorithm kha = new HMACSHA1(); byte[] digest = kha.ComputeHash(data); foreach (byte b in digest) { Console.Write("{0:x}",b); }
SymmetricAlgorithm
RC2CryptoServiceProvider
RijindaelManaged
TripleDESCryptoServiceProvider
DESCryptoServiceProvider
DES
TripleDES
Rijndael
RC2
為了解決加密字符串加密后仍然有重復部分,引入IV,加密字符串即使有重復,也會被打亂。
IV值可以隨意指定,但長度固定,通常為64位byte類型
密匙長度也是固定的,通常為128位或196位byte類型
如果使用UTF8,會變長編碼
加密方法:CreateEncryptor(),返回ICryptoTransform接口類型
解密方法:CreateDecryptor(),返回ICrtyptoTransform接口類型
public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode) class Program { static void Main(string[] args) { #region 對稱加密和解密 string key = "secret key"; string str = "Hello World"; //加密 string encryptedText = SymmetricCryptoHelper.Encrypt(str, key); Console.WriteLine(encryptedText); //解密 string clearText = SymmetricCryptoHelper.Decrypt(encryptedText, key); Console.WriteLine(clearText); Console.ReadKey(); #endregion } } //對稱加密幫助類 public class SymmetricCryptoHelper { private ICryptoTransform encryptor; //加密器對象 private ICryptoTransform decryptor; //解密器對象 private const int BufferSize = 1024; public SymmetricCryptoHelper(string algorithmName, byte[] key) { SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName); provider.Key = key; provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; encryptor = provider.CreateEncryptor(); decryptor = provider.CreateDecryptor(); } public SymmetricCryptoHelper(byte[] key) : this("TripleDES", key){} //加密算法 public string Encrypt(string clearText) { //創建明文流 byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText); //byte[] clearBuffer = Encoding.Default.GetBytes(clearText); MemoryStream clearStream = new MemoryStream(clearBuffer); //創建空的密文流 MemoryStream encryptedStream = new MemoryStream(); //明文流和密文流轉換流,準備寫到密文流中 CryptoStream cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write); int bytesRead = 0; byte[] buffer = new byte[BufferSize]; do { //讀取明文流到buffer中 bytesRead = clearStream.Read(buffer, 0, BufferSize); //通過CryptoStream將buffer中的明文流字節數組寫到明文流中 cryptoStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); cryptoStream.FlushFinalBlock(); //獲取加密后的字節數組 buffer = encryptedStream.ToArray(); //將加密后的字節數組轉換成字符串 string encryptedText = Convert.ToBase64String(buffer); return encryptedText; } //解密算法 public string Decrypt(string encryptedText) { //把加密字符串轉換為加密字節數組 byte[] encryptedBuffer = Convert.FromBase64String(encryptedText); //創建密文流 Stream encryptedStream = new MemoryStream(encryptedBuffer); //創建空的明文流 MemoryStream clearStream = new MemoryStream(); //創建明文流和密文流的轉化流,讀取密文流 CryptoStream cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read); int bytesRead = 0; byte[] buffer = new byte[BufferSize]; do { //通過CryptoStream讀取密文流到Buffer bytesRead = cryptoStream.Read(buffer, 0, BufferSize); //把Buffer中的密文流寫到明文流中 clearStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); //將明文流轉換成字節數組 buffer = clearStream.GetBuffer(); string clearText = Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length); //string clearText = Encoding.Default.GetString(buffer, 0, (int)clearStream.Length); return clearText; } //密匙加密 public static string Encrypt(string clearText, string key) { byte[] keyData = new byte[16]; //TripleDES密匙固定長度為16個字節 //把密匙字符串轉換成字節數組 byte[] sourceData = Encoding.Default.GetBytes(key); int copyBytes = 16; if (sourceData.Length < 16) { copyBytes = sourceData.Length; } //把密匙數組復制到keyData字節數組中 Array.Copy(sourceData,keyData,copyBytes); SymmetricCryptoHelper helper = new SymmetricCryptoHelper(keyData); return helper.Encrypt(clearText); } //密匙解密 public static string Decrypt(string encryptedText, string key) { byte[] keyData = new byte[16]; byte[] sourceData = Encoding.Default.GetBytes(key); int copyBytes = 16; if (sourceData.Length < 16) { copyBytes = sourceData.Length; } Array.Copy(sourceData,keyData,copyBytes); SymmetricCryptoHelper helper = new SymmetricCryptoHelper(keyData); return helper.Decrypt(encryptedText); } }
AsymmetricAlgorithm
DSACryptoServiceProvider:只能進行認證模式,即數字簽名
RSACryptoServiceProvider
RSA
DSA
密匙為由開發者設定的字符串
通常是自動生成,不同的算法有不同的密匙格式
在創建RSACryptoServiceProvider實例時,會自動創建一個公/私密匙對。在實例上調用ToXmlString()方法獲得。
RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); string publicPrivate = provider.ToXmlString(true);//獲得公/私匙對 //string publicOnly = provider.ToXmlString(false); //只獲得公匙 Console.Write(publicPrivate); Console.ReadKey();
//非對稱加密幫助類 public class RSACryptoHelper { //加密 public static string Encrypt(string publicKeyXml, string plainText) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publicKeyXml); //使用公匙初始化對象 byte[] plainData = Encoding.Default.GetBytes(plainText); byte[] encryptedData = provider.Encrypt(plainData, true); return Convert.ToBase64String(encryptedData); } //解密 public static string Decrypt(string privateKeyXml, string encryptedText) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privateKeyXml); byte[] encryptedData = Convert.FromBase64String(encryptedText); byte[] plainData = provider.Decrypt(encryptedData, true); string plainText = Encoding.Default.GetString(plainData); return plainText; } }
RSACryptoServiceProvider或DSACryptoServiceProvider
SignData()對摘要進行簽名,并返回簽名后的摘要。
VerifyData()得出本地摘要,并解密傳遞進來的原始摘要,對比返回bool類型結果。
public class RSACryptoHelper { public static string SignData(string plainText, string privateKeyXml) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privateKeyXml); byte[] plainData = Encoding.Default.GetBytes(plainText); //設置獲取摘要的算法 HashAlgorithm sha1 = HashAlgorithm.Create("SHA1"); //獲取簽名過的摘要,是使用私匙加密過的摘要 byte[] signedDigest = provider.SignData(plainData, sha1); return Convert.ToBase64String(signedDigest); } public static bool VerifyData(string plainText, string signature, string publicKeyXml) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publicKeyXml); byte[] plainData = Encoding.Default.GetBytes(plainText); byte[] signedDigest = Convert.FromBase64String(signature); HashAlgorithm sha1 = HashAlgorithm.Create("SHA1"); bool isDataIntact = provider.VerifyData(plainData, sha1, signedDigest); return isDataIntact; } //使用SingnHash public static string SignData2(string plainText, string privateKeyXml) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privateKeyXml); byte[] plainData = Encoding.Default.GetBytes(plainText); //設置獲取摘要的算法 HashAlgorithm sha1 = HashAlgorithm.Create("SHA1"); //獲得原始摘要 byte[] digestData = sha1.ComputeHash(plainData); //對元素摘要進行簽名 byte[] signedDigest = provider.SignHash(digestData, "SHA1"); return Convert.ToBase64String(signedDigest); } //使用VerifyHash public static bool VerifyData2(string plainText, string signedDigest, string publicKeyXml) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publicKeyXml); byte[] plainData = Encoding.Default.GetBytes("SHA1"); byte[] signedDigestData = Convert.FromBase64String(signedDigest); //獲得本地摘要 HashAlgorithm sha1 = HashAlgorithm.Create("SHA1"); byte[] digest = sha1.ComputeHash(plainData); //解密簽名 bool isDataIntact = provider.VerifyHash(digest, "SHA1", signedDigestData); return isDataIntact; } }
到此,關于“ASP.NET中加密和解密怎么實現”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。