c# 加密与解密
来源:互联网 发布:苹果手机音乐软件 编辑:程序博客网 时间:2024/05/01 19:46
三种加密方法,代码如下:
using System.Security.Cryptography; using System.IO; namespace Common { /**//// <summary> /// DESEncrypt加密解密算法。 /// </summary> public sealed class DESEncrypt { private DESEncrypt() { // // TODO: 在此处添加构造函数逻辑 // } private static string key = "zhoufoxcn"; /**//// <summary> /// 对称加密解密的密钥 /// </summary> public static string Key { get { return key; } set { key = value; } } /**//// <summary> /// DES加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public static string DesEncrypt(string encryptString) { byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); byte[] keyIV = keyBytes; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } /**//// <summary> /// DES解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public static string DesDecrypt(string decryptString) { byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); byte[] keyIV = keyBytes; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } } /// <summary> /// 一个实现MD5散列字符串的类 /// 作者:周公 /// 日期:2007 /// </summary> public sealed class MD5Hashing { private static MD5 md5 = MD5.Create(); //私有化构造函数 private MD5Hashing() { } /// <summary> /// 使用utf8编码将字符串散列 /// </summary> /// <param name="sourceString">要散列的字符串</param> /// <returns>散列后的字符串</returns> public static string HashString(string sourceString) { return HashString(Encoding.UTF8, sourceString); } /// <summary> /// 使用指定的编码将字符串散列 /// </summary> /// <param name="encode">编码</param> /// <param name="sourceString">要散列的字符串</param> /// <returns>散列后的字符串</returns> public static string HashString(Encoding encode, string sourceString) { byte[] source = md5.ComputeHash(encode.GetBytes(sourceString)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < source.Length; i++) { sBuilder.Append(source[i].ToString("x2")); } return sBuilder.ToString(); } } /// <summary> /// 实现Base64加密解密 /// 作者:周公 /// 时间:2007 /// </summary> public sealed class Base64 { /// <summary> /// Base64加密 /// </summary> /// <param name="codeName">加密采用的编码方式</param> /// <param name="source">待加密的明文</param> /// <returns></returns> public static string EncodeBase64(Encoding encode, string source) { byte[] bytes = encode.GetBytes(source); string str; try { str = Convert.ToBase64String(bytes);//Convert.ToBase64String(bytes); } catch { str = source; //encode = source; } return str; //return encode } /// <summary> /// Base64加密,采用utf8编码方式加密 /// </summary> /// <param name="source">待加密的明文</param> /// <returns>加密后的字符串</returns> public static string EncodeBase64(string source) { return EncodeBase64(Encoding.UTF8, source); } /// <summary> /// Base64解密 /// </summary> /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string DecodeBase64(Encoding encode, string result) { string decode = ""; byte[] bytes = Convert.FromBase64String(result); try { decode = encode.GetString(bytes); } catch { decode = result; } return decode; } /// <summary> /// Base64解密,采用utf8编码方式解密 /// </summary> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string DecodeBase64(string result) { return DecodeBase64(Encoding.UTF8, result); } }
0 0
- C#加密与解密
- C# 加密与解密
- C#加密与解密
- C#加密与解密
- c# 加密与解密
- C# 加密与解密
- C#数据加密与解密
- C#加密与解密函数
- C# MD5加密与解密
- C#加密与解密函数
- C#加密与解密技术
- C# Base64加密与解密
- C#加密与解密 (转)
- C# DES加密与解密
- 加密与解密 java c#
- c# 加密与解密 Base64Encode
- C#加密与解密数据
- C# RSA加密与解密
- for循环的执行顺序问题
- Python3+Opencv3的安装
- Codefoeces 581B Luxurious Houses
- 桥接模式
- oracle单机11.2.0.4.0升级到11.2.0.4.7
- c# 加密与解密
- Kali虚拟机及VMware Tools安装
- C基本小程序
- bzoj-3545 Peaks
- 浅析插入排序
- CF#322-C-Developing Skills-模拟大水题
- 【static&extern】用GCC输出带源代码的汇编程序进行链接的调试
- 旋转数组的最小数字
- 黑马程序员——C语言日志——指针、地址和变量值