码迷,mamicode.com
首页 > 其他好文 > 详细

MD5,DES,RSA

时间:2020-04-28 11:13:56      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:class   签名   open   insert   ide   algorithm   需要   md5   密文   

 1,MD5

  用途:

  1 防止明文:加密后保存比较安全,下次加密检验;密码要复杂一点

  2 防篡改:文件上传;文件下载防挂马;svn

  3 防止抵赖:需要一个第三方认证,数字签名

技术图片
 1     /// <summary>
 2     /// 不可逆加密
 3     /// 1 防止被篡改
 4     /// 2 防止明文存储
 5     /// 3 防止抵赖,数字签名
 6     /// </summary>
 7     public class MD5Encrypt
 8     {
 9         #region MD5
10         /// <summary>
11         /// MD5加密,和动网上的16/32位MD5加密结果相同,
12         /// 使用的UTF8编码
13         /// </summary>
14         /// <param name="source">待加密字串</param>
15         /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
16         /// <returns>加密后的字串</returns>
17         public static string Encrypt(string source, int length = 32)//默认参数
18         {
19             if (string.IsNullOrEmpty(source)) return string.Empty;
20             HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
21             byte[] bytes = Encoding.UTF8.GetBytes(source);//这里需要区别编码的
22             byte[] hashValue = provider.ComputeHash(bytes);
23             StringBuilder sb = new StringBuilder();
24             switch (length)
25             {
26                 case 16://16位密文是32位密文的9到24位字符
27                     for (int i = 4; i < 12; i++)
28                     {
29                         sb.Append(hashValue[i].ToString("x2"));
30                     }
31                     break;
32                 case 32:
33                     for (int i = 0; i < 16; i++)
34                     {
35                         sb.Append(hashValue[i].ToString("x2"));
36                     }
37                     break;
38                 default:
39                     for (int i = 0; i < hashValue.Length; i++)
40                     {
41                         sb.Append(hashValue[i].ToString("x2"));
42                     }
43                     break;
44             }
45             return sb.ToString();
46         }
47         #endregion MD5
48 
49         #region MD5摘要
50         /// <summary>
51         /// 获取文件的MD5摘要
52         /// </summary>
53         /// <param name="fileName"></param>
54         /// <returns></returns>
55         public static string AbstractFile(string fileName)
56         {
57             FileStream file = new FileStream(fileName, FileMode.Open);
58             MD5 md5 = new MD5CryptoServiceProvider();
59             byte[] retVal = md5.ComputeHash(file);
60             file.Close();
61 
62             StringBuilder sb = new StringBuilder();
63             for (int i = 0; i < retVal.Length; i++)
64             {
65                 sb.Append(retVal[i].ToString("x2"));
66             }
67             return sb.ToString();
68         }
69         #endregion
70     }
View Code

  2,DES

  可逆对称加密

技术图片
 1     /// <summary>
 2     /// DES AES Blowfish
 3     /// 对称加密算法的优点是速度快,
 4     /// 缺点是密钥管理不方便,要求共享密钥。
 5     /// 可逆对称加密  密钥长度8
 6     /// </summary>
 7     public class DesEncrypt
 8     {
 9         private static byte[] _rgbKey = ASCIIEncoding.ASCII.GetBytes(Constant.DesKey.Substring(0, 8));
10         private static byte[] _rgbIV = ASCIIEncoding.ASCII.GetBytes(Constant.DesKey.Insert(0, "w").Substring(0, 8));
11 
12         /// <summary>
13         /// DES 加密
14         /// </summary>
15         /// <param name="text">需要加密的值</param>
16         /// <returns>加密后的结果</returns>
17         public static string Encrypt(string text)
18         {
19             DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
20             using (MemoryStream memStream = new MemoryStream())
21             {
22                 CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
23                 StreamWriter sWriter = new StreamWriter(crypStream);
24                 sWriter.Write(text);
25                 sWriter.Flush();
26                 crypStream.FlushFinalBlock();
27                 memStream.Flush();
28                 return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
29             }
30         }
31 
32         /// <summary>
33         /// DES解密
34         /// </summary>
35         /// <param name="encryptText"></param>
36         /// <returns>解密后的结果</returns>
37         public static string Decrypt(string encryptText)
38         {
39             DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
40             byte[] buffer = Convert.FromBase64String(encryptText);
41 
42             using (MemoryStream memStream = new MemoryStream())
43             {
44                 CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
45                 crypStream.Write(buffer, 0, buffer.Length);
46                 crypStream.FlushFinalBlock();
47                 return ASCIIEncoding.UTF8.GetString(memStream.ToArray());
48             }
49         }
50     }
51 
52 
53     public static class Constant
54     {
55         public static string DesKey = AppSettings("DesKey", "ruanmou1");
56         
57         private static T AppSettings<T>(string key, T defaultValue)
58         {
59             var v = ConfigurationManager.AppSettings[key];
60             return String.IsNullOrEmpty(v) ? defaultValue : (T)Convert.ChangeType(v, typeof(T));
61         }
62     }
63 
64 
65     //app.config
66 <?xml version="1.0" encoding="utf-8" ?>
67 <configuration>
68   <startup>
69     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
70   </startup>
71   <appSettings>
72     <add key="DesKey" value="eleven11"/>
73   </appSettings>
74 </configuration>
View Code

  3,RSA

  可逆非对称加密

  加密钥  解密钥  钥匙的功能划分

  公钥  私钥      公开程度划分

  加密钥公开,解密钥揣兜里,有什么作用呢?  保证内容只有我看得到,防止外传

  解密钥公开,加密钥揣兜里,有什么作用呢?  保证数据不能篡改,一定来自于我

技术图片
 1     /// <summary>
 2     /// RSA ECC
 3     /// 可逆非对称加密 
 4     /// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。
 5     /// </summary>
 6     public class RsaEncrypt
 7     {
 8 
 9         /// <summary>
10         /// 获取加密/解密对
11         /// Encrypt   Decrypt
12         /// </summary>
13         /// <returns>Encrypt   Decrypt</returns>
14         public static KeyValuePair<string, string> GetKeyPair()
15         {
16             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
17             string publicKey = RSA.ToXmlString(false);
18             string privateKey = RSA.ToXmlString(true);
19             return new KeyValuePair<string, string>(publicKey, privateKey);
20         }
21 
22         /// <summary>
23         /// 加密:内容+加密key
24         /// </summary>
25         /// <param name="content"></param>
26         /// <param name="encryptKey">加密key</param>
27         /// <returns></returns>
28         public static string Encrypt(string content, string encryptKey)
29         {
30             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
31             rsa.FromXmlString(encryptKey);
32             UnicodeEncoding ByteConverter = new UnicodeEncoding();
33             byte[] DataToEncrypt = ByteConverter.GetBytes(content);
34             byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false);
35             return Convert.ToBase64String(resultBytes);
36         }
37 
38         /// <summary>
39         /// 解密  内容+解密key
40         /// </summary>
41         /// <param name="content"></param>
42         /// <param name="decryptKey">解密key</param>
43         /// <returns></returns>
44         public static string Decrypt(string content, string decryptKey)
45         {
46             byte[] dataToDecrypt = Convert.FromBase64String(content);
47             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
48             RSA.FromXmlString(decryptKey);
49             byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false);
50             UnicodeEncoding ByteConverter = new UnicodeEncoding();
51             return ByteConverter.GetString(resultBytes);
52         }
53 
54 
55         /// <summary>
56         /// 可以合并在一起的,,每次产生一组新的密钥
57         /// </summary>
58         /// <param name="content"></param>
59         /// <param name="encryptKey">加密key</param>
60         /// <param name="decryptKey">解密key</param>
61         /// <returns>加密后结果</returns>
62         private static string Encrypt(string content, out string publicKey, out string privateKey)
63         {
64             RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
65             publicKey = rsaProvider.ToXmlString(false);
66             privateKey = rsaProvider.ToXmlString(true);
67 
68             UnicodeEncoding ByteConverter = new UnicodeEncoding();
69             byte[] DataToEncrypt = ByteConverter.GetBytes(content);
70             byte[] resultBytes = rsaProvider.Encrypt(DataToEncrypt, false);
71             return Convert.ToBase64String(resultBytes);
72         }
73     }
View Code

  4,CA

  技术图片

  5,单边认证

  技术图片

  6,双边认证

   技术图片

 

MD5,DES,RSA

标签:class   签名   open   insert   ide   algorithm   需要   md5   密文   

原文地址:https://www.cnblogs.com/shangec/p/9802112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!