C#中使用MD5文件、MD5字串、AES加解密的一个通用类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Security.Cryptography;

namespace DangshaojunLib
{
   public static class MD5
    {
        public static string GetFileMD5(string filename)
        {
            try
            {
                FileStream get_file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash_byte = get_md5.ComputeHash(get_file);
                string resule = System.BitConverter.ToString(hash_byte);
                resule = resule.Replace("-", "");
                //Clipboard.SetText(resule);
                return resule;
            }
            catch (Exception e)
            {
                return null;// e.ToString();
           }
 
        }
        public static string GetMD5(string str)
        {
            try
            {
                //FileStream get_file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash_byte = get_md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
                string resule = System.BitConverter.ToString(hash_byte);
                resule = resule.Replace("-", "");

                return resule;
            }
            catch (Exception e)
            {
                return null;// e.ToString();
            }

        }
        public static string AES_Encrypt(string toEncrypt, string Key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key);//"12345678901234567890123456789012");
            byte[] toEncryptArray = UTF8Encoding.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);
        }

        public static string AES_Decrypt(string toDecrypt, string Key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key);//"12345678901234567890123456789012");
            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);
        }
    }
}

 

C#中使用MD5文件、MD5字串、AES加解密的一个通用类
微信打赏支持