页首Html代码

sql字段加密解密存储

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;
        }
    }
}

 

posted @ 2018-07-09 10:11 Mask-male 阅读(...) 评论(...) 编辑 收藏
页脚Html代码