1、C#加密算法汇总C#加密算法汇总view sourceprint?01方法一:02/须添加对System.Web的引用 03using System.Web.Security; 0405. 0607/ 08/ SHA1加密字符串 09/ 10/ 源字符串11/ 加密后的字符串12public string SHA1(string source) 13 14return FormsAuthentication.HashPasswordForStoringInConfigFile(source, SHA1); 15 161718/ 19/ MD5加密字符串 20/ 21/ 源字符串22/ 加密后的
2、字符串23public string MD5(string source) 24 25return FormsAuthentication.HashPasswordForStoringInConfigFile(source, MD5); 26view sourceprint?01方法二(可逆加密解密):02using System.Security.Cryptography; 0304. 0506public string Encode(string data) 07 08byte byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64)
3、; 09byte byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); 1011DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); 12int i = cryptoProvider.KeySize; 13MemoryStream ms = new MemoryStream(); 14CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byI
4、V), CryptoStreamMode.Write); 1516StreamWriter sw = new StreamWriter(cst); 17sw.Write(data); 18sw.Flush(); 19cst.FlushFinalBlock(); 20sw.Flush(); 21return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); 2223 2425public string Decode(string data) 26 27byte byKey = System.Text.ASCIIEncoding.
5、ASCII.GetBytes(KEY_64); 28byte byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); 2930byte byEnc; 31try32 33byEnc = Convert.FromBase64String(data); 34 35catch36 37return null; 38 3940DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); 41MemoryStream ms = new MemoryStream(b
6、yEnc); 42CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); 43StreamReader sr = new StreamReader(cst); 44return sr.ReadToEnd(); 45view sourceprint?01方法三(MD5不可逆):02using System.Security.Cryptography; 0304. 0506/MD5不可逆加密 0708/32位加密 0910public s
7、tring GetMD5_32(string s, string _input_charset) 11 12MD5 md5 = new MD5CryptoServiceProvider(); 13byte t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s); 14StringBuilder sb = new StringBuilder(32); 15for (int i = 0; i t.Length; i+) 16 17sb.Append(ti.ToString(x).PadLeft(2, 0); 18 1
8、9return sb.ToString(); 20 2122/16位加密 23public static string GetMd5_16(string ConvertString) 24 25MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 26string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString), 4, 8); 27t2 = t2.Replace(-, ); 28return t2
9、; 29view sourceprint?01方法四(对称加密):02using System.IO; 03using System.Security.Cryptography; 0405. 0607private SymmetricAlgorithm mobjCryptoService; 08private string Key; 09/ 10/ 对称加密类的构造函数 11/ 12public SymmetricMethod() 13 14mobjCryptoService = new RijndaelManaged(); 15Key = Guz(%&hj7x89H$yuBI0456Ftma
10、T5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7; 16 17/ 18/ 获得密钥 19/ 20/ 密钥21private byte GetLegalKey() 22 23string sTemp = Key; 24mobjCryptoService.GenerateKey(); 25byte bytTemp = mobjCryptoService.Key; 26int KeyLength = bytTemp.Length; 27if (sTemp.Length KeyLength) 28sTemp = sTemp.Substring(0, KeyLength);
11、29else if (sTemp.Length KeyLength) 30sTemp = sTemp.PadRight(KeyLength, ); 31return ASCIIEncoding.ASCII.GetBytes(sTemp); 32 33/ 34/ 获得初始向量IV 35/ 36/ 初试向量IV37private byte GetLegalIV() 38 39string sTemp = E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk; 40mobjCryptoService.GenerateIV()
12、; 41byte bytTemp = mobjCryptoService.IV; 42int IVLength = bytTemp.Length; 43if (sTemp.Length IVLength) 44sTemp = sTemp.Substring(0, IVLength); 45else if (sTemp.Length IVLength) 46sTemp = sTemp.PadRight(IVLength, ); 47return ASCIIEncoding.ASCII.GetBytes(sTemp); 48 49/ 50/ 加密方法 51/ 52/ 待加密的串53/ 经过加密的串
13、54public string Encrypto(string Source) 55 56byte bytIn = UTF8Encoding.UTF8.GetBytes(Source); 57MemoryStream ms = new MemoryStream(); 58mobjCryptoService.Key = GetLegalKey(); 59mobjCryptoService.IV = GetLegalIV(); 60ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); 61CryptoStream cs =
14、 new CryptoStream(ms, encrypto, CryptoStreamMode.Write); 62cs.Write(bytIn, 0, bytIn.Length); 63cs.FlushFinalBlock(); 64ms.Close(); 65byte bytOut = ms.ToArray(); 66return Convert.ToBase64String(bytOut); 67 68/ 69/ 解密方法 70/ 71/ 待解密的串72/ 经过解密的串73public string Decrypto(string Source) 74 75byte bytIn = C
15、onvert.FromBase64String(Source); 76MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); 77mobjCryptoService.Key = GetLegalKey(); 78mobjCryptoService.IV = GetLegalIV(); 79ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); 80CryptoStream cs = new CryptoStream(ms, encrypto, CryptoS
16、treamMode.Read); 81StreamReader sr = new StreamReader(cs); 82return sr.ReadToEnd(); 83view sourceprint?01方法五:02using System.IO; 03using System.Security.Cryptography; 04using System.Text; 0506. 0708/默认密钥向量 09private static byte Keys = 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF ; 10/ 11/ DES加密字符串
17、12/ 13/ 待加密的字符串14/ 加密密钥,要求为8位15/ 加密成功返回加密后的字符串,失败返回源串16public static string EncryptDES(string encryptString, string encryptKey) 17 18try19 20byte rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8); 21byte rgbIV = Keys; 22byte inputByteArray = Encoding.UTF8.GetBytes(encryptString); 23DESCrypt
18、oServiceProvider dCSP = new DESCryptoServiceProvider(); 24MemoryStream mStream = new MemoryStream(); 25CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 26cStream.Write(inputByteArray, 0, inputByteArray.Length); 27cStream.FlushFinalBlock()
19、; 28return Convert.ToBase64String(mStream.ToArray(); 29 30catch31 32return encryptString; 33 34 3536/ 37/ DES解密字符串 38/ 39/ 待解密的字符串40/ 解密密钥,要求为8位,和加密密钥相同41/ 解密成功返回解密后的字符串,失败返源串42public static string DecryptDES(string decryptString, string decryptKey) 43 44try45 46byte rgbKey = Encoding.UTF8.GetBytes(
20、decryptKey); 47byte rgbIV = Keys; 48byte inputByteArray = Convert.FromBase64String(decryptString); 49DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 50MemoryStream mStream = new MemoryStream(); 51CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), Cr
21、yptoStreamMode.Write); 52cStream.Write(inputByteArray, 0, inputByteArray.Length); 53cStream.FlushFinalBlock(); 54return Encoding.UTF8.GetString(mStream.ToArray(); 55 56catch57 58return decryptString; 59 60view sourceprint?01方法六(文件加密):02using System.IO; 03using System.Security.Cryptography; 04using S
22、ystem.Text; 0506. 0708/加密文件 09private static void EncryptData(String inName, String outName, byte desKey, byte desIV) 10 11/Create the file streams to handle the input and output files. 12FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); 13FileStream fout = new FileStream(outN
23、ame, FileMode.OpenOrCreate, FileAccess.Write); 14fout.SetLength(0); 1516/Create variables to help with read and write. 17byte bin = new byte100; /This is intermediate storage for the encryption. 18long rdlen = 0; /This is the total number of bytes written. 19long totlen = fin.Length; /This is the to
24、tal length of the input file. 20int len; /This is the number of bytes to be written at a time. 2122DES des = new DESCryptoServiceProvider(); 23CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write); 2425/Read from the input file, then encrypt and write to the output file. 26while (rdlen totlen) 27 28len = fin.Read(bin, 0, 100);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1