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

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


  1 None.gif using  System;
  2 None.gif using  System.IO;
  3 None.gif using  System.Security.Cryptography;
  4 None.gif using  System.Text;
  5 None.gif
  6 None.gif namespace  goody9807.Shared.Crypt
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  9InBlock.gif    /// Summary description for CryptUtil.
 10ExpandedSubBlockEnd.gif    /// </summary>

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

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

 32InBlock.gif
 33InBlock.gif        public static string EncryptString(string input)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35InBlock.gif            if (input.Equals(string.Empty))
 36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 37InBlock.gif                return input;
 38ExpandedSubBlockEnd.gif            }

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

 50ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 51InBlock.gif        /// DES + Base64 加密
 52InBlock.gif        /// </summary>
 53InBlock.gif        /// <param name="input">明文字符串</param>
 54ExpandedSubBlockEnd.gif        /// <returns>已加密字符串</returns>

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

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

 85InBlock.gif
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 87InBlock.gif        /// DES + Base64 解密
 88InBlock.gif        /// </summary>
 89InBlock.gif        /// <param name="input">密文字符串</param>
 90ExpandedSubBlockEnd.gif        /// <returns>解密字符串</returns>

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

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

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

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

154InBlock.gif            return result;
155ExpandedSubBlockEnd.gif        }

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

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

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

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

210ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
211InBlock.gif        /// Base64编码
212InBlock.gif        /// </summary>
213InBlock.gif        /// <param name="str"></param>
214ExpandedSubBlockEnd.gif        /// <returns></returns>

215InBlock.gif        public static string EncodingToBase64(string str)
216ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
217InBlock.gif            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
218ExpandedSubBlockEnd.gif        }

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

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

230ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
231InBlock.gif        /// 根据指定的编码方式Base64编码
232InBlock.gif        /// </summary>
233InBlock.gif        /// <param name="str"></param>
234InBlock.gif        /// <param name="strEncoding"></param>
235ExpandedSubBlockEnd.gif        /// <returns></returns>

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

240ExpandedSubBlockEnd.gif    }

241ExpandedBlockEnd.gif}


用3DES 加密时 需要注意Asp.net这边的模式需要用CBC模式
des.Mode = System.Security.Cryptography.CipherMode.CBC;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值