sql字段加密解密存储
2018-07-09 13:40:15来源:博客园 阅读 ()
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace Topevery.Basis.Web.Code { public class CryptographyHelper { private DESCryptoServiceProvider key = null; public CryptographyHelper() { key = new DESCryptoServiceProvider(); key.Key = Encoding.UTF8.GetBytes("WilsonFu"); key.IV = Encoding.UTF8.GetBytes("WilsonFu"); } /// <summary> /// 加密 /// </summary> /// <param name="PlainText"></param> /// <returns></returns> public string Encrypt(string PlainText) { try { MemoryStream ms = new MemoryStream(); CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(encStream); sw.WriteLine(PlainText); sw.Close(); encStream.Close(); byte[] buffer = ms.ToArray(); ms.Close(); return Convert.ToBase64String(buffer); } catch (Exception) { return string.Empty; } } /// <summary> /// 解密 /// </summary> /// <param name="value"></param> /// <returns></returns> public string Decrypt(string value) { try { byte[] CypherText = Convert.FromBase64String(value); MemoryStream ms = new MemoryStream(CypherText); CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); StreamReader sr = new StreamReader(encStream); string val = sr.ReadLine(); sr.Close(); encStream.Close(); ms.Close(); return val; } catch (Exception) { return string.Empty; } } /// <summary> /// 将字节数组转换成字符串 /// </summary> /// <param name="bytes"></param> /// <returns></returns> private string BytesToString(Byte[] bytes) { char[] chars = new char[bytes.Length]; for (int i = 0; i < bytes.Length; i++) { chars[i] = (char)bytes[i]; } string result = new string(chars); return result; } /// <summary> /// 将字符串转换成字节数组 /// </summary> /// <param name="value"></param> /// <returns></returns> private Byte[] StringToBytes(string value) { byte[] bValue = new byte[value.Length]; for (int i = 0; i < value.Length; i++) { bValue[i] = (byte)value[i]; } return bValue; } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- SqlDataReader指定转换无效的解决方法 2020-03-23
- 让Sqlite脱离VC++ Runtime独立运行的方法 2020-03-03
- 深入浅析.NET应用程序SQL注入 2020-02-23
- 如何在C#中使用SQLite数据库 2019-08-26
- 【转载】如何查看sqlserver客户端的版本号信息 2019-07-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash