.Net(c#)加密解密之Aes和Des_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > .Net(c#)加密解密之Aes和Des

.Net(c#)加密解密之Aes和Des

 2015/1/14 16:28:35  青风坠  程序员俱乐部  我要评论(0)
  • 摘要:.Net(c#)加密解密工具类:///<summary>///.Net加密解密帮助类///</summary>publicclassNetCryptoHelper{#regiondes实现///<summary>///Des默认密钥向量///</summary>publicstaticbyte[]DesIv={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};///<summary>
  • 标签:.net C# net 加密解密

.Net(c#)加密解密工具类:

/// <summary>
/// .Net加密解密帮助类
/// </summary>
public class NetCryptoHelper
{
    #region des实现

    /// <summary>
    /// Des默认密钥向量
    /// </summary>
    public static byte[] DesIv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    /// <summary>
    /// Des加解密钥必须8位
    /// </summary>
    public const string DesKey = "deskey8w";
    /// <summary>
    /// 获取Des8位密钥
    /// </summary>
    /// <param name="key">Des密钥字符串</param>
    /// <returns>Des8位密钥</returns>
    static byte[] GetDesKey(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentNullException("key", "Des密钥不能为空");
        }
        if (key.Length > 8)
        {
            key = key.Substring(0, 8);
        }
        if (key.Length < 8)
        {
            // 不足8补全
            key = key.PadRight(8, '0');
        }
        return Encoding.UTF8.GetBytes(key);
    }
    /// <summary>
    /// Des加密
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <param name="key">des密钥,长度必须8位</param>
    /// <param name="iv">密钥向量</param>
    /// <returns>加密后的字符串</returns>
    public static string EncryptDes(string source, string key, byte[] iv)
    {
        using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
        {
            byte[] rgbKeys = GetDesKey(key),
                rgbIvs = iv,
                inputByteArray = Encoding.UTF8.GetBytes(source);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, desProvider.CreateEncryptor(rgbKeys, rgbIvs), CryptoStreamMode.Write))
                {
                    cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
                    cryptoStream.FlushFinalBlock();
                    // 1.第一种
                    return Convert.ToBase64String(memoryStream.ToArray());

                    // 2.第二种
                    //StringBuilder result = new StringBuilder();
                    //foreach (byte b in memoryStream.ToArray())
                    //{
                    //    result.AppendFormat("{0:X2}", b);
                    //}
                    //return result.ToString();
                }
            }
        }
    }
    /// <summary>
    /// Des解密
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <param name="key">des密钥,长度必须8位</param>
    /// <param name="iv">密钥向量</param>
    /// <returns>解密后的字符串</returns>
    public static string DecryptDes(string source, string key, byte[] iv)
    {
        using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
        {
            byte[] rgbKeys = GetDesKey(key),
                rgbIvs = iv,
                inputByteArray = Convert.FromBase64String(source);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, desProvider.CreateDecryptor(rgbKeys, rgbIvs), CryptoStreamMode.Write))
                {
                    cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
                    cryptoStream.FlushFinalBlock();
                    return Encoding.UTF8.GetString(memoryStream.ToArray());
                }
            }
        }
    }

    #endregion

    #region aes实现

    /// <summary>
    /// Aes加解密钥必须32位
    /// </summary>
    public static string AesKey = "asekey32w";
    /// <summary>
    /// 获取Aes32位密钥
    /// </summary>
    /// <param name="key">Aes密钥字符串</param>
    /// <returns>Aes32位密钥</returns>
    static byte[] GetAesKey(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentNullException("key", "Aes密钥不能为空");
        }
        if (key.Length < 32)
        {
            // 不足32补全
            key = key.PadRight(32, '0');
        }
        if (key.Length > 32)
        {
            key = key.Substring(0, 32);
        }
        return Encoding.UTF8.GetBytes(key);
    }
    /// <summary>
    /// Aes加密
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <param name="key">aes密钥,长度必须32位</param>
    /// <returns>加密后的字符串</returns>
    public static string EncryptAes(string source, string key)
    {
        using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
        {
            aesProvider.Key = GetAesKey(key);
            aesProvider.Mode = CipherMode.ECB;
            aesProvider.Padding = PaddingMode.PKCS7;
            using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor())
            {
                byte[] inputBuffers = Encoding.UTF8.GetBytes(source);
                byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
                aesProvider.Clear();
                aesProvider.Dispose();
                return Convert.ToBase64String(results, 0, results.Length);
            }
        }
    }
    /// <summary>
    /// Aes解密
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <param name="key">aes密钥,长度必须32位</param>
    /// <returns>解密后的字符串</returns>
    public static string DecryptAes(string source, string key)
    {
        using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
        {
            aesProvider.Key = GetAesKey(key);
            aesProvider.Mode = CipherMode.ECB;
            aesProvider.Padding = PaddingMode.PKCS7;
            using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
            {
                byte[] inputBuffers = Convert.FromBase64String(source);
                byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
                aesProvider.Clear();
                return Encoding.UTF8.GetString(results);
            }
        }
    }

    #endregion
}

调试运行:

class Program
{
    static void Main(string[] args)
    {
        string plainText = "博客园",
            encryptString = NetCryptoHelper.EncryptDes(plainText, NetCryptoHelper.DesKey, NetCryptoHelper.DesIv);

        Console.WriteLine("des加密前的字符串:{0}", plainText);
        Console.WriteLine("des加密后的字符串:{0}", encryptString);
        Console.WriteLine("des解密后的字符串:{0}", NetCryptoHelper.DecryptDes(encryptString, NetCryptoHelper.DesKey, NetCryptoHelper.DesIv));

        Console.WriteLine("-----------分割线-----------");

        Console.WriteLine("aes加密前的字符串:{0}", plainText);
        encryptString = NetCryptoHelper.EncryptAes(plainText, NetCryptoHelper.AesKey);
        Console.WriteLine("aes加密后的字符串:{0}", encryptString);
        Console.WriteLine("aes解密后的字符串:{0}", NetCryptoHelper.DecryptAes(encryptString, NetCryptoHelper.AesKey));
        Console.ReadKey();
    }
}

发表评论
用户名: 匿名