加密解密

 1 using System;
 2 using System.Security.Cryptography;
 3 using System.Text;
 4 
 5 namespace ManageSystem.Core {
 6     public static class CryptographyHelper {
 7         /// <summary> 
 8         /// MD5加密字符串 WEB
 9         /// </summary> 
10         /// <param name="source">源字符串</param> 
11         /// <returns>加密后的字符串</returns> 
12         public string MD5(string source) {
13             return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5"); ;
14         }
15         /// <summary>
16         /// MD5 32位加密
17         /// </summary>
18         public static string MD5Encrypt32(string strCode) {
19             MD5 md5 = new MD5CryptoServiceProvider();
20             byte[] t = md5.ComputeHash(Encoding.UTF8.GetBytes(strCode));
21             StringBuilder sb = new StringBuilder(32);
22             for (int i = 0; i < t.Length; i++) {
23                 sb.Append(t[i].ToString("x").PadLeft(2, '0'));
24             }
25             return sb.ToString();
26         }
27         /// <summary>
28         /// MD5 16位加密
29         /// </summary>
30         public static string MD5Encrypt16(string strCode) {
31             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
32             string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(strCode)), 4, 8);
33             t2 = t2.Replace("-", "");
34             return t2;
35         }
36 
37         // Methods
38         public static string TripleDESDecrypt(string cipherString, string key, bool useHashing) {
39             byte[] buffer;
40             byte[] inputBuffer = Convert.FromBase64String(cipherString);
41             //string key = "johnny depp";
42             if (useHashing) {
43                 MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
44                 buffer = provider.ComputeHash(Encoding.UTF8.GetBytes(key));
45                 provider.Clear();
46             }
47             else {
48                 buffer = Encoding.UTF8.GetBytes(key);
49             }
50             TripleDESCryptoServiceProvider tripleDesProvider = new TripleDESCryptoServiceProvider();
51             tripleDesProvider.Key = buffer;
52             tripleDesProvider.Mode = CipherMode.ECB;
53             tripleDesProvider.Padding = PaddingMode.PKCS7;
54             byte[] bytes = tripleDesProvider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
55             tripleDesProvider.Clear();
56             return Encoding.UTF8.GetString(bytes);
57         }
58 
59         public static string TripleDESEncrypt(string toEncrypt, string key, bool useHashing) {
60             byte[] buffer;
61             byte[] bytes = Encoding.UTF8.GetBytes(toEncrypt);
62             //string key = "johnny depp";
63             if (useHashing) {
64                 MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
65                 buffer = provider.ComputeHash(Encoding.UTF8.GetBytes(key));
66                 provider.Clear();
67             }
68             else {
69                 buffer = Encoding.UTF8.GetBytes(key);
70             }
71             TripleDESCryptoServiceProvider tripleDesProvider = new TripleDESCryptoServiceProvider();
72             tripleDesProvider.Key = buffer;
73             tripleDesProvider.Mode = CipherMode.ECB;
74             tripleDesProvider.Padding = PaddingMode.PKCS7;
75             byte[] inArray = tripleDesProvider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
76             tripleDesProvider.Clear();
77             return Convert.ToBase64String(inArray, 0, inArray.Length);
78         }
79     }
80 }

 

posted @ 2013-12-03 17:44  zhuhc  阅读(284)  评论(0编辑  收藏  举报