zoukankan      html  css  js  c++  java
  • 加密解密类

    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    using System.Management;//引入命名空间

    namespace Comm
    {
    public class DataEncrypt
    {

    //注册日期yyyyMMdd,CPU码,注册天数

    /// <summary>
    /// 获取CUP ID编号
    /// </summary>
    /// <returns></returns>
    public static string GetCPUID()
    {
    string CPU_ID = string.Empty;
    //通过Win32_Processor对象可以获取磁盘驱动器的相关信息:
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc)
    {
    //描述处理器的信息,ID标识
    CPU_ID = mo.Properties["ProcessorId"].Value.ToString();
    break;
    }
    return CPU_ID;
    }

    #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 加密解密

    public static string DES_Key = "tb_zz_rj";//钥匙(没有钥匙解密会失败)

    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="pToEncrypt"></param>
    /// <returns></returns>
    public static string DESEncrypt(string pToEncrypt)
    {
    //pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);


    des.Key = ASCIIEncoding.ASCII.GetBytes(DES_Key);
    des.IV = ASCIIEncoding.ASCII.GetBytes(DES_Key);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();

    StringBuilder ret = new StringBuilder();
    foreach (byte b in ms.ToArray())
    {
    ret.AppendFormat("{0:X2}", b);
    }
    ret.ToString();
    return ret.ToString();
    }


    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="pToDecrypt"></param>
    /// <returns></returns>
    public static string DESDecrypt(string pToDecrypt)
    {
    try
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

    byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
    for (int x = 0; x < pToDecrypt.Length / 2; x++)
    {
    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
    inputByteArray[x] = (byte)i;
    }

    des.Key = ASCIIEncoding.ASCII.GetBytes(DES_Key);
    des.IV = ASCIIEncoding.ASCII.GetBytes(DES_Key);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();

    StringBuilder ret = new StringBuilder();

    return System.Text.Encoding.Default.GetString(ms.ToArray());
    }
    catch (Exception)
    {
    return "";
    }

    }

    #endregion

    #region RC2加密解密
    /// <summary>
    /// RC2 加密(用变长密钥对大量数据进行加密)
    /// </summary>
    /// <param name="EncryptString">待加密密文</param>
    /// <param name="EncryptKey">加密密钥(必须为5-16位)</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">解密密钥(必须为5-16位)</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 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

    const string keyStr = "xixihaha"; //加密的KEY字符,用MD5码生成128位KEY密钥
    const string ivStr = "jijizaza"; //加密的IV字符,用MD5码生成128位IV密钥

    /// <summary>
    /// 十转十六进制
    /// </summary>
    /// <param name="day"></param>
    /// <returns></returns>
    public static string RegisterDayEncrypt10To16(int day)
    {
    string strA = day.ToString("x8");

    return strA;
    }

    /// <summary>
    /// 十六转十进制
    /// </summary>
    /// <param name="day"></param>
    /// <returns></returns>
    public static int RegisterDayEncrypt16To10(string day)
    {
    int num = Int32.Parse(day, System.Globalization.NumberStyles.HexNumber);

    return num;
    }

    }
    }

  • 相关阅读:
    PHP5.5新特性
    并发导论【转】
    Redis常用数据结构和操作
    git常用命令【转】
    curl 的用法指南
    pycurl模块
    单点登陆
    MySql 里的IFNULL、NULLIF和ISNULL用法
    Mysql coalesce()函数认识和用法
    python文件操作
  • 原文地址:https://www.cnblogs.com/jiangyunfeng/p/10608226.html
Copyright © 2011-2022 走看看