13
2012
08

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


/// <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;
    }
}

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。