欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sql字段加密解密存储

程序员文章站 2022-03-12 14:50:01
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using Syste... ......
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;
        }
    }
}