C#加密解密类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

/// <summary>
///SDKSecurity 的摘要说明
///Edit by JaymeZhang
/// </summary>
public class SDKSecurity
{
 public SDKSecurity()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    #region MD5 加密
    /// <summary>
    /// MD5 加密静态方法
    /// </summary>
    /// <param name="EncryptString">待加密的密文</param>
    /// <returns>returns</returns>
    public static string MD5Encrypt(string EncryptString)
    {
        if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
        MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
        string m_strEncrypt = "";
        try
        {
            m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
        }
        catch (ArgumentException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_ClassMD5.Clear(); }
        return m_strEncrypt;
    }
    #endregion

    #region DES 加密解密
    /// <summary>
    /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
    /// </summary>
    /// <param name="EncryptString">待加密的密文</param>
    /// <param name="EncryptKey">加密的密钥</param>
    /// <returns>returns</returns>
    public static string DESEncrypt(string EncryptString, string EncryptKey)
    {
        if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
        if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
        if (EncryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
        byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        string m_strEncrypt = "";
        DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
        try
        {
            byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
            MemoryStream m_stream = new MemoryStream();
            CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
            m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
            m_cstream.FlushFinalBlock();
            m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
            m_stream.Close(); m_stream.Dispose();
            m_cstream.Close(); m_cstream.Dispose();
        }
        catch (IOException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (ArgumentException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_DESProvider.Clear(); }

        return m_strEncrypt;
    }
    /// <summary>
    /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
    /// </summary>
    /// <param name="DecryptString">待解密的密文</param>
    /// <param name="DecryptKey">解密的密钥</param>
    /// <returns>returns</returns>
    public static string DESDecrypt(string DecryptString, string DecryptKey)
    {
        if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
        if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
        if (DecryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
        byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        string m_strDecrypt = "";
        DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
        try
        {
            byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
            MemoryStream m_stream = new MemoryStream();
            CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
            m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
            m_cstream.FlushFinalBlock();
            m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
            m_stream.Close(); m_stream.Dispose();
            m_cstream.Close(); m_cstream.Dispose();
        }
        catch (IOException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (ArgumentException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_DESProvider.Clear(); }

        return m_strDecrypt;
    }
    #endregion

    #region  RC2 加密解密
    /// <summary>
    /// RC2 加密(用变长密钥对大量数据进行加密)
    /// </summary>
    /// <param name="EncryptString">待加密密文</param>
    /// <param name="EncryptKey">加密密钥</param>
    /// <returns>returns</returns>
    public static string RC2Encrypt(string EncryptString, string EncryptKey)
    {
        if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
        if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
        if (EncryptKey.Length < 5 || EncryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
        string m_strEncrypt = "";
        byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
        try
        {
            byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
            MemoryStream m_stream = new MemoryStream();
            CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
            m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
            m_cstream.FlushFinalBlock();
            m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
            m_stream.Close(); m_stream.Dispose();
            m_cstream.Close(); m_cstream.Dispose();
        }
        catch (IOException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (ArgumentException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_RC2Provider.Clear(); }
        return m_strEncrypt;
    }
    /// <summary>
    /// RC2 解密(用变长密钥对大量数据进行加密)
    /// </summary>
    /// <param name="DecryptString">待解密密文</param>
    /// <param name="DecryptKey">解密密钥</param>
    /// <returns>returns</returns>
    public static string RC2Decrypt(string DecryptString, string DecryptKey)
    {
        if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
        if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
        if (DecryptKey.Length < 5 || DecryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
        byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        string m_strDecrypt = "";
        RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
        try
        {
            byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
            MemoryStream m_stream = new MemoryStream();
            CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
            m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
            m_cstream.FlushFinalBlock();
            m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
            m_stream.Close(); m_stream.Dispose();
            m_cstream.Close(); m_cstream.Dispose();
        }
        catch (IOException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (ArgumentException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_RC2Provider.Clear(); }
        return m_strDecrypt;
    }
    #endregion

    #region 3DES 加密解密
    /// <summary>
    /// 3DES 加密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
    /// </summary>
    /// <param name="EncryptString">待加密密文</param>
    /// <param name="EncryptKey1">密钥一</param>
    /// <param name="EncryptKey2">密钥二</param>
    /// <param name="EncryptKey3">密钥三</param>
    /// <returns>returns</returns>
    public static string DES3Encrypt(string EncryptString, string EncryptKey1, string EncryptKey2, string EncryptKey3)
    {
        string m_strEncrypt = "";

        try
        {
            m_strEncrypt = DESEncrypt(EncryptString, EncryptKey3);

            m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey2);

            m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey1);
        }
        catch (Exception ex) { throw ex; }

        return m_strEncrypt;
    }
    /// <summary>
    /// 3DES 解密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
    /// </summary>
    /// <param name="DecryptString">待解密密文</param>
    /// <param name="DecryptKey1">密钥一</param>
    /// <param name="DecryptKey2">密钥二</param>
    /// <param name="DecryptKey3">密钥三</param>
    /// <returns>returns</returns>
    public static string DES3Decrypt(string DecryptString, string DecryptKey1, string DecryptKey2, string DecryptKey3)
    {
        string m_strDecrypt = "";

        try
        {
            m_strDecrypt = DESDecrypt(DecryptString, DecryptKey1);

            m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey2);

            m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey3);
        }
        catch (Exception ex) { throw ex; }

        return m_strDecrypt;
    }
    #endregion

    #region AES加密解密
    /// <summary>
    /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
    /// </summary>
    /// <param name="EncryptString">待加密密文</param>
    /// <param name="EncryptKey">加密密钥</param>
    /// <returns></returns>
    public static string AESEncrypt(string EncryptString, string EncryptKey)
    {
        if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
        if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
        string m_strEncrypt = "";
        byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
        Rijndael m_AESProvider = Rijndael.Create();
        try
        {
            byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
            MemoryStream m_stream = new MemoryStream();
            CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
            m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_csstream.FlushFinalBlock();
            m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
            m_stream.Close(); m_stream.Dispose();
            m_csstream.Close(); m_csstream.Dispose();
        }
        catch (IOException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (ArgumentException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_AESProvider.Clear(); }
        return m_strEncrypt;
    }
    /// <summary>
    /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
    /// </summary>
    /// <param name="DecryptString">待解密密文</param>
    /// <param name="DecryptKey">解密密钥</param>
    /// <returns></returns>
    public static string AESDecrypt(string DecryptString, string DecryptKey)
    {
        if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
        if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
        string m_strDecrypt = "";
        byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
        Rijndael m_AESProvider = Rijndael.Create();
        try
        {
            byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
            MemoryStream m_stream = new MemoryStream();
            CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
            m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
            m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
            m_stream.Close(); m_stream.Dispose();
            m_csstream.Close(); m_csstream.Dispose();
        }
        catch (IOException ex) { throw ex; }
        catch (CryptographicException ex) { throw ex; }
        catch (ArgumentException ex) { throw ex; }
        catch (Exception ex) { throw ex; }
        finally { m_AESProvider.Clear(); }

        return m_strDecrypt;
    }
    #endregion

    #region 1、方法一
    /// <summary>
    /// 1、方法一 (不可逆加密
    /// </summary>
    /// <param name="PasswordString"></param>
    /// <param name="PasswordFormat"></param>
    /// <returns></returns>
    public string EncryptPassword(string PasswordString, string PasswordFormat)
    {
        string encryptPassword = null;
        if (PasswordFormat == "SHA1")
        {
            //encryptPassword = FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
            encryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
        }
        else if (PasswordFormat == "MD5")
        {
            //encryptPassword =  FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
            encryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
        }
        return encryptPassword;
    }
    #endregion

    #region 2、方法二 (可逆加密)
    public interface IBindesh
    {
        string encode(string str);
        string decode(string str);
    }

    public class EncryptionDecryption : IBindesh
    {
        public string encode(string str)
        {
            string htext = "";

            for (int i = 0; i < str.Length; i++)
            {
                htext = htext + (char)(str[i] + 10 - 1 * 2);
            }
            return htext;
        }

        public string decode(string str)
        {
            string dtext = "";

            for (int i = 0; i < str.Length; i++)
            {
                dtext = dtext + (char)(str[i] - 10 + 1 * 2);
            }
            return dtext;
        }
    }
    #endregion

    #region 3、方法三 (可逆加密)
    const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
    const string IV_64 = "VavicApp";
    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="data">待加密的数据字符</param>
    /// <returns></returns>
    public string Encode(string data)
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        int i = cryptoProvider.KeySize;
        MemoryStream ms = new MemoryStream();
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

        StreamWriter sw = new StreamWriter(cst);
        sw.Write(data);
        sw.Flush();
        cst.FlushFinalBlock();
        sw.Flush();
        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    }
    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="data">待解密的数据字符</param>
    /// <returns></returns>
    public string Decode(string data)
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

        byte[] byEnc;
        try
        {
            byEnc = Convert.FromBase64String(data);
        }
        catch
        {
            return null;
        }

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream(byEnc);
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cst);
        return sr.ReadToEnd();
    }
    #endregion

    #region 4、方法四 MD5不可逆加密 (32位加密)
    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="s"></param>
    /// <param name="_input_charset"></param>
    /// <returns></returns>
    public string GetMD5(string s, string _input_charset)
    {

        /// <summary>
        /// 与ASP兼容的MD5加密算法
        /// </summary>

        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
        StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < t.Length; i++)
        {
            sb.Append(t[i].ToString("x").PadLeft(2, '0'));
        }
        return sb.ToString();
    }

    /// <summary>
    /// MD5加密 (16位加密)
    /// </summary>
    /// <param name="ConvertString"></param>
    /// <returns></returns>
    public static string GetMd5Str(string ConvertString)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
        t2 = t2.Replace("-", "");
        return t2;
    }
    #endregion

    #region 5、方法五 加密解密文本文件

    /// <summary>
    /// 加密文件
    /// </summary>
    /// <param name="inName"></param>
    /// <param name="outName"></param>
    /// <param name="desKey"></param>
    /// <param name="desIV"></param>
    private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {
        //Create the file streams to handle the input and output files.
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(0);

        //Create variables to help with read and write.
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
        long rdlen = 0;              //This is the total number of bytes written.
        long totlen = fin.Length;    //This is the total length of the input file.
        int len;                     //This is the number of bytes to be written at a time.

        DES des = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

        //Read from the input file, then encrypt and write to the output file.
        while (rdlen < totlen)
        {
            len = fin.Read(bin, 0, 100);
            encStream.Write(bin, 0, len);
            rdlen = rdlen + len;
        }
        encStream.Close();
        fout.Close();
        fin.Close();
    }

    /// <summary>
    /// 解密文件
    /// </summary>
    /// <param name="inName"></param>
    /// <param name="outName"></param>
    /// <param name="desKey"></param>
    /// <param name="desIV"></param>
    private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {
        //Create the file streams to handle the input and output files.
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(0);

        //Create variables to help with read and write.
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
        long rdlen = 0;              //This is the total number of bytes written.
        long totlen = fin.Length;    //This is the total length of the input file.
        int len;                     //This is the number of bytes to be written at a time.

        DES des = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);

        //Read from the input file, then encrypt and write to the output file.
        while (rdlen < totlen)
        {
            len = fin.Read(bin, 0, 100);
            encStream.Write(bin, 0, len);
            rdlen = rdlen + len;
        }
        encStream.Close();
        fout.Close();
        fin.Close();
    }
    #endregion

    #region 6、方法六 DES加密解密
    //默认密钥向量
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encryptString">待加密的字符串</param>
    /// <param name="encryptKey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
    public static string EncryptDES(string encryptString, string encryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        catch
        {
            return encryptString;
        }
    }
    /// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="decryptString">待解密的字符串</param>
    /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
    public static string DecryptDES(string decryptString, string decryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
        {
            return decryptString;
        }
    }
    #endregion

  }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值