ASP.NET加密解密算法分享
#regionDES加密解密 ///<summary> ///DES加密 ///</summary> ///<paramname="strSource">待加密字串</param> ///<paramname="key">32位Key值</param> ///<returns>加密后的字符串</returns> publicstringDESEncrypt(stringstrSource) { returnDESEncrypt(strSource,DESKey); } publicstringDESEncrypt(stringstrSource,byte[]key) { SymmetricAlgorithmsa=Rijndael.Create(); sa.Key=key; sa.Mode=CipherMode.ECB; sa.Padding=PaddingMode.Zeros; MemoryStreamms=newMemoryStream(); CryptoStreamcs=newCryptoStream(ms,sa.CreateEncryptor(),CryptoStreamMode.Write); byte[]byt=Encoding.Unicode.GetBytes(strSource); cs.Write(byt,0,byt.Length); cs.FlushFinalBlock(); cs.Close(); returnConvert.ToBase64String(ms.ToArray()); } ///<summary> ///DES解密 ///</summary> ///<paramname="strSource">待解密的字串</param> ///<paramname="key">32位Key值</param> ///<returns>解密后的字符串</returns> publicstringDESDecrypt(stringstrSource) { returnDESDecrypt(strSource,DESKey); } publicstringDESDecrypt(stringstrSource,byte[]key) { SymmetricAlgorithmsa=Rijndael.Create(); sa.Key=key; sa.Mode=CipherMode.ECB; sa.Padding=PaddingMode.Zeros; ICryptoTransformct=sa.CreateDecryptor(); byte[]byt=Convert.FromBase64String(strSource); MemoryStreamms=newMemoryStream(byt); CryptoStreamcs=newCryptoStream(ms,ct,CryptoStreamMode.Read); StreamReadersr=newStreamReader(cs,Encoding.Unicode); returnsr.ReadToEnd(); } #endregion #region一个用hash实现的加密解密方法 ///<summary> ///加密 ///</summary> ///<paramname="src"></param> ///<returns></returns> publicstaticstringEncryptStrByHash(stringsrc) { if(src.Length==0) { return""; } byte[]HaKey=System.Text.Encoding.ASCII.GetBytes((src+"Test").ToCharArray()); byte[]HaData=newbyte[20]; HMACSHA1Hmac=newHMACSHA1(HaKey); CryptoStreamcs=newCryptoStream(Stream.Null,Hmac,CryptoStreamMode.Write); try { cs.Write(HaData,0,HaData.Length); } finally { cs.Close(); } stringHaResult=System.Convert.ToBase64String(Hmac.Hash).Substring(0,16); byte[]RiKey=System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray()); byte[]RiDataBuf=System.Text.Encoding.ASCII.GetBytes(src.ToCharArray()); byte[]EncodedBytes={}; MemoryStreamms=newMemoryStream(); RijndaelManagedrv=newRijndaelManaged(); cs=newCryptoStream(ms,rv.CreateEncryptor(RiKey,RiKey),CryptoStreamMode.Write); try { cs.Write(RiDataBuf,0,RiDataBuf.Length); cs.FlushFinalBlock(); EncodedBytes=ms.ToArray(); } finally { ms.Close(); cs.Close(); } returnHaResult+System.Convert.ToBase64String(EncodedBytes); } ///<summary> ///解密 ///</summary> ///<paramname="src"></param> ///<returns></returns> publicstaticstringDecrypStrByHash(stringsrc) { if(src.Length<40)return""; byte[]SrcBytes=System.Convert.FromBase64String(src.Substring(16)); byte[]RiKey=System.Text.Encoding.ASCII.GetBytes(src.Substring(0,16).ToCharArray()); byte[]InitialText=newbyte[SrcBytes.Length]; RijndaelManagedrv=newRijndaelManaged(); MemoryStreamms=newMemoryStream(SrcBytes); CryptoStreamcs=newCryptoStream(ms,rv.CreateDecryptor(RiKey,RiKey),CryptoStreamMode.Read); try { cs.Read(InitialText,0,InitialText.Length); } finally { ms.Close(); cs.Close(); } System.Text.StringBuilderResult=newSystem.Text.StringBuilder(); for(inti=0;i<InitialText.Length;++i)if(InitialText[i]>0)Result.Append((char)InitialText[i]); returnResult.ToString(); } ///<summary> ///对加密后的密文重新编码,如果密文长>16,则去掉前16个字符,如果长度小于16,返回空字符串 ///</summary> ///<paramname="s"></param> ///<returns></returns> publicstringReEncryptStrByHash(strings) { stringe=Encrypt.EncryptStrByHash(s); return((e.Length>16)?e.Substring(16):""); } #endregion #regionMd5加密,生成16位或32位,生成的密文都是大写 publicstaticstringMd5To16(stringstr) { MD5CryptoServiceProvidermd5=newMD5CryptoServiceProvider(); stringt2=BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)),4,8); t2=t2.Replace("-",""); returnt2; } ////<summary> ///MD532位加密 ///</summary> ///<paramname="str"></param> ///<returns></returns> publicstaticstringMd5To32(stringstr) { stringpwd=""; MD5md5=MD5.Create(); byte[]s=md5.ComputeHash(Encoding.UTF8.GetBytes(str)); for(inti=0;i<s.Length;i++) { pwd=pwd+s[i].ToString("X"); } returnpwd; } #endregion #region3DES加密解密 publicstringEncrypt3DES(stringstr) { //密钥 stringsKey="wyw308"; ////矢量,可为空 stringsIV="scf521"; ////构造对称算法 SymmetricAlgorithmmCSP=newTripleDESCryptoServiceProvider(); ICryptoTransformct; MemoryStreamms; CryptoStreamcs; byte[]byt; mCSP.Key=Convert.FromBase64String(sKey); mCSP.IV=Convert.FromBase64String(sIV); mCSP.Mode=System.Security.Cryptography.CipherMode.ECB; mCSP.Padding=System.Security.Cryptography.PaddingMode.PKCS7; ct=mCSP.CreateEncryptor(mCSP.Key,mCSP.IV); byt=Encoding.UTF8.GetBytes(str); ms=newMemoryStream(); cs=newCryptoStream(ms,ct,CryptoStreamMode.Write); cs.Write(byt,0,byt.Length); cs.FlushFinalBlock(); cs.Close(); returnConvert.ToBase64String(ms.ToArray()); } ///<summary> ///带指定密钥和矢量的3DES加密 ///</summary> ///<paramname="str"></param> ///<paramname="sKey"></param> ///<paramname="sIV"></param> ///<returns></returns> publicstringEncrypt3DES(stringstr,stringsKey,stringsIV) { SymmetricAlgorithmmCSP=newTripleDESCryptoServiceProvider(); ICryptoTransformct; MemoryStreamms; CryptoStreamcs; byte[]byt; mCSP.Key=Convert.FromBase64String(sKey); mCSP.IV=Convert.FromBase64String(sIV); mCSP.Mode=System.Security.Cryptography.CipherMode.ECB; mCSP.Padding=System.Security.Cryptography.PaddingMode.PKCS7; ct=mCSP.CreateEncryptor(mCSP.Key,mCSP.IV); byt=Encoding.UTF8.GetBytes(str); ms=newMemoryStream(); cs=newCryptoStream(ms,ct,CryptoStreamMode.Write); cs.Write(byt,0,byt.Length); cs.FlushFinalBlock(); cs.Close(); returnConvert.ToBase64String(ms.ToArray()); } //解密 publicstringDecrypt3DES(stringValue) { stringsKey="wyw308"; stringsIV="scf521"; SymmetricAlgorithmmCSP=newTripleDESCryptoServiceProvider(); ICryptoTransformct; MemoryStreamms; CryptoStreamcs; byte[]byt; mCSP.Key=Convert.FromBase64String(sKey); mCSP.IV=Convert.FromBase64String(sIV); mCSP.Mode=System.Security.Cryptography.CipherMode.ECB; mCSP.Padding=System.Security.Cryptography.PaddingMode.PKCS7; ct=mCSP.CreateDecryptor(mCSP.Key,mCSP.IV); byt=Convert.FromBase64String(Value); ms=newMemoryStream(); cs=newCryptoStream(ms,ct,CryptoStreamMode.Write); cs.Write(byt,0,byt.Length); cs.FlushFinalBlock(); cs.Close(); returnEncoding.UTF8.GetString(ms.ToArray()); } ///<summary> ///带指定密钥和矢量的3DES解密 ///</summary> ///<paramname="Value"></param> ///<paramname="sKey"></param> ///<paramname="sIV"></param> ///<returns></returns> publicstringDecrypt3DES(stringstr,stringsKey,stringsIV) { SymmetricAlgorithmmCSP=newTripleDESCryptoServiceProvider(); ICryptoTransformct; MemoryStreamms; CryptoStreamcs; byte[]byt; mCSP.Key=Convert.FromBase64String(sKey); mCSP.IV=Convert.FromBase64String(sIV); mCSP.Mode=System.Security.Cryptography.CipherMode.ECB; mCSP.Padding=System.Security.Cryptography.PaddingMode.PKCS7; ct=mCSP.CreateDecryptor(mCSP.Key,mCSP.IV); byt=Convert.FromBase64String(str); ms=newMemoryStream(); cs=newCryptoStream(ms,ct,CryptoStreamMode.Write); cs.Write(byt,0,byt.Length); cs.FlushFinalBlock(); cs.Close(); returnEncoding.UTF8.GetString(ms.ToArray()); } #endregion #region一个简单的加密解密方法,只支持英文 publicstaticstringEnCryptEnStr(stringstr)//倒序加1加密 { byte[]by=newbyte[str.Length]; for(inti=0; i<=str.Length-1; i++) { by[i]=(byte)((byte)str[i]+1); } str=""; for(inti=by.Length-1; i>=0; i--) { str+=((char)by[i]).ToString(); } returnstr; } publicstaticstringDeCryptEnStr(stringstr)//顺序减1解码 { byte[]by=newbyte[str.Length]; for(inti=0; i<=str.Length-1; i++) { by[i]=(byte)((byte)str[i]-1); } str=""; for(inti=by.Length-1; i>=0; i--) { str+=((char)by[i]).ToString(); } returnstr; } #endregion #region一个简单的加密解密方法,在上一个的基础上支持中文 publicstaticstringEnCryptCnStr(stringstr) { stringhtext="";//blanktext for(inti=0;i<str.Length;i++) { htext=htext+(char)(str[i]+10-1*2); } returnhtext; } publicstaticstringDeCryptCnStr(stringstr) { stringdtext=""; for(inti=0;i<str.Length;i++) { dtext=dtext+(char)(str[i]-10+1*2); } returndtext; } #endregion #regionUrl地址编码解码 ///<summary> ///编码Url地址 ///</summary> ///<paramname="url"></param> ///<returns></returns> publicstaticstringUrlEncode(stringurl) { byte[]mByte=null; mByte=System.Text.Encoding.GetEncoding("GB2312").GetBytes(url); returnSystem.Web.HttpUtility.UrlEncode(mByte); } ///<summary> ///解码Url地址 ///</summary> ///<paramname="url"></param> ///<returns></returns> publicstaticstringUrlDecode(stringurl) { returnHttpUtility.UrlDecode(url,System.Text.Encoding.GetEncoding("GB2312")); } #endregion
以上所述就是本文的全部内容了,希望大家能够喜欢。