zoukankan      html  css  js  c++  java
  • C# 加密解密类

    一. MD5
      1 防止看到明文 数据库密码,加盐(原密码+固定字符串,然后再MD5/双MD5)
      2 防篡改
      3 急速秒传(第一次上传文件,保存md5摘要,第二次上传检查md5摘要)
      4文件下载(防篡改,官方发布的时候给一个md5摘要,安装的时候首先检查下摘要)
      5 svn TFS git VSS(本地保存文件的md5摘要,任何修改都会影响md5)

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

       二 . 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("key"));
    10         private static byte[] _rgbIV = ASCIIEncoding.ASCII.GetBytes("value");
    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     }
    View Code

      三 RSA可逆非对称加密

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

       

  • 相关阅读:
    [Dynamic Language] Python Django: 模板引擎(1)基本使用
    [ASM C/C++] C函数调用分析
    [Linux] Ubuntu: 相关工具及配置
    xml小练习
    饿汉式懒汉式
    给定一个数组,获取数组中的最大值,以及最小值。
    Fighting
    通过API文档查询Math类的方法,打印出近似圆,只要给定不同半径,圆的大小就会随之发生改变
    对给定数组进行选择排序
    对给定数组进行冒泡排序
  • 原文地址:https://www.cnblogs.com/saodiseng2015/p/9296639.html
Copyright © 2011-2022 走看看