sql字段加密解密存储

2018-07-09 13:40:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:第二节. SignalR开篇以及如何指定传输协议

下一篇:Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutr