简单说一下加密解密:
加密解密有挺多方式去实现,今天呢我做为宇宙第一帅的人就给大家来分享以下源代码!听清楚是源代码哦!!
话不多说上干货
1:MD5加密/解密
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Web; 5 using System.Text.RegularExpressions; 6 7 namespace hic.Common 8 { 9 public class SecurityHelper 10 { 11 /// <summary> 12 /// MD5字符串加密 13 /// </summary> 14 /// <param name="source">待加密字符串</param> 15 /// <returns></returns> 16 public static string MD5(string source) 17 { 18 return MD5(source, true); 19 } 20 21 /// <summary> 22 /// MD5字符串加密 23 /// </summary> 24 /// <param name="source">待加密字符串</param> 25 /// <param name="ishalf">加密是16位还是32位,如果为true则是16位。</param> 26 /// <returns></returns> 27 public static string MD5(string source, bool ishalf) 28 { 29 string outputStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5").ToLower(); 30 if (ishalf)//16位MD5加密(取32位加密的9~25字符) 31 outputStr = outputStr.Substring(8, 16); 32 return outputStr; 33 } 34 35 /// <summary> 36 /// 对字符串进行Base64编码 37 /// </summary> 38 /// <param name="source">待编码字符串</param> 39 /// <returns></returns> 40 public static string EncodeBase64(string source) 41 { 42 UnicodeEncoding code = new UnicodeEncoding(); 43 byte[] bytes = code.GetBytes(source); 44 return Convert.ToBase64String(bytes); 45 } 46 47 /// <summary> 48 /// 对字符串进行Base64解码 49 /// </summary> 50 /// <param name="source">待解码字符串</param> 51 /// <returns></returns> 52 public static string DecodeBase64(string source) 53 { 54 UnicodeEncoding code = new UnicodeEncoding(); 55 byte[] bytes = Convert.FromBase64String(source); 56 return code.GetString(bytes); 57 } 58 59 /// <summary> 60 /// 检查当前IP是否是受限IP 61 /// </summary> 62 /// <param name="LimitedIP">受限的IP,格式如:192.168.1.110|212.235.*.*|232.*.*.*</param> 63 /// <returns>返回true表示IP未受到限制</returns> 64 public static bool CheckIPIsLimited(string limitedIP) 65 { 66 string currentIP = GetUserIP(); 67 if (limitedIP == null || limitedIP.Trim() == string.Empty) 68 return true; 69 limitedIP.Replace(".", @"\."); 70 limitedIP.Replace("*", @"[^\.]{1,3}"); 71 Regex reg = new Regex(limitedIP, RegexOptions.Compiled); 72 Match match = reg.Match(currentIP); 73 return !match.Success; 74 } 75 76 /// <summary> 77 /// 得到用户IP 78 /// </summary> 79 /// <returns></returns> 80 public static string GetUserIP() 81 { 82 return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); 83 } 84 } 85 }
2:DES加密/解密类
1 using System; 2 using System.Security.Cryptography; 3 using System.Text; 4 namespace Maticsoft.Common.DEncrypt 5 { 6 /// <summary> 7 /// DES加密/解密类。 8 /// LiTianPing 9 /// </summary> 10 public class DESEncrypt 11 { 12 public DESEncrypt() 13 { 14 } 15 16 #region ========加密======== 17 18 /// <summary> 19 /// 加密 20 /// </summary> 21 /// <param name="Text"></param> 22 /// <returns></returns> 23 public static string Encrypt(string Text) 24 { 25 return Encrypt(Text,"MATICSOFT"); 26 } 27 /// <summary> 28 /// 加密数据 29 /// </summary> 30 /// <param name="Text"></param> 31 /// <param name="sKey"></param> 32 /// <returns></returns> 33 public static string Encrypt(string Text,string sKey) 34 { 35 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 36 byte[] inputByteArray; 37 inputByteArray=Encoding.Default.GetBytes(Text); 38 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 39 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 40 System.IO.MemoryStream ms=new System.IO.MemoryStream(); 41 CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); 42 cs.Write(inputByteArray,0,inputByteArray.Length); 43 cs.FlushFinalBlock(); 44 StringBuilder ret=new StringBuilder(); 45 foreach( byte b in ms.ToArray()) 46 { 47 ret.AppendFormat("{0:X2}",b); 48 } 49 return ret.ToString(); 50 } 51 52 #endregion 53 54 #region ========解密======== 55 56 57 /// <summary> 58 /// 解密 59 /// </summary> 60 /// <param name="Text"></param> 61 /// <returns></returns> 62 public static string Decrypt(string Text) 63 { 64 return Decrypt(Text,"MATICSOFT"); 65 } 66 /// <summary> 67 /// 解密数据 68 /// </summary> 69 /// <param name="Text"></param> 70 /// <param name="sKey"></param> 71 /// <returns></returns> 72 public static string Decrypt(string Text,string sKey) 73 { 74 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 75 int len; 76 len=Text.Length/2; 77 byte[] inputByteArray = new byte[len]; 78 int x,i; 79 for(x=0;x<len;x++) 80 { 81 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); 82 inputByteArray[x]=(byte)i; 83 } 84 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 85 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 86 System.IO.MemoryStream ms=new System.IO.MemoryStream(); 87 CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); 88 cs.Write(inputByteArray,0,inputByteArray.Length); 89 cs.FlushFinalBlock(); 90 return Encoding.Default.GetString(ms.ToArray()); 91 } 92 93 #endregion 94 95 96 } 97 }
3:RSA加密解密及RSA签名和验证
1 using System; 2 using System.Text; 3 using System.Security.Cryptography; 4 namespace Maticsoft.Common.DEncrypt 5 { 6 /// <summary> 7 /// RSA加密解密及RSA签名和验证 8 /// </summary> 9 public class RSACryption 10 { 11 public RSACryption() 12 { 13 } 14 15 16 #region RSA 加密解密 17 18 #region RSA 的密钥产生 19 20 /// <summary> 21 /// RSA 的密钥产生 产生私钥 和公钥 22 /// </summary> 23 /// <param name="xmlKeys"></param> 24 /// <param name="xmlPublicKey"></param> 25 public void RSAKey(out string xmlKeys,out string xmlPublicKey) 26 { 27 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 28 xmlKeys=rsa.ToXmlString(true); 29 xmlPublicKey = rsa.ToXmlString(false); 30 } 31 #endregion 32 33 #region RSA的加密函数 34 //############################################################################## 35 //RSA 方式加密 36 //说明KEY必须是XML的行式,返回的是字符串 37 //在有一点需要说明!!该加密方式有 长度 限制的!! 38 //############################################################################## 39 40 //RSA的加密函数 string 41 public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString ) 42 { 43 44 byte[] PlainTextBArray; 45 byte[] CypherTextBArray; 46 string Result; 47 RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 48 rsa.FromXmlString(xmlPublicKey); 49 PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString); 50 CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); 51 Result=Convert.ToBase64String(CypherTextBArray); 52 return Result; 53 54 } 55 //RSA的加密函数 byte[] 56 public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString ) 57 { 58 59 byte[] CypherTextBArray; 60 string Result; 61 RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 62 rsa.FromXmlString(xmlPublicKey); 63 CypherTextBArray = rsa.Encrypt(EncryptString, false); 64 Result=Convert.ToBase64String(CypherTextBArray); 65 return Result; 66 67 } 68 #endregion 69 70 #region RSA的解密函数 71 //RSA的解密函数 string 72 public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString ) 73 { 74 byte[] PlainTextBArray; 75 byte[] DypherTextBArray; 76 string Result; 77 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 78 rsa.FromXmlString(xmlPrivateKey); 79 PlainTextBArray =Convert.FromBase64String(m_strDecryptString); 80 DypherTextBArray=rsa.Decrypt(PlainTextBArray, false); 81 Result=(new UnicodeEncoding()).GetString(DypherTextBArray); 82 return Result; 83 84 } 85 86 //RSA的解密函数 byte 87 public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString ) 88 { 89 byte[] DypherTextBArray; 90 string Result; 91 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 92 rsa.FromXmlString(xmlPrivateKey); 93 DypherTextBArray=rsa.Decrypt(DecryptString, false); 94 Result=(new UnicodeEncoding()).GetString(DypherTextBArray); 95 return Result; 96 97 } 98 #endregion 99 100 #endregion 101 102 #region RSA数字签名 103 104 #region 获取Hash描述表 105 //获取Hash描述表 106 public bool GetHash(string m_strSource, ref byte[] HashData) 107 { 108 //从字符串中取得Hash描述 109 byte[] Buffer; 110 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); 111 Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); 112 HashData = MD5.ComputeHash(Buffer); 113 114 return true; 115 } 116 117 //获取Hash描述表 118 public bool GetHash(string m_strSource, ref string strHashData) 119 { 120 121 //从字符串中取得Hash描述 122 byte[] Buffer; 123 byte[] HashData; 124 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); 125 Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); 126 HashData = MD5.ComputeHash(Buffer); 127 128 strHashData = Convert.ToBase64String(HashData); 129 return true; 130 131 } 132 133 //获取Hash描述表 134 public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData) 135 { 136 137 //从文件中取得Hash描述 138 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); 139 HashData = MD5.ComputeHash(objFile); 140 objFile.Close(); 141 142 return true; 143 144 } 145 146 //获取Hash描述表 147 public bool GetHash(System.IO.FileStream objFile, ref string strHashData) 148 { 149 150 //从文件中取得Hash描述 151 byte[] HashData; 152 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); 153 HashData = MD5.ComputeHash(objFile); 154 objFile.Close(); 155 156 strHashData = Convert.ToBase64String(HashData); 157 158 return true; 159 160 } 161 #endregion 162 163 #region RSA签名 164 //RSA签名 165 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData) 166 { 167 168 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 169 170 RSA.FromXmlString(p_strKeyPrivate); 171 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); 172 //设置签名的算法为MD5 173 RSAFormatter.SetHashAlgorithm("MD5"); 174 //执行签名 175 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); 176 177 return true; 178 179 } 180 181 //RSA签名 182 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData) 183 { 184 185 byte[] EncryptedSignatureData; 186 187 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 188 189 RSA.FromXmlString(p_strKeyPrivate); 190 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); 191 //设置签名的算法为MD5 192 RSAFormatter.SetHashAlgorithm("MD5"); 193 //执行签名 194 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); 195 196 m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); 197 198 return true; 199 200 } 201 202 //RSA签名 203 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData) 204 { 205 206 byte[] HashbyteSignature; 207 208 HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); 209 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 210 211 RSA.FromXmlString(p_strKeyPrivate); 212 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); 213 //设置签名的算法为MD5 214 RSAFormatter.SetHashAlgorithm("MD5"); 215 //执行签名 216 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); 217 218 return true; 219 220 } 221 222 //RSA签名 223 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData) 224 { 225 226 byte[] HashbyteSignature; 227 byte[] EncryptedSignatureData; 228 229 HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); 230 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 231 232 RSA.FromXmlString(p_strKeyPrivate); 233 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); 234 //设置签名的算法为MD5 235 RSAFormatter.SetHashAlgorithm("MD5"); 236 //执行签名 237 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); 238 239 m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); 240 241 return true; 242 243 } 244 #endregion 245 246 #region RSA 签名验证 247 248 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData) 249 { 250 251 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 252 253 RSA.FromXmlString(p_strKeyPublic); 254 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); 255 //指定解密的时候HASH算法为MD5 256 RSADeformatter.SetHashAlgorithm("MD5"); 257 258 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) 259 { 260 return true; 261 } 262 else 263 { 264 return false; 265 } 266 267 } 268 269 public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData) 270 { 271 272 byte[] HashbyteDeformatter; 273 274 HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); 275 276 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 277 278 RSA.FromXmlString(p_strKeyPublic); 279 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); 280 //指定解密的时候HASH算法为MD5 281 RSADeformatter.SetHashAlgorithm("MD5"); 282 283 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) 284 { 285 return true; 286 } 287 else 288 { 289 return false; 290 } 291 292 } 293 294 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData) 295 { 296 297 byte[] DeformatterData; 298 299 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 300 301 RSA.FromXmlString(p_strKeyPublic); 302 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); 303 //指定解密的时候HASH算法为MD5 304 RSADeformatter.SetHashAlgorithm("MD5"); 305 306 DeformatterData =Convert.FromBase64String(p_strDeformatterData); 307 308 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) 309 { 310 return true; 311 } 312 else 313 { 314 return false; 315 } 316 317 } 318 319 public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData) 320 { 321 322 byte[] DeformatterData; 323 byte[] HashbyteDeformatter; 324 325 HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); 326 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); 327 328 RSA.FromXmlString(p_strKeyPublic); 329 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); 330 //指定解密的时候HASH算法为MD5 331 RSADeformatter.SetHashAlgorithm("MD5"); 332 333 DeformatterData =Convert.FromBase64String(p_strDeformatterData); 334 335 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) 336 { 337 return true; 338 } 339 else 340 { 341 return false; 342 } 343 344 } 345 346 347 #endregion 348 349 350 #endregion 351 352 } 353 }
4:哈希加密/解密
1 using System; 2 using System.Text; 3 using System.Security.Cryptography; 4 namespace Maticsoft.Common.DEncrypt 5 { 6 /// <summary> 7 /// 得到随机安全码(哈希加密)。 8 /// </summary> 9 public class HashEncode 10 { 11 public HashEncode() 12 { 13 // 14 // TODO: 在此处添加构造函数逻辑 15 // 16 } 17 /// <summary> 18 /// 得到随机哈希加密字符串 19 /// </summary> 20 /// <returns></returns> 21 public static string GetSecurity() 22 { 23 string Security = HashEncoding(GetRandomValue()); 24 return Security; 25 } 26 /// <summary> 27 /// 得到一个随机数值 28 /// </summary> 29 /// <returns></returns> 30 public static string GetRandomValue() 31 { 32 Random Seed = new Random(); 33 string RandomVaule = Seed.Next(1, int.MaxValue).ToString(); 34 return RandomVaule; 35 } 36 /// <summary> 37 /// 哈希加密一个字符串 38 /// </summary> 39 /// <param name="Security"></param> 40 /// <returns></returns> 41 public static string HashEncoding(string Security) 42 { 43 byte[] Value; 44 UnicodeEncoding Code = new UnicodeEncoding(); 45 byte[] Message = Code.GetBytes(Security); 46 SHA512Managed Arithmetic = new SHA512Managed(); 47 Value = Arithmetic.ComputeHash(Message); 48 Security = ""; 49 foreach(byte o in Value) 50 { 51 Security += (int) o + "O"; 52 } 53 return Security; 54 } 55 } 56 }
5:Encrypt
1 using System; 2 using System.Security.Cryptography; 3 using System.Text; 4 namespace Maticsoft.Common.DEncrypt 5 { 6 /// <summary> 7 /// Encrypt 的摘要说明。 8 /// LiTianPing 9 /// </summary> 10 public class DEncrypt 11 { 12 /// <summary> 13 /// 构造方法 14 /// </summary> 15 public DEncrypt() 16 { 17 } 18 19 #region 使用 缺省密钥字符串 加密/解密string 20 21 /// <summary> 22 /// 使用缺省密钥字符串加密string 23 /// </summary> 24 /// <param name="original">明文</param> 25 /// <returns>密文</returns> 26 public static string Encrypt(string original) 27 { 28 return Encrypt(original,"MATICSOFT"); 29 } 30 /// <summary> 31 /// 使用缺省密钥字符串解密string 32 /// </summary> 33 /// <param name="original">密文</param> 34 /// <returns>明文</returns> 35 public static string Decrypt(string original) 36 { 37 return Decrypt(original,"MATICSOFT",System.Text.Encoding.Default); 38 } 39 40 #endregion 41 42 #region 使用 给定密钥字符串 加密/解密string 43 /// <summary> 44 /// 使用给定密钥字符串加密string 45 /// </summary> 46 /// <param name="original">原始文字</param> 47 /// <param name="key">密钥</param> 48 /// <param name="encoding">字符编码方案</param> 49 /// <returns>密文</returns> 50 public static string Encrypt(string original, string key) 51 { 52 byte[] buff = System.Text.Encoding.Default.GetBytes(original); 53 byte[] kb = System.Text.Encoding.Default.GetBytes(key); 54 return Convert.ToBase64String(Encrypt(buff,kb)); 55 } 56 /// <summary> 57 /// 使用给定密钥字符串解密string 58 /// </summary> 59 /// <param name="original">密文</param> 60 /// <param name="key">密钥</param> 61 /// <returns>明文</returns> 62 public static string Decrypt(string original, string key) 63 { 64 return Decrypt(original,key,System.Text.Encoding.Default); 65 } 66 67 /// <summary> 68 /// 使用给定密钥字符串解密string,返回指定编码方式明文 69 /// </summary> 70 /// <param name="encrypted">密文</param> 71 /// <param name="key">密钥</param> 72 /// <param name="encoding">字符编码方案</param> 73 /// <returns>明文</returns> 74 public static string Decrypt(string encrypted, string key,Encoding encoding) 75 { 76 byte[] buff = Convert.FromBase64String(encrypted); 77 byte[] kb = System.Text.Encoding.Default.GetBytes(key); 78 return encoding.GetString(Decrypt(buff,kb)); 79 } 80 #endregion 81 82 #region 使用 缺省密钥字符串 加密/解密/byte[] 83 /// <summary> 84 /// 使用缺省密钥字符串解密byte[] 85 /// </summary> 86 /// <param name="encrypted">密文</param> 87 /// <param name="key">密钥</param> 88 /// <returns>明文</returns> 89 public static byte[] Decrypt(byte[] encrypted) 90 { 91 byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT"); 92 return Decrypt(encrypted,key); 93 } 94 /// <summary> 95 /// 使用缺省密钥字符串加密 96 /// </summary> 97 /// <param name="original">原始数据</param> 98 /// <param name="key">密钥</param> 99 /// <returns>密文</returns> 100 public static byte[] Encrypt(byte[] original) 101 { 102 byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT"); 103 return Encrypt(original,key); 104 } 105 #endregion 106 107 #region 使用 给定密钥 加密/解密/byte[] 108 109 /// <summary> 110 /// 生成MD5摘要 111 /// </summary> 112 /// <param name="original">数据源</param> 113 /// <returns>摘要</returns> 114 public static byte[] MakeMD5(byte[] original) 115 { 116 MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); 117 byte[] keyhash = hashmd5.ComputeHash(original); 118 hashmd5 = null; 119 return keyhash; 120 } 121 122 123 /// <summary> 124 /// 使用给定密钥加密 125 /// </summary> 126 /// <param name="original">明文</param> 127 /// <param name="key">密钥</param> 128 /// <returns>密文</returns> 129 public static byte[] Encrypt(byte[] original, byte[] key) 130 { 131 TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); 132 des.Key = MakeMD5(key); 133 des.Mode = CipherMode.ECB; 134 135 return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length); 136 } 137 138 /// <summary> 139 /// 使用给定密钥解密数据 140 /// </summary> 141 /// <param name="encrypted">密文</param> 142 /// <param name="key">密钥</param> 143 /// <returns>明文</returns> 144 public static byte[] Decrypt(byte[] encrypted, byte[] key) 145 { 146 TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); 147 des.Key = MakeMD5(key); 148 des.Mode = CipherMode.ECB; 149 150 return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length); 151 } 152 153 #endregion 154 155 156 157 158 } 159 }
希望能给大家带来帮助。