ImageVerifierCode 换一换
格式:DOCX , 页数:28 ,大小:20.61KB ,
资源ID:24992792      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/24992792.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#加密算法汇总.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#加密算法汇总.docx

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