加密解密

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace Lhyi.Helper
{
    /// <summary>
    /// 加密
    /// </summary>
    public class Encryption
    {
        #region 方法
        
        /// <summary>
        /// Base64(MD5)
        /// </summary>
        /// <param name="pEncData"></param>
        /// <returns></returns>
        public static string GetSign(string pEncData)
        {
            string result = null;
            try
            {
                MD5 md = new MD5CryptoServiceProvider();
                byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(pEncData);
                byte[] inArray = md.ComputeHash(bytes);
                result = System.Convert.ToBase64String(inArray, 0, inArray.Length);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Encryption.GetSign(string) :: " + ex.Message);
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 加密 Base64(3DES(加密内容))
        /// </summary>
        /// <param name="content"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encrypt3DES(string content, string key)
        {
            string result = string.Empty;
            try
            {
                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
                byte[] buffer3 = Encoding.GetEncoding("utf-8").GetBytes(content);
                provider.Key = Encoding.GetEncoding("utf-8").GetBytes(key);
                provider.Mode = CipherMode.ECB;
                MemoryStream stream = new MemoryStream();
                ICryptoTransform transform = provider.CreateEncryptor();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
                stream2.Write(buffer3, 0, buffer3.Length);
                stream2.FlushFinalBlock();
                result = System.Convert.ToBase64String(stream.ToArray());
                stream.Flush();
                stream.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Encryption.Encrypt3DES(string, string) :: " + ex.Message);
                throw ex;
            }

            return result;
        }

        /// <summary>
        /// 解密 Base64(3DES(加密内容))
        /// </summary>
        /// <param name="sourceData"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decrypt3DES(string sourceData, string key)
        {
            string result = string.Empty;
            try
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                byte[] content = System.Convert.FromBase64String(sourceData);
                des.Key = Encoding.GetEncoding("utf-8").GetBytes(key);
                des.Mode = CipherMode.ECB;
                MemoryStream ms = new MemoryStream();
                ICryptoTransform transform = des.CreateDecryptor();
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
                cs.Write(content, 0, content.Length);
                cs.FlushFinalBlock();
                byte[] b = ms.ToArray();
                result = Encoding.GetEncoding("utf-8").GetString(b, 0, b.Length);

                ms.Flush();
                ms.Close();
                
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Encryption.Decrypt3DES(string, string) :: " + ex.Message);
                throw ex;
            }
            return result;
        }

        #endregion
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值