C#文件加密方法汇总

本文实例汇总了C#文件加密方法。分享给大家供大家参考。具体实现方法如下:

1、AES加密类

代码如下:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utils
{
    /// <summary>
    /// AES加密解密
    /// </summary>
    public class AES
    {
        #region 加密
        #region 加密字符串
        /// <summary>
        /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="EncryptString">待加密密文</param>
        /// <param name="EncryptKey">加密密钥</param>
        public static string AESEncrypt(string EncryptString, string EncryptKey)
        {
            return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey));
        }
        #endregion

#region 加密字节数组
        /// <summary>
        /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="EncryptString">待加密密文</param>
        /// <param name="EncryptKey">加密密钥</param>
        public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey)
        {
            if (EncryptByte.Length == 0) { throw (new Exception("明文不得为空")); }
            if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
            byte[] m_strEncrypt;
            byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
            Rijndael m_AESProvider = Rijndael.Create();
            try
            {
                MemoryStream m_stream = new MemoryStream();
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(EncryptKey, m_salt);
                ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(32), m_btIV);
                CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
                m_csstream.Write(EncryptByte, 0, EncryptByte.Length);
                m_csstream.FlushFinalBlock();
                m_strEncrypt = m_stream.ToArray();
                m_stream.Close(); m_stream.Dispose();
                m_csstream.Close(); m_csstream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { m_AESProvider.Clear(); }
            return m_strEncrypt;
        }
        #endregion
        #endregion

#region 解密
        #region 解密字符串
        /// <summary>
        /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="DecryptString">待解密密文</param>
        /// <param name="DecryptKey">解密密钥</param>
        public static string AESDecrypt(string DecryptString, string DecryptKey)
        {
            return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString), DecryptKey));
        }
        #endregion

#region 解密字节数组
        /// <summary>
        /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="DecryptString">待解密密文</param>
        /// <param name="DecryptKey">解密密钥</param>
        public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey)
        {
            if (DecryptByte.Length == 0) { throw (new Exception("密文不得为空")); }
            if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
            byte[] m_strDecrypt;
            byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
            Rijndael m_AESProvider = Rijndael.Create();
            try
            {
                MemoryStream m_stream = new MemoryStream();
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(DecryptKey, m_salt);
                ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(32), m_btIV);
                CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
                m_csstream.Write(DecryptByte, 0, DecryptByte.Length);
                m_csstream.FlushFinalBlock();
                m_strDecrypt = m_stream.ToArray();
                m_stream.Close(); m_stream.Dispose();
                m_csstream.Close(); m_csstream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { m_AESProvider.Clear(); }
            return m_strDecrypt;
        }
        #endregion
        #endregion

}
}

2、文件加密类

代码如下:

using System.IO;
using System;

namespace Utils
{
    /// <summary>
    /// 文件加密类
    /// </summary>
    public class FileEncrypt
    {
        #region 变量
        /// <summary>
        /// 一次处理的明文字节数
        /// </summary>
        public static readonly int encryptSize = 10000000;
        /// <summary>
        /// 一次处理的密文字节数
        /// </summary>
        public static readonly int decryptSize = 10000016;
        #endregion

#region 加密文件
        /// <summary>
        /// 加密文件
        /// </summary>
        public static void EncryptFile(string path, string pwd, RefreshFileProgress refreshFileProgress)
        {
            try
            {
                if (File.Exists(path + ".temp")) File.Delete(path + ".temp");
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    if (fs.Length > 0)
                    {
                        using (FileStream fsnew = new FileStream(path + ".temp", FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            if (File.Exists(path + ".temp")) File.SetAttributes(path + ".temp", FileAttributes.Hidden);
                            int blockCount = ((int)fs.Length - 1) / encryptSize + 1;
                            for (int i = 0; i < blockCount; i++)
                            {
                                int size = encryptSize;
                                if (i == blockCount - 1) size = (int)(fs.Length - i * encryptSize);
                                byte[] bArr = new byte[size];
                                fs.Read(bArr, 0, size);
                                byte[] result = AES.AESEncrypt(bArr, pwd);
                                fsnew.Write(result, 0, result.Length);
                                fsnew.Flush();
                                refreshFileProgress(blockCount, i + 1); //更新进度
                            }
                            fsnew.Close();
                            fsnew.Dispose();
                        }
                        fs.Close();
                        fs.Dispose();
                        FileAttributes fileAttr = File.GetAttributes(path);
                        File.SetAttributes(path, FileAttributes.Archive);
                        File.Delete(path);
                        File.Move(path + ".temp", path);
                        File.SetAttributes(path, fileAttr);
                    }
                }
            }
            catch (Exception ex)
            {
                File.Delete(path + ".temp");
                throw ex;
            }
        }
        #endregion

#region 解密文件
        /// <summary>
        /// 解密文件
        /// </summary>
        public static void DecryptFile(string path, string pwd, RefreshFileProgress refreshFileProgress)
        {
            try
            {
                if (File.Exists(path + ".temp")) File.Delete(path + ".temp");
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    if (fs.Length > 0)
                    {
                        using (FileStream fsnew = new FileStream(path + ".temp", FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            if (File.Exists(path + ".temp")) File.SetAttributes(path + ".temp", FileAttributes.Hidden);
                            int blockCount = ((int)fs.Length - 1) / decryptSize + 1;
                            for (int i = 0; i < blockCount; i++)
                            {
                                int size = decryptSize;
                                if (i == blockCount - 1) size = (int)(fs.Length - i * decryptSize);
                                byte[] bArr = new byte[size];
                                fs.Read(bArr, 0, size);
                                byte[] result = AES.AESDecrypt(bArr, pwd);
                                fsnew.Write(result, 0, result.Length);
                                fsnew.Flush();
                                refreshFileProgress(blockCount, i + 1); //更新进度
                            }
                            fsnew.Close();
                            fsnew.Dispose();
                        }
                        fs.Close();
                        fs.Dispose();
                        FileAttributes fileAttr = File.GetAttributes(path);
                        File.SetAttributes(path, FileAttributes.Archive);
                        File.Delete(path);
                        File.Move(path + ".temp", path);
                        File.SetAttributes(path, fileAttr);
                    }
                }
            }
            catch (Exception ex)
            {
                File.Delete(path + ".temp");
                throw ex;
            }
        }
        #endregion

}

/// <summary>
    /// 更新文件加密进度
    /// </summary>
    public delegate void RefreshFileProgress(int max, int value);

}

3、文件夹加密类

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Utils;

namespace EncryptFile.Utils
{
    /// <summary>
    /// 文件夹加密类
    /// </summary>
    public class DirectoryEncrypt
    {
        #region 加密文件夹及其子文件夹中的所有文件
        /// <summary>
        /// 加密文件夹及其子文件夹中的所有文件
        /// </summary>
        public static void EncryptDirectory(string dirPath, string pwd, RefreshDirProgress refreshDirProgress, RefreshFileProgress refreshFileProgress)
        {
            string[] filePaths = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories);
            for (int i = 0; i < filePaths.Length; i++)
            {
                FileEncrypt.EncryptFile(filePaths[i], pwd, refreshFileProgress);
                refreshDirProgress(filePaths.Length, i + 1);
            }
        }
        #endregion

#region 解密文件夹及其子文件夹中的所有文件
        /// <summary>
        /// 解密文件夹及其子文件夹中的所有文件
        /// </summary>
        public static void DecryptDirectory(string dirPath, string pwd, RefreshDirProgress refreshDirProgress, RefreshFileProgress refreshFileProgress)
        {
            string[] filePaths = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories);
            for (int i = 0; i < filePaths.Length; i++)
            {
                FileEncrypt.DecryptFile(filePaths[i], pwd, refreshFileProgress);
                refreshDirProgress(filePaths.Length, i + 1);
            }
        }
        #endregion

}

/// <summary>
    /// 更新文件夹加密进度
    /// </summary>
    public delegate void RefreshDirProgress(int max, int value);

}

4、跨线程访问控制委托

代码如下:

using System;
using System.Windows.Forms;

namespace Utils
{
    /// <summary>
    /// 跨线程访问控件的委托
    /// </summary>
    public delegate void InvokeDelegate();

/// <summary>
    /// 跨线程访问控件类
    /// </summary>
    public class InvokeUtil
    {
        /// <summary>
        /// 跨线程访问控件
        /// </summary>
        /// <param name="ctrl">Form对象</param>
        /// <param name="de">委托</param>
        public static void Invoke(Control ctrl, Delegate de)
        {
            if (ctrl.IsHandleCreated)
            {
                ctrl.BeginInvoke(de);
            }
        }
    }
}

5、Form1.cs文件

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Utils;
using System.Threading;
using EncryptFile.Utils;

namespace EncryptFile
{
    public partial class Form1 : Form
    {
        #region 变量
        /// <summary>
        /// 一次处理的明文字节数
        /// </summary>
        public static int encryptSize = 10000000;
        /// <summary>
        /// 一次处理的密文字节数
        /// </summary>
        public static int decryptSize = 10000016;
        #endregion

#region 构造函数
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

#region 加密文件
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            #region 验证
            if (txtPwd.Text == "")
            {
                MessageBox.Show("密码不能为空", "提示");
                return;
            }

if (txtPwdCfm.Text == "")
            {
                MessageBox.Show("确认密码不能为空", "提示");
                return;
            }

if (txtPwdCfm.Text != txtPwd.Text)
            {
                MessageBox.Show("两次输入的密码不相同", "提示");
                return;
            }
            #endregion

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
                {
                    try
                    {
                        InvokeDelegate invokeDelegate = delegate()
                        {
                            pbFile.Value = 0;
                            lblProgressFile.Text = "0%";
                            pbDir.Visible = false;
                            lblProgressDir.Visible = false;
                            pbFile.Visible = false;
                            lblProgressFile.Visible = false;
                            lblShowPath.Text = "文件:" + openFileDialog1.FileName;
                            lblShowPath.Visible = true;
                            DisableBtns();
                        };
                        InvokeUtil.Invoke(this, invokeDelegate);
                        DateTime t1 = DateTime.Now;
                        FileEncrypt.EncryptFile(openFileDialog1.FileName, txtPwd.Text, RefreshFileProgress);
                        DateTime t2 = DateTime.Now;
                        string t = t2.Subtract(t1).TotalSeconds.ToString("0.00");
                        if (MessageBox.Show("加密成功,耗时" + t + "秒", "提示") == DialogResult.OK)
                        {
                            invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("加密失败:" + ex.Message, "提示") == DialogResult.OK)
                        {
                            InvokeDelegate invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                }));
                thread.Start();
            }
        }
        #endregion

#region 解密文件
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            #region 验证
            if (txtPwd.Text == "")
            {
                MessageBox.Show("密码不能为空", "提示");
                return;
            }
            #endregion

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
                {
                    try
                    {
                        InvokeDelegate invokeDelegate = delegate()
                        {
                            pbFile.Value = 0;
                            lblProgressFile.Text = "0%";
                            pbDir.Visible = false;
                            lblProgressDir.Visible = false;
                            pbFile.Visible = false;
                            lblProgressFile.Visible = false;
                            lblShowPath.Text = "文件:" + openFileDialog1.FileName;
                            lblShowPath.Visible = true;
                            DisableBtns();
                        };
                        InvokeUtil.Invoke(this, invokeDelegate);
                        DateTime t1 = DateTime.Now;
                        FileEncrypt.DecryptFile(openFileDialog1.FileName, txtPwd.Text, RefreshFileProgress);
                        DateTime t2 = DateTime.Now;
                        string t = t2.Subtract(t1).TotalSeconds.ToString("0.00");
                        if (MessageBox.Show("解密成功,耗时" + t + "秒", "提示") == DialogResult.OK)
                        {
                            invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("解密失败:" + ex.Message, "提示") == DialogResult.OK)
                        {
                            InvokeDelegate invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                }));
                thread.Start();
            }
        }
        #endregion

#region 文件夹加密
        private void btnEncryptDir_Click(object sender, EventArgs e)
        {
            #region 验证
            if (txtPwd.Text == "")
            {
                MessageBox.Show("密码不能为空", "提示");
                return;
            }

if (txtPwdCfm.Text == "")
            {
                MessageBox.Show("确认密码不能为空", "提示");
                return;
            }

if (txtPwdCfm.Text != txtPwd.Text)
            {
                MessageBox.Show("两次输入的密码不相同", "提示");
                return;
            }
            #endregion

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (MessageBox.Show(string.Format("确定加密文件夹{0}?", folderBrowserDialog1.SelectedPath),
                    "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    return;
                }

Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
                {
                    try
                    {
                        InvokeDelegate invokeDelegate = delegate()
                        {
                            pbDir.Value = 0;
                            lblProgressDir.Text = "0%";
                            pbFile.Value = 0;
                            lblProgressFile.Text = "0%";
                            pbDir.Visible = true;
                            lblProgressDir.Visible = true;
                            pbFile.Visible = false;
                            lblProgressFile.Visible = false;
                            lblShowPath.Text = "文件夹:" + folderBrowserDialog1.SelectedPath;
                            lblShowPath.Visible = true;
                            DisableBtns();
                        };
                        InvokeUtil.Invoke(this, invokeDelegate);
                        DateTime t1 = DateTime.Now;
                        DirectoryEncrypt.EncryptDirectory(folderBrowserDialog1.SelectedPath, txtPwd.Text, RefreshDirProgress, RefreshFileProgress);
                        DateTime t2 = DateTime.Now;
                        string t = t2.Subtract(t1).TotalSeconds.ToString("0.00");
                        if (MessageBox.Show("加密成功,耗时" + t + "秒", "提示") == DialogResult.OK)
                        {
                            invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("加密失败:" + ex.Message, "提示") == DialogResult.OK)
                        {
                            InvokeDelegate invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                }));
                thread.Start();
            }
        }
        #endregion

#region 文件夹解密
        private void btnDecryptDir_Click(object sender, EventArgs e)
        {
            #region 验证
            if (txtPwd.Text == "")
            {
                MessageBox.Show("密码不能为空", "提示");
                return;
            }
            #endregion

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (MessageBox.Show(string.Format("确定解密文件夹{0}?", folderBrowserDialog1.SelectedPath),
                    "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    return;
                }

Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
                {
                    try
                    {
                        InvokeDelegate invokeDelegate = delegate()
                        {
                            pbDir.Value = 0;
                            lblProgressDir.Text = "0%";
                            pbFile.Value = 0;
                            lblProgressFile.Text = "0%";
                            pbDir.Visible = true;
                            lblProgressFile.Visible = true;
                            pbFile.Visible = false;
                            lblProgressFile.Visible = false;
                            lblShowPath.Text = "文件夹:" + folderBrowserDialog1.SelectedPath;
                            lblShowPath.Visible = true;
                            DisableBtns();
                        };
                        InvokeUtil.Invoke(this, invokeDelegate);
                        DateTime t1 = DateTime.Now;
                        DirectoryEncrypt.DecryptDirectory(folderBrowserDialog1.SelectedPath, txtPwd.Text, RefreshDirProgress, RefreshFileProgress);
                        DateTime t2 = DateTime.Now;
                        string t = t2.Subtract(t1).TotalSeconds.ToString("0.00");
                        if (MessageBox.Show("解密成功,耗时" + t + "秒", "提示") == DialogResult.OK)
                        {
                            invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("解密失败:" + ex.Message, "提示") == DialogResult.OK)
                        {
                            InvokeDelegate invokeDelegate = delegate()
                            {
                                EnableBtns();
                            };
                            InvokeUtil.Invoke(this, invokeDelegate);
                        }
                    }
                }));
                thread.Start();
            }
        }
        #endregion

#region 更新文件加密进度
        /// <summary>
        /// 更新文件加密进度
        /// </summary>
        public void RefreshFileProgress(int max, int value)
        {
            InvokeDelegate invokeDelegate = delegate()
            {
                if (max > 1)
                {
                    pbFile.Visible = true;
                    lblProgressFile.Visible = true;
                }
                else
                {
                    pbFile.Visible = false;
                    lblProgressFile.Visible = false;
                }
                pbFile.Maximum = max;
                pbFile.Value = value;
                lblProgressFile.Text = value * 100 / max + "%";
            };
            InvokeUtil.Invoke(this, invokeDelegate);
        }
        #endregion

#region 更新文件夹加密进度
        /// <summary>
        /// 更新文件夹加密进度
        /// </summary>
        public void RefreshDirProgress(int max, int value)
        {
            InvokeDelegate invokeDelegate = delegate()
            {
                pbDir.Maximum = max;
                pbDir.Value = value;
                lblProgressDir.Text = value * 100 / max + "%";
            };
            InvokeUtil.Invoke(this, invokeDelegate);
        }
        #endregion

#region 显示密码
        private void cbxShowPwd_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxShowPwd.Checked)
            {
                txtPwd.PasswordChar = default(char);
                txtPwdCfm.PasswordChar = default(char);
            }
            else
            {
                txtPwd.PasswordChar = '*';
                txtPwdCfm.PasswordChar = '*';
            }
        }
        #endregion

#region 关闭窗体事件
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (progressPanel.Visible)
            {
                MessageBox.Show("正在处理文件,请等待…", "提示");
                e.Cancel = true;
            }
        }
        #endregion

#region 控制按钮状态
        /// <summary>
        /// 禁用按钮
        /// </summary>
        public void DisableBtns()
        {
            progressPanel.Visible = true;
            btnEncrypt.Enabled = false;
            btnDecrypt.Enabled = false;
            btnEncryptDir.Enabled = false;
            btnDecryptDir.Enabled = false;
        }
        /// <summary>
        /// 启用按钮
        /// </summary>
        public void EnableBtns()
        {
            lblShowPath.Visible = false;
            progressPanel.Visible = false;
            btnEncrypt.Enabled = true;
            btnDecrypt.Enabled = true;
            btnEncryptDir.Enabled = true;
            btnDecryptDir.Enabled = true;
        }
        #endregion

}
}

完整实例代码点击此处本站下载。

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • 基于私钥加密公钥解密的RSA算法C#实现方法

    本文实例讲述了基于私钥加密公钥解密的RSA算法C#实现方法,是一种应用十分广泛的算法.分享给大家供大家参考之用.具体方法如下: 一.概述 RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一.RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价. RSA的安全性依赖于大数分解.公钥和私钥都是两个大素数( 大于 1

  • c#多种加解密示例(md5加密解密)

    复制代码 代码如下: using System;using System.Collections.Generic;using System.Globalization;using System.IO;using System.Security.Cryptography;using System.Text; /// <summary>/// 加解密/// </summary>public static class CryptHelper{    #region RSA加密 /// &

  • C#的3DES加密解密算法实例代码

    C#类如下: 复制代码 代码如下: using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;using System.IO; namespace ConsoleApplication1{    /// <summary>    /// 加解密类    /// </summary>    public class EncryptHelper  

  • C#编写的Base64加密和解密类

    这个C#类是一个基于Base64的加密和解密类,用户可以可以使用默认的秘钥进行加密.解密,也可以自己设定秘钥进行加密和解密,非常实用 代码一:非常精简的代码 /// <summary> /// Base64加密,解密方法 /// </summary> /// <paramname="s">输入字符串</param> /// <paramname="c">true-加密,false-解密</param&

  • 基于C#对用户密码使用MD5加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1)16位的MD5加密 /// <summary> /// 16位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns&

  • C#实现对AES加密和解密的方法

    AES简介 AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范.它被预期能成为人们公认的加密包括金融.电信和政府数字信息的方法. AES 是一个新的可以用于保护电子数据的加密算法.明确地说,AES 是一个迭代的.对称密钥分组的密码,它可以使用128.192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据.与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据.通过分组密码返回的加密数据

  • C#加密解密文件小工具实现代码

    DebugLZQ在网上搜索相关文件加密的程序,发现给出的基本都是针对"字符创"."文本"的加密与解密.对视频文件.图片等一般文件的加密解密程序少之又少,故写下此文,实现一个对一般文件进行加密的小工具. 程序的主要功能是:用户通过文件选择框选择要加密的文件->输入密码进行加密:选择加密后的文件,输入密码进行解密. 程序的主界面如下: 三个按钮的Click事件处理程序如下: 复制代码 代码如下: private void btnSelectFile_Click(o

  • c# rsa注册实现加密文字

    复制代码 代码如下: RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); private void btencoding_Click(object sender, EventArgs e){    if (mbox.Text == "")    {        MessageBox.Show("加密文字信息不能为空!");        return;    }    if (publick

  • C#常用字符串加密解密方法封装代码

    复制代码 代码如下: //方法一//须添加对System.Web的引用//using System.Web.Security;/// <summary>/// SHA1加密字符串/// </summary>/// <param name="source">源字符串</param>/// <returns>加密后的字符串</returns>public string SHA1(string source){    r

  • C# RSA分段加解密实现方法详解

    本文实例讲述了C# RSA分段加解密实现方法.分享给大家供大家参考,具体如下: RSA加解密: 1024位的证书,加密时最大支持117个字节,解密时为128: 2048位的证书,加密时最大支持245个字节,解密时为256. 加密时支持的最大字节数:证书位数/8 -11(比如:2048位的证书,支持的最大加密字节数:2048/8 - 11 = 245) .NET中的RSA加密算法为了提高安全性,在待加密数据前要添加一些随机数,因此,使用.NET中的RSA加密算法一次最多加密117字节数据(多于11

  • C#实现的AES加密解密完整实例

    本文实例讲述了C#实现的AES加密解密.分享给大家供大家参考,具体如下: /****************************************************************** * 创建人:HTL * 说明:C# AES加密解密 *******************************************************************/ using System; using System.Security.Cryptography;

  • c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法源代码

    复制代码 代码如下: using System; using System.IO; using System.Data; using System.Text; using System.Diagnostics; using System.Security; using System.Security.Cryptography; /**//* * .Net框架由于拥有CLR提供的丰富库支持,只需很少的代码即可实现先前使用C等旧式语言很难实现的加密算法.本类实现一些常用机密算法,供参考.其中MD5算

  • C#自定义RSA加密解密及RSA签名和验证类实例

    本文实例讲述了C#自定义RSA加密解密及RSA签名和验证类.分享给大家供大家参考.具体分析如下: 这个C#类自定义RSA加密解密及RSA签名和验证,包含了RSA加密.解密及签名所需的相关函数,带有详细的注释说明. using System; using System.Text; using System.Security.Cryptography; namespace DotNet.Utilities { /// <summary> /// RSA加密解密及RSA签名和验证 /// </

随机推荐