[C#]加密解密 MD5、AES

/// 
/// MD5函数
/// 
/// 原始字符串
/// MD5结果
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;
}

/// 
/// AES加密
/// 
/// 
/// 
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;
    }
}

/// 
/// AES解密
/// 
/// 
/// 
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)