C#常用加密方法

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
/// <summary>
/// Encrypt 的摘要说明
/// </summary>
public static class Encrypt
{
    #region 解密由AES加密的字符串
    /// <summary>
    /// 解密由AES加密的字符串
    /// </summary>
    /// <param name="source">加密字符串</param>
    /// <param name="key">加密密钥</param>
    /// <returns>解密后的字符串</returns>
    public static string Dencrypt_AES(string source, string key)
    {
        try
        {
            Rijndael rijndael = Rijndael.Create();
            rijndael.Key = Encoding.Default.GetBytes(key);
            rijndael.IV = Encoding.Default.GetBytes(key.Substring(0, 0x10));
            byte[] buffer = Convert.FromBase64String(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Default.GetString(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 解密由DES加密的字符串
    /// <summary>
    /// 解密由DES加密的字符串
    /// </summary>
    /// <param name="source">加密的字符串</param>
    /// <param name="key">加密的密钥</param>
    /// <returns>解密后的字符串</returns>
    public static string Dencrypt_DES(string source, string key)
    {
        try
        {
            DESCryptoServiceProvider provider2 = new DESCryptoServiceProvider
            {
                Key = Encoding.Default.GetBytes(key),
                IV = Encoding.Default.GetBytes(key)
            };
            DESCryptoServiceProvider provider = provider2;
            byte[] buffer = Convert.FromBase64String(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Default.GetString(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 解密由RC2加密的字符串
    /// <summary>
    /// 解密由RC2加密的字符串
    /// </summary>
    /// <param name="source">加密的字符串</param>
    /// <param name="key">加密的密钥</param>
    /// <returns>解密后的字符串</returns>
    public static string DEncrypt_RC2(string source, string key)
    {
        try
        {
            RC2CryptoServiceProvider provider2 = new RC2CryptoServiceProvider
            {
                Key = Encoding.Default.GetBytes(key),
                IV = Encoding.Default.GetBytes(key)
            };
            RC2CryptoServiceProvider provider = provider2;
            byte[] buffer = Convert.FromBase64String(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Default.GetString(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 解密由TripleDES加密的字符串
    /// <summary>
    /// 解密由TripleDES加密的字符串
    /// </summary>
    /// <param name="source">加密的字符串</param>
    /// <param name="key">加密的密钥</param>
    /// <returns>解密后的字符串</returns>
    public static string Dencrypt_TripleDES(string source, string key)
    {
        try
        {
            TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider
            {
                Key = Encoding.Default.GetBytes(key),
                IV = Encoding.Default.GetBytes(key.Substring(0, 8))
            };
            TripleDESCryptoServiceProvider provider = provider2;
            byte[] buffer = Convert.FromBase64String(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Default.GetString(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 使用AES方式进行加密
    /// <summary>
    /// 使用AES方式进行加密
    /// </summary>
    /// <param name="source">需要加密的字符串</param>
    /// <param name="key">加密用的密钥</param>
    /// <returns>加密后的字符串</returns>
    public static string Encrypt_AES(string source, string key)
    {
        try
        {
            Rijndael rijndael = Rijndael.Create();
            rijndael.Key = Encoding.Default.GetBytes(key);
            rijndael.IV = Encoding.Default.GetBytes(key.Substring(0, 0x10));
            byte[] bytes = Encoding.Default.GetBytes(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 使用DES方式进行加密
    /// <summary>
    /// 使用DES方式进行加密
    /// </summary>
    /// <param name="source">需要加密的字符串</param>
    /// <param name="key">加密用的密钥</param>
    /// <returns>加密后的字符串</returns>
    public static string Encrypt_DES(string source, string key)
    {
        try
        {
            DESCryptoServiceProvider provider2 = new DESCryptoServiceProvider
            {
                Key = Encoding.Default.GetBytes(key),
                IV = Encoding.Default.GetBytes(key)
            };
            DESCryptoServiceProvider provider = provider2;
            byte[] bytes = Encoding.Default.GetBytes(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 使用MD5方式进行加密
    /// <summary>
    /// 使用MD5方式进行加密
    /// </summary>
    /// <param name="source">需要加密的字符串</param>
    /// <param name="key">加密用的密钥</param>
    /// <returns>加密后的字符串</returns>
    public static string Encrypt_MD5(string source)
    {
        string str = "";
        MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
        byte[] bytes = Encoding.Default.GetBytes(source);
        byte[] buffer2 = provider.ComputeHash(bytes);
        foreach (byte num in buffer2)
        {
            str = str + num.ToString("X").PadLeft(2, '0');
        }
        return str;
    }
    #endregion
 
    #region 使用RC2方式进行加密
    /// <summary>
    /// 使用RC2方式进行加密
    /// </summary>
    /// <param name="source">需要加密的字符串</param>
    /// <param name="key">加密用的密钥</param>
    /// <returns>加密后的字符串</returns>
    public static string Encrypt_RC2(string source, string key)
    {
        try
        {
            RC2CryptoServiceProvider provider2 = new RC2CryptoServiceProvider
            {
                Key = Encoding.Default.GetBytes(key),
                IV = Encoding.Default.GetBytes(key)
            };
            RC2CryptoServiceProvider provider = provider2;
            byte[] bytes = Encoding.Default.GetBytes(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 使用TripleDES方式进行加密
    /// <summary>
    /// 使用TripleDES方式进行加密
    /// </summary>
    /// <param name="source">需要加密的字符串</param>
    /// <param name="key">加密用的密钥</param>
    /// <returns>加密后的字符串</returns>
    public static string Encrypt_TripleDES(string source, string key)
    {
        try
        {
            TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider
            {
                Key = Encoding.Default.GetBytes(key),
                IV = Encoding.Default.GetBytes(key.Substring(0, 8))
            };
            TripleDESCryptoServiceProvider provider = provider2;
            byte[] bytes = Encoding.Default.GetBytes(source);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }
        catch
        {
            return null;
        }
    }
    #endregion
 
    #region 获取加密的密钥
    /// <summary>
    /// 获取加密的密钥
    /// </summary>
    /// <param name="EncryptType">加密方式</param>
    /// <returns>密钥</returns>
    public static string GetKey(EncryptType encryptType)
    {
        switch (encryptType)
        {
            case EncryptType.AES:
                return "1zx2fqw3eas4dtg8hui9opl6dfh5aok0";
 
            case EncryptType.DES:
                return "zxc52mxb";
 
            case EncryptType.TDES:
                return "zxc52mxbqwdftghu24dfhji0";
 
            case EncryptType.RC2:
                return "qwedfgbd";
        }
        return "";
    }
    #endregion
 
    public enum EncryptType:byte
    {
        AES,
        DES,
        TDES,
        RC2
    }
}
posted @ 2014-05-10 11:58  网事  阅读(217)  评论(0编辑  收藏  举报