• 开发通用类 字符串加密、解密篇


    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static string EncryptByMd5(string source)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");
    }
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="source">待加密字符串</param>
    /// <param name="key">密钥</param>
    /// <returns></returns>
    public static string EncryptByDes(string source, string key)
    {
        using (var des = new DESCryptoServiceProvider())
        {
            byte[] sourceArray = Encoding.UTF8.GetBytes(source);
            des.Key = Encoding.ASCII.GetBytes(key);
            des.IV = Encoding.ASCII.GetBytes(key);
            var memoryStream = new MemoryStream();
            using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cryptoStream.Write(sourceArray, 0, sourceArray.Length);
                cryptoStream.FlushFinalBlock();
                cryptoStream.Close();
            }
            string str = Convert.ToBase64String(memoryStream.ToArray());
            memoryStream.Close();
            return str;
        }
    }
    
    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="source">加密字符串</param>
    /// <param name="key">密钥</param>
    /// <returns></returns>
    public static string DecryptByDes(string source, string key)
    {
        byte[] sourceArray = Convert.FromBase64String(source);
        using (var des = new DESCryptoServiceProvider())
        {
            des.Key = Encoding.ASCII.GetBytes(key);
            des.IV = Encoding.ASCII.GetBytes(key);
            var memoryStream = new MemoryStream();
            using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cryptoStream.Write(sourceArray, 0, sourceArray.Length);
                cryptoStream.FlushFinalBlock();
                cryptoStream.Close();
            }
            string str = Encoding.UTF8.GetString(memoryStream.ToArray());
            memoryStream.Close();
            return str;
        }
    }
    /// <summary>
    /// 3DES加密
    /// key1: qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3
    /// key2: qcDY6X+aPLw=
    /// </summary>
    /// <param name="source">待加密字符串</param>
    /// <param name="key1">密钥</param>
    /// <param name="key2">矢量,矢量可以为空</param>
    /// <returns></returns>
    public static string EncryptBy3Des(string source, string key1, string key2)
    {
        using (SymmetricAlgorithm symmetricAlgorithm = new TripleDESCryptoServiceProvider())
        {
            symmetricAlgorithm.Key = Convert.FromBase64String(key1);
            symmetricAlgorithm.IV = Convert.FromBase64String(key2);
            symmetricAlgorithm.Mode = CipherMode.ECB;
            symmetricAlgorithm.Padding = PaddingMode.PKCS7;
            ICryptoTransform iCryptoTransform = symmetricAlgorithm.CreateEncryptor(symmetricAlgorithm.Key,
                                                                                    symmetricAlgorithm.IV);
            byte[] sourceArray = Encoding.UTF8.GetBytes(source);
            var memoryStream = new MemoryStream();
            using (var cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Write))
            {
                cryptoStream.Write(sourceArray, 0, sourceArray.Length);
                cryptoStream.FlushFinalBlock();
                cryptoStream.Close();
            }
            string str = Convert.ToBase64String(memoryStream.ToArray());
            memoryStream.Close();
            return str;
        }
    }
    
    /// <summary>
    /// 3DES解密
    /// </summary>
    /// <param name="source">加密字符串</param>
    /// <param name="key1">密钥 </param>
    /// <param name="key2">矢量,矢量可以为空</param>
    /// <returns></returns>
    public static string DecryptBy3Des(string source, string key1, string key2)
    {
        using (SymmetricAlgorithm symmetricAlgorithm = new TripleDESCryptoServiceProvider())
        {
            symmetricAlgorithm.Key = Convert.FromBase64String(key1);
            symmetricAlgorithm.IV = Convert.FromBase64String(key2);
            symmetricAlgorithm.Mode = CipherMode.ECB;
            symmetricAlgorithm.Padding = PaddingMode.PKCS7;
            ICryptoTransform iCryptoTransform = symmetricAlgorithm.CreateDecryptor(symmetricAlgorithm.Key,
                                                                                    symmetricAlgorithm.IV);
            byte[] sourceArray = Convert.FromBase64String(source);
            var memoryStream = new MemoryStream();
            using (var cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Write))
            {
                cryptoStream.Write(sourceArray, 0, sourceArray.Length);
                cryptoStream.FlushFinalBlock();
                cryptoStream.Close();
            }
            string str = Encoding.UTF8.GetString(memoryStream.ToArray());
            memoryStream.Close();
            return str;
        }
    }
  • 相关阅读:
    观察者模式学习--使用jdk的工具类简单实现
    观察者模式的初始学习--自己实现
    反射 reflect 初始学习
    eclipse java 空心J文件的回复
    linux 命令 more
    spring 3 的 @value的使用
    linux rm 命令
    linux log find 查询
    Resource is out of sync with the file system
    JavaScript(七)数组
  • 原文地址:https://www.cnblogs.com/judgelee/p/2631349.html
Copyright © 2020-2023  润新知