一个在DotNet下和Java下都通用的加密类!

开始做项目时,我们2边都用的是MD5加密的方法,但是后来加密后的密文怎么也对应不上,然后后来考虑别的方法采用3Des加密算法!


  1using System;
  2using System.IO;
  3using System.Security.Cryptography;
  4using System.Text;
  5
  6namespace goody9807.Shared.Crypt
  7{
  8    /// <summary>
  9    /// Summary description for CryptUtil.
 10    /// </summary>

 11    public class CryptUtil
 12    {
 13        public static string DecryptString(string input)
 14        {
 15            if (input.Equals(string.Empty))
 16            {
 17                return input;
 18            }

 19
 20            byte[] byKey = {0x630x680x650x6E0x790x750x610x6E};
 21            byte[] IV = {0xFE0xDC0xBA0x980x760x540x320x10};
 22            byte[] inputByteArray = new Byte[input.Length];
 23            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 24            inputByteArray = Convert.FromBase64String(input);
 25            MemoryStream ms = new MemoryStream();
 26            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
 27            cs.Write(inputByteArray, 0, inputByteArray.Length);
 28            cs.FlushFinalBlock();
 29            Encoding encoding = new UTF8Encoding();
 30            return encoding.GetString(ms.ToArray());
 31        }

 32
 33        public static string EncryptString(string input)
 34        {
 35            if (input.Equals(string.Empty))
 36            {
 37                return input;
 38            }

 39
 40            byte[] byKey = {0x630x680x650x6E0x790x750x610x6E};
 41            byte[] IV = {0xFE0xDC0xBA0x980x760x540x320x10};
 42            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 43            byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
 44            MemoryStream ms = new MemoryStream();
 45            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
 46            cs.Write(inputByteArray, 0, inputByteArray.Length);
 47            cs.FlushFinalBlock();
 48            return Convert.ToBase64String(ms.ToArray());
 49        }

 50        /// <summary>
 51        /// DES + Base64 加密
 52        /// </summary>
 53        /// <param name="input">明文字符串</param>
 54        /// <returns>已加密字符串</returns>

 55        public static string DesBase64Encrypt(string input)
 56        {
 57            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
 58            des.Mode = System.Security.Cryptography.CipherMode.ECB;
 59            ICryptoTransform ct;
 60            MemoryStream ms;
 61            CryptoStream cs;
 62            byte[] byt;
 63            byte[] Key = new byte[8]{56,50,55,56,56,55,49,49}
 64            byte[] IV = new byte[8]{0,0,0,0,0,0,0,0};
 65
 66            ct = des.CreateEncryptor(Key, IV);
 67
 68            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
 69            
 70            ms = new MemoryStream();
 71            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
 72            cs.Write(byt, 0, byt.Length);
 73            cs.FlushFinalBlock();
 74
 75            cs.Close();
 76
 77            byte[] answer = ms.ToArray();
 78            for(int j=0;j<answer.Length;j++)
 79            {
 80                Console.Write(answer[j].ToString()+ " ");
 81            }

 82            Console.WriteLine();
 83            return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串
 84        }

 85
 86        /// <summary>
 87        /// DES + Base64 解密
 88        /// </summary>
 89        /// <param name="input">密文字符串</param>
 90        /// <returns>解密字符串</returns>

 91        public static string DesBase64Decrypt(string input)
 92        {
 93            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
 94            des.Mode = System.Security.Cryptography.CipherMode.ECB;
 95            ICryptoTransform ct;
 96            MemoryStream ms;
 97            CryptoStream cs;
 98            byte[] byt;
 99            byte[] Key = new byte[8]{56,50,55,56,56,55,49,49}
100            byte[] IV = new byte[8]{0,0,0,0,0,0,0,0};
101            
102            ct = des.CreateDecryptor(Key, IV);
103            byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组
104
105            ms = new MemoryStream();
106            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
107            cs.Write(byt, 0, byt.Length);
108            cs.FlushFinalBlock();
109
110            cs.Close();
111
112            return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串
113        }

114
115        /// <summary>
116        /// 3DES 加密 Byte[] to HEX string
117        /// </summary>
118        /// <param name="input">明文字符串</param>
119        /// <returns>已加密字符串</returns>

120        public static string ThreeDesEncryptHEX(string input)
121        {
122            string result = "";
123            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();
124            des.Mode = System.Security.Cryptography.CipherMode.CBC;
125            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
126            ICryptoTransform ct;
127            MemoryStream ms;
128            CryptoStream cs;
129            byte[] byt;
130            byte[] Key = new byte[24]{
131                                         1,2,3,4,5,6,
132                                         1,2,3,4,5,6,
133                                         1,2,3,4,5,6,
134                                         1,2,3,4,5,6
135                                     }

136            byte[] IV = new byte[8]{1,2,3,4,5,6,1,2};
137
138            ct = des.CreateEncryptor(Key, IV);
139
140            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组
141            
142            ms = new MemoryStream();
143            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
144            cs.Write(byt, 0, byt.Length);
145            cs.FlushFinalBlock();
146
147            cs.Close();
148
149            byte[] answer = ms.ToArray();
150            for(int j=0;j<answer.Length;j++)
151            {
152                result += answer[j].ToString("x").PadLeft(2,'0');
153            }

154            return result;
155        }

156
157        /// <summary>
158        /// 3DES + HEX to byte[] 解密
159        /// </summary>
160        /// <param name="input">密文字符串</param>
161        /// <returns>解密字符串</returns>

162        public static string ThreeDesDecryptHEX(string input)
163        {
164            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();
165            des.Mode = System.Security.Cryptography.CipherMode.CBC;

166            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
167            ICryptoTransform ct;
168            MemoryStream ms;
169            CryptoStream cs;
170            byte[] Key = new byte[24]{
171                                         1,2,3,4,5,6,
172                                         1,2,3,4,5,6,
173                                         1,2,3,4,5,6,
174                                         1,2,3,4,5,6
175                                     }; 
176            byte[] IV = new byte[8]{1,2,3,4,5,6,1,2};
177            
178            ct = des.CreateDecryptor(Key, IV);
179            //byt = Convert.FromBase64String(input); // 将 密文 以 HEX to byte[]编码转换成 byte 数组
180            if(input.Length<=1)
181            {
182                throw new Exception("encrypted HEX string is too short!");
183            }
184            byte[] byt = new byte[input.Length/2];
185            for(int i=0;i<byt.Length;i++)
186            {
187                //Console.WriteLine(input.Substring(i*2,2));
188                byt[i] = Convert.ToByte(input.Substring(i*2,2),16);
189            }
190
191            ms = new MemoryStream();
192            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
193            cs.Write(byt, 0, byt.Length);
194            cs.FlushFinalBlock();
195
196            cs.Close();
197
198            return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串
199        }
200        /// <summary>
201        /// Base64解码
202        /// </summary>
203        /// <param name="base64Str"></param>
204        /// <returns></returns>

205        public static string DecodingFromBase64(string base64Str)
206        {
207            Byte[] bytes = Convert.FromBase64String(base64Str);
208            return System.Text.Encoding.UTF8.GetString(bytes);
209        }

210        /// <summary>
211        /// Base64编码
212        /// </summary>
213        /// <param name="str"></param>
214        /// <returns></returns>

215        public static string EncodingToBase64(string str)
216        {
217            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
218        }

219        /// <summary>
220        /// 根据指定的编码方式Base64解码
221        /// </summary>
222        /// <param name="base64Str"></param>
223        /// <param name="strEncoding"></param>
224        /// <returns></returns>

225        public static string DecodingFromBase64(string base64Str,System.Text.Encoding strEncoding)
226        {
227            Byte[] bytes = Convert.FromBase64String(base64Str);
228            return strEncoding.GetString(bytes);
229        }

230        /// <summary>
231        /// 根据指定的编码方式Base64编码
232        /// </summary>
233        /// <param name="str"></param>
234        /// <param name="strEncoding"></param>
235        /// <returns></returns>

236        public static string EncodingToBase64(string str,System.Text.Encoding strEncoding)
237        {
238            return Convert.ToBase64String(strEncoding.GetBytes(str));
239        }

240    }

241}


用3DES 加密时 需要注意Asp.net这边的模式需要用CBC模式
des.Mode = System.Security.Cryptography.CipherMode.CBC;
posted @ 2006-11-07 17:48  PointNet  阅读(2888)  评论(14编辑  收藏  举报