博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#加密解密
阅读量:5287 次
发布时间:2019-06-14

本文共 4891 字,大约阅读时间需要 16 分钟。

///   /// 加密/解密工具  ///   public class CEncoder  {      const string key = "a3f3bc6d43e7f10d";      ///       /// 字符串加密.由DESCryptoServiceProvider对象加密      ///       public static string Encode(string str)      {          try          {              DESCryptoServiceProvider provider = new DESCryptoServiceProvider();              provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));              provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));              byte[] bytes = Encoding.Default.GetBytes(str);              MemoryStream stream = new MemoryStream();              CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);              stream2.Write(bytes, 0, bytes.Length);              stream2.FlushFinalBlock();              StringBuilder builder = new StringBuilder();              foreach (byte num in stream.ToArray())              {                  builder.AppendFormat("{0:X2}", num);              }              stream.Close();              return builder.ToString();          }          catch          {              return string.Empty;          }      }      ///       /// 字符串解密.由DESCryptoServiceProvider对象解密      ///       public static string Decode(string str)      {          try          {              DESCryptoServiceProvider provider = new DESCryptoServiceProvider();              provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));              provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));              byte[] buffer = new byte[str.Length / 2];              for (int i = 0; i < (str.Length / 2); i++)              {                  int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);                  buffer[i] = (byte)num2;              }              MemoryStream stream = new MemoryStream();              CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);              stream2.Write(buffer, 0, buffer.Length);              stream2.FlushFinalBlock();              stream.Close();              return Encoding.Default.GetString(stream.ToArray());          }          catch          {              return string.Empty;          }      }      private static int PREFIX_LEN = 6;      ///       /// RETURN [PREFIX]+STRING      ///       public static string Encode(string source, bool genPrefix)      {          if (source == string.Empty) return "";          if (genPrefix)              return GetPrefix(PREFIX_LEN) + CEncoder.Encode(source);          else              return CEncoder.Encode(source);      }      ///       /// [PREFIX]STRING-->STRING      ///       public static string Decode(string source, bool hasPrefix)      {          if (source == string.Empty) return "";          if (source.Length <= PREFIX_LEN || !hasPrefix)              return Decode(source);          else if (hasPrefix)              return Decode(source.Substring(PREFIX_LEN, source.Length - PREFIX_LEN));          else              return Decode(source);      }      ///       /// MD5加密,Use object MD5,返回16字符串      ///       public static string encryptMD5(string content)      {          string retContent = "";          MD5 md5 = MD5.Create();          // 加密后是一个字节类型的数组           byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(content));          // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得           for (int i = 0; i < s.Length; i++)          {              // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,              //如果使用大写(X)则格式后的字符是大写字符               retContent = retContent + s[i].ToString("X");          }          return retContent;      }      ///       /// MD5不可逆加密.use MD5CryptoServiceProvider      ///       public static string GenerateMD5Hash(string input)      {          MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();          byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));          StringBuilder sBuilder = new StringBuilder();          for (int i = 0; i < data.Length; i++)          {              sBuilder.Append(data[i].ToString("x2"));          }          return sBuilder.ToString();      }      ///       /// 密码头      ///       public static string GetPrefix(int bit)      {          //A-Z 0-9          string chars = "0A1D30A4B5C006D72C8E92030F506E7E02F1A0D023B3B40C5E180D6F4D5E6F";          string ret = "";          if (bit < 1) bit = PREFIX_LEN;          for (int i = 1; i <= bit; i++)          {              int n = GetRandomNum(chars.Length - 2);              ret = ret + chars.Substring(n, 1);          }          return ret;      }      ///       /// 取随机数      ///       /// 最大数      /// 
public static int GetRandomNum(int maxValue) { Random seed = new Random(); Random randomNum = new Random(); Thread.Sleep(20); return randomNum.Next(1, maxValue); } }

 

转载于:https://www.cnblogs.com/GarsonZhang/p/4062742.html

你可能感兴趣的文章
dom4j 通用解析器,解析成List<Map<String,Object>>
查看>>
TYVJ.1864.[Poetize I]守卫者的挑战(概率DP)
查看>>
0925 韩顺平java视频
查看>>
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
模板设计模式的应用
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>
Matlab parfor-loop并行运算
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>
旅途上看的电影和观后感
查看>>
Ztree异步树加载
查看>>
关于IE和火狐,谷歌,Safari对Html标签Object和Embed的支持问题
查看>>
poj3320 Jessica's Reading Problem(尺取思路+STL)
查看>>
分布式计算开源框架Hadoop介绍
查看>>
坏的事情不都会带来坏的结果
查看>>
RPC的基础:调研EOS插件http_plugin
查看>>
第二次团队冲刺第二天
查看>>
11)Java abstract class 和 interface
查看>>
使用xrdp或Xmanager 远程连接 CentOS6
查看>>