03 | using System.Web.Security; |
10 | /// <param name="source">源字符串</param> |
11 | /// <returns>加密后的字符串</returns> |
12 | public string SHA1( string source) |
14 | return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1" ); |
21 | /// <param name="source">源字符串</param> |
22 | /// <returns>加密后的字符串</returns> |
23 | public string MD5( string source) |
25 | return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5" );; |
02 | using System.Security.Cryptography; |
06 | public string Encode( string data) |
08 | byte [] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); |
09 | byte [] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); |
11 | DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); |
12 | int i = cryptoProvider.KeySize; |
13 | MemoryStream ms = new MemoryStream(); |
14 | CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); |
16 | StreamWriter sw = new StreamWriter(cst); |
19 | cst.FlushFinalBlock(); |
21 | return Convert.ToBase64String(ms.GetBuffer(), 0, ( int )ms.Length); |
25 | public string Decode( string data) |
27 | byte [] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); |
28 | byte [] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); |
33 | byEnc = Convert.FromBase64String(data); |
40 | DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); |
41 | MemoryStream ms = new MemoryStream(byEnc); |
42 | CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); |
43 | StreamReader sr = new StreamReader(cst); |
44 | return sr.ReadToEnd(); |
02 | using System.Security.Cryptography; |
10 | public string GetMD5_32( string s, string _input_charset) |
12 | MD5 md5 = new MD5CryptoServiceProvider(); |
13 | byte [] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); |
14 | StringBuilder sb = new StringBuilder(32); |
15 | for ( int i = 0; i < t.Length; i++) |
17 | sb.Append(t[i].ToString( "x" ).PadLeft(2, '0' )); |
23 | public static string GetMd5_16( string ConvertString) |
25 | MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); |
26 | string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8); |
27 | t2 = t2.Replace( "-" , "" ); |
03 | using System.Security.Cryptography; |
07 | private SymmetricAlgorithm mobjCryptoService; |
12 | public SymmetricMethod() |
14 | mobjCryptoService = new RijndaelManaged(); |
15 | Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7" ; |
20 | /// <returns>密钥</returns> |
21 | private byte [] GetLegalKey() |
24 | mobjCryptoService.GenerateKey(); |
25 | byte [] bytTemp = mobjCryptoService.Key; |
26 | int KeyLength = bytTemp.Length; |
27 | if (sTemp.Length > KeyLength) |
28 | sTemp = sTemp.Substring(0, KeyLength); |
29 | else if (sTemp.Length < KeyLength) |
30 | sTemp = sTemp.PadRight(KeyLength, ' ' ); |
31 | return ASCIIEncoding.ASCII.GetBytes(sTemp); |
36 | /// <returns>初试向量IV</returns> |
37 | private byte [] GetLegalIV() |
39 | string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk" ; |
40 | mobjCryptoService.GenerateIV(); |
41 | byte [] bytTemp = mobjCryptoService.IV; |
42 | int IVLength = bytTemp.Length; |
43 | if (sTemp.Length > IVLength) |
44 | sTemp = sTemp.Substring(0, IVLength); |
45 | else if (sTemp.Length < IVLength) |
46 | sTemp = sTemp.PadRight(IVLength, ' ' ); |
47 | return ASCIIEncoding.ASCII.GetBytes(sTemp); |
52 | /// <param name="Source">待加密的串</param> |
53 | /// <returns>经过加密的串</returns> |
54 | public string Encrypto( string Source) |
56 | byte [] bytIn = UTF8Encoding.UTF8.GetBytes(Source); |
57 | MemoryStream ms = new MemoryStream(); |
58 | mobjCryptoService.Key = GetLegalKey(); |
59 | mobjCryptoService.IV = GetLegalIV(); |
60 | ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); |
61 | CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); |
62 | cs.Write(bytIn, 0, bytIn.Length); |
65 | byte [] bytOut = ms.ToArray(); |
66 | return Convert.ToBase64String(bytOut); |
71 | /// <param name="Source">待解密的串</param> |
72 | /// <returns>经过解密的串</returns> |
73 | public string Decrypto( string Source) |
75 | byte [] bytIn = Convert.FromBase64String(Source); |
76 | MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); |
77 | mobjCryptoService.Key = GetLegalKey(); |
78 | mobjCryptoService.IV = GetLegalIV(); |
79 | ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); |
80 | CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); |
81 | StreamReader sr = new StreamReader(cs); |
82 | return sr.ReadToEnd(); |
03 | using System.Security.Cryptography; |
09 | private static byte [] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; |
13 | /// <param name="encryptString">待加密的字符串</param> |
14 | /// <param name="encryptKey">加密密钥,要求为8位</param> |
15 | /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> |
16 | public static string EncryptDES( string encryptString, string encryptKey) |
20 | byte [] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); |
22 | byte [] inputByteArray = Encoding.UTF8.GetBytes(encryptString); |
23 | DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); |
24 | MemoryStream mStream = new MemoryStream(); |
25 | CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); |
26 | cStream.Write(inputByteArray, 0, inputByteArray.Length); |
27 | cStream.FlushFinalBlock(); |
28 | return Convert.ToBase64String(mStream.ToArray()); |
39 | /// <param name="decryptString">待解密的字符串</param> |
40 | /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> |
41 | /// <returns>解密成功返回解密后的字符串,失败返源串</returns> |
42 | public static string DecryptDES( string decryptString, string decryptKey) |
46 | byte [] rgbKey = Encoding.UTF8.GetBytes(decryptKey); |
48 | byte [] inputByteArray = Convert.FromBase64String(decryptString); |
49 | DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); |
50 | MemoryStream mStream = new MemoryStream(); |
51 | CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); |
52 | cStream.Write(inputByteArray, 0, inputByteArray.Length); |
53 | cStream.FlushFinalBlock(); |
54 | return Encoding.UTF8.GetString(mStream.ToArray()); |
03 | using System.Security.Cryptography; |
09 | private static void EncryptData(String inName, String outName, byte [] desKey, byte [] desIV) |
12 | FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); |
13 | FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); |
17 | byte [] bin = new byte [100]; |
19 | long totlen = fin.Length; |
22 | DES des = new DESCryptoServiceProvider(); |
23 | CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write); |
26 | while (rdlen < totlen) |
28 | len = fin.Read(bin, 0, 100); |
29 | encStream.Write(bin, 0, len); |
39 | private static void DecryptData(String inName, String outName, byte [] desKey, byte [] desIV) |
42 | FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); |
43 | FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); |
47 | byte [] bin = new byte [100]; |
49 | long totlen = fin.Length; |
52 | DES des = new DESCryptoServiceProvider(); |
53 | CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write); |
56 | while (rdlen < totlen) |
58 | len = fin.Read(bin, 0, 100); |
59 | encStream.Write(bin, 0, len); |
02 | using System.Security.Cryptography; |
08 | /// <param name="strPlain">明文</param> |
09 | /// <param name="strDESKey">密钥</param> |
10 | /// <param name="strDESIV">向量</param> |
11 | /// <returns>密文</returns> |
12 | public string DESEncrypt( string strPlain, string strDESKey, string strDESIV) |
15 | byte [] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey); |
17 | byte [] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV); |
19 | DESCryptoServiceProvider desEncrypt= new DESCryptoServiceProvider(); |
21 | MemoryStream msEncrypt= new MemoryStream(); |
23 | CryptoStream csEncrypt= new CryptoStream(msEncrypt,desEncrypt.CreateEncryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Write); |
25 | StreamWriter swEncrypt= new StreamWriter(csEncrypt); |
27 | swEncrypt.WriteLine(strPlain); |
33 | byte [] bytesCipher=msEncrypt.ToArray(); |
37 | return UnicodeEncoding.Unicode.GetString(bytesCipher); |
46 | /// <param name="strCipher">密文</param> |
47 | /// <param name="strDESKey">密钥</param> |
48 | /// <param name="strDESIV">向量</param> |
49 | /// <returns>明文</returns> |
50 | public string DESDecrypt( string strCipher, string strDESKey, string strDESIV) |
53 | byte [] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey); |
55 | byte [] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV); |
57 | byte [] bytesCipher=UnicodeEncoding.Unicode.GetBytes(strCipher); |
59 | DESCryptoServiceProvider desDecrypt= new DESCryptoServiceProvider(); |
61 | MemoryStream msDecrypt= new MemoryStream(bytesCipher); |
63 | CryptoStream csDecrypt= new CryptoStream(msDecrypt,desDecrypt.CreateDecryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Read); |
65 | StreamReader srDecrypt= new StreamReader(csDecrypt); |
67 | string strPlainText=srDecrypt.ReadLine(); |
转载于:https://www.cnblogs.com/skyshenwei/archive/2010/03/03/1677064.html