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

这是一个C#的静态类,包含了计算文件和字符串的MD5值,以及使用AES进行加解密的功能。类中定义了GetFileMD5用于计算文件的MD5,GetMD5用于计算字符串的MD5,AES_Encrypt和AES_Decrypt分别用于AES加密和解密操作。加密和解密方法使用了RijndaelManaged算法,支持ECB模式和PKCS7填充。
摘要由CSDN通过智能技术生成

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

 

微信打赏支持
微信打赏支持

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值