[C#]加密解密 MD5、AES

/// <summary>
/// MD5函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>MD5结果</returns>
public static string MD5(string str)
{
    byte[] b = Encoding.UTF8.GetBytes(str);
    b = new MD5CryptoServiceProvider().ComputeHash(b);
    string ret = string.Empty;
    for (int i = 0; i < b.Length; i++)
    {
        ret += b[i].ToString("x").PadLeft(2, 0);
    }
    return ret;
}

/// <summary>
/// AES加密
/// </summary>
/// <param name="toEncrypt"></param>
/// <returns></returns>
public static string AesEncrypt(string toEncrypt)
{
    if (string.IsNullOrEmpty(toEncrypt))
    {
        return string.Empty;
    }
    try
    {
        byte[] keyArray = Encoding.UTF8.GetBytes(@"7BF45189A0ED4BD6BE7936EFD11169E7");

        byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = rDel.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
    catch
    {
        return string.Empty;
    }
}

/// <summary>
/// AES解密
/// </summary>
/// <param name="toDecrypt"></param>
/// <returns></returns>
public static string AesDecrypt(string toDecrypt)
{
    if (string.IsNullOrEmpty(toDecrypt))
    {
        return string.Empty;
    }
    try
    {

        byte[] keyArray = Encoding.UTF8.GetBytes(@"7BF45189A0ED4BD6BE7936EFD11169E7");

        byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;


        ICryptoTransform cTransform = rDel.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return UTF8Encoding.UTF8.GetString(resultArray);
    }
    catch
    {
        return string.Empty;
    }
}

 

[C#]加密解密 MD5、AES

上一篇:iOS中画矩形的几种方法总结


下一篇:win10蓝屏1