首页 > Windows开发 > 详细

[C#]加密解密 MD5、AES

时间:2019-07-09 09:04:33      阅读:107      评论:0      收藏:0      [点我收藏+]
/// <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

原文:https://www.cnblogs.com/z5337/p/11155137.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!