⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 文件加密解密算法
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

namespace FileEncrypt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.EncryptRdBtn.Checked = true;
            this.DecryptRdBtn.Checked = false;
            this.EncryptOrDecryptLbl.Text = "公钥文件";
            this.EncryptOrDecryptBtn.Text = "加密选择文件";
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        private void PublicKeyFileViewBtn_Click(object sender, EventArgs e)
        {
            FolderDialog folderDlg = new FolderDialog();
            if (folderDlg.DisplayDialog() == DialogResult.OK)
            {
                string path=folderDlg.Path;
                this.PublicKeyFileEdt.Text = path;
                int len=path.Length;
                if (path[len - 1] == '\\')
                {
                    this.PublicKeyFileEdt.Text += "publicKey.xml";
                }
                else
                {
                    this.PublicKeyFileEdt.Text += "\\publicKey.xml";
                }
            }
        }

        private void PrivateKeyFileViewBtn_Click(object sender, EventArgs e)
        {
            FolderDialog folderDlg = new FolderDialog();
            if (folderDlg.DisplayDialog() == DialogResult.OK)
            {
                string path = folderDlg.Path;
                this.PrivateKeyFileEdt.Text = path;
                int len = path.Length;
                if (path[len - 1] == '\\')
                {
                    this.PrivateKeyFileEdt.Text += "privateKey.xml";
                }
                else
                {
                    this.PrivateKeyFileEdt.Text += "\\privateKey.xml";
                }
            }
        }

        private void GenKeysBtn_Click(object sender, EventArgs e)
        {
            if (this.PublicKeyFileEdt.Text == "" || this.PrivateKeyFileEdt.Text == "")
            {
                MessageBox.Show("请先选择存储秘钥的文件所在的路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            RsaEncryptAndDecrypt rsaEncryptAndDecrypt = new RsaEncryptAndDecrypt();

            StreamWriter sw01 = new StreamWriter(this.PublicKeyFileEdt.Text);
            sw01.Write(rsaEncryptAndDecrypt.GetPublicKey());
            sw01.Close();
            StreamWriter sw02 = new StreamWriter(this.PrivateKeyFileEdt.Text);
            sw02.Write(rsaEncryptAndDecrypt.GetPrivateKey());
            sw02.Close();

            MessageBox.Show("成功生成秘钥并存储到指定文件中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void SourceFileViewBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg=new OpenFileDialog();
			openFileDlg.Title="选择文件";
			openFileDlg.Filter="所有文件 (*.*)|*.*";
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                string sourceFilePath = openFileDlg.FileName;
                this.SourceFilePathEdt.Text = sourceFilePath;

                string destFilePath = "";
                int len = sourceFilePath.Length;
                int ind01 = len-1;
                int i=0;
                while (sourceFilePath[ind01] != '.')
                {
                    ind01--;
                }
                for (i = 0; i < ind01; i++)
                {
                    destFilePath += sourceFilePath[i].ToString();
                }
                destFilePath += "_dest";
                for (i = ind01; i < len; i++)
                {
                    destFilePath += sourceFilePath[i].ToString();
                }
                this.DestFilePathEdt.Text=destFilePath;
            }

        }

        private void KeyFileViewBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Title = "选择文件";
            openFileDlg.Filter = "所有文件 (*.*)|*.*";
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                this.KeyFilePathEdt.Text = openFileDlg.FileName;
            }
        }

        private void EncryptOrDecryptBtn_Click(object sender, EventArgs e)
        {
            if (this.SourceFilePathEdt.Text.Length == 0 || this.DestFilePathEdt.Text.Length == 0 || this.KeyFilePathEdt.Text.Length ==0)
            {
                MessageBox.Show("请选择/输入需要的文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            RsaEncryptAndDecrypt rsaEncryptAndDecrypt = new RsaEncryptAndDecrypt();

            if (this.EncryptRdBtn.Checked == true)
            {
                try
                {
                    rsaEncryptAndDecrypt.EncrptyFile(this.SourceFilePathEdt.Text, this.DestFilePathEdt.Text, this.KeyFilePathEdt.Text);
                    MessageBox.Show("加密选择的文件成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("加密选择的文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                try
                {
                    rsaEncryptAndDecrypt.DecryptFile(this.SourceFilePathEdt.Text, this.DestFilePathEdt.Text, this.KeyFilePathEdt.Text);
                    MessageBox.Show("解密选择的文件成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("解密选择的文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void DecryptRdBtn_CheckedChanged(object sender, EventArgs e)
        {
            if (this.DecryptRdBtn.Checked == true)
            {
                this.EncryptOrDecryptLbl.Text = "私钥文件";
                this.EncryptOrDecryptBtn.Text = "解密选择文件";
            }
        }

        private void EncryptRdBtn_CheckedChanged(object sender, EventArgs e)
        {
            if (this.EncryptRdBtn.Checked == true)
            {
                this.EncryptOrDecryptLbl.Text = "公钥文件";
                this.EncryptOrDecryptBtn.Text = "加密选择文件";
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -