form1.cs

来自「用des算法与ras算法实现的文件加密选择器的实现与控制。」· CS 代码 · 共 209 行

CS
209
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace EncryptSelector
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            init();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 做初始化工作,比如加载备用算法等。
        /// </summary>
        private void init()
        {
            this.radioButton1.Checked = true;

            listBox1.Items.Add("MyEncrypt");
            this.listBox1.Items.Add("RSA");
            //this.listBox1.Items.Add("DES");
            //this.listBox1.Items.Add("MD5");
            
        }

        /// <summary>
        /// 弹出文件选择器,以便选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">触发该动作的事件源</param>
        private void button3_Click(object sender, EventArgs e)
        {
            label4.Visible = false;
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.Filter = "*.*|*.*";
            this.openFileDialog1.ShowDialog();
            string filePath = this.openFileDialog1.FileName;
            //没有选文件
            if (filePath == null || filePath.Trim().Length == 0)
            {
                this.label4.Text = "you must select at least one file, please!";
                return;
            }
            this.textBox1.Text = filePath;

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 开始加密或解密,将加密前和加密后的内容予以展示,以便查看和对比加密结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            
            if (this.listBox1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请选择加密或解密算法.");
                return;
            }
            string file = this.textBox1.Text;
            //判断是否以已经选择了一定的文件,没有选择的话需要提示选择
            if (file == null || file.Trim().Length == 0)
            {
                MessageBox.Show("您还没有选择文件,请选择要加密或解密的文件");
                return;
            }
            //获取是加密还是解密
            bool encrypt = this.radioButton1.Checked ? true : false;
            label4.Visible = true;
            label4.Text = "正在"+(encrypt?"加密":"解密");
            
            //获得要加密或解密的算法
            for (int i = 0; i < listBox1.SelectedItems.Count; i++)
            {
                //得到算法
                string alg = listBox1.SelectedItems[i].ToString();
                switch (alg)
                {
                    case "RSA":
                        EncryptByRsa(encrypt, file);
                        break;
                    case "DES":
                        EncryptByDes(encrypt, file);
                        break;
                    case "MyEncrypt":
                        EncryptByAsciiAddSomeValue(encrypt, file);
                        break;
                    default:

                        break;
                
                }
            }
        }
        private void EncryptByAsciiAddSomeValue(bool enctypt, string file)
        {
            if (file == null || file.Trim().Length == 0)
            {
                MessageBox.Show("选择的文件错误");
                return;
            }
            string fileout="";
            string filesubfix=".enc";
            if (enctypt)
            {
                fileout = file + filesubfix;
            }
            else
            {
                fileout = file.Substring(0, file.Trim().Length - filesubfix.Length);
            }
            FileStream fin = new FileStream(file, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(fileout, FileMode.OpenOrCreate, FileAccess.Write);
             fout.SetLength(0);
             byte[] bin = new byte[1000];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;


             while (rdlen < totlen)
             {
                    len = fin.Read(bin, 0, 1000);
                    byte[] data = bin;
                    for (int i = 0; i < len; i++)
                    {
                        int ch = (int)data[i];
                        data[i] =(byte)(enctypt?ch+ 1:ch-1);
                    }
                    fout.Write(data, 0, len);
                    rdlen = rdlen + len;
             }
             fin.Close();
            fout.Flush();
            fout.Close();

            File.Delete(file);
            File.Move(fileout, file);
            string filesource = file;
            file = file.Substring(file.LastIndexOf("\\")+1);
            fileout = fileout.Substring(filesource.LastIndexOf("\\"));
            string msg = "文件";
            msg+=enctypt?"加密":"解密";
            msg += "成功:" + file;
            this.label4.Text = msg;
            

        }
        /// <summary>
        /// 具体的一个DES算法的实现
        /// </summary>
        /// <param name="enctypt"></param>
        /// <param name="file"></param>
        private void EncryptByDes(bool enctypt, string file)
        {
        }
        /// <summary>
        /// 具体的一个RSA算法的实现
        /// </summary>
        /// <param name="enctypt"></param>
        /// <param name="file"></param>
        private void EncryptByRsa(bool enctypt,string file)
        {
            RSA_ rsa2 = new RSA_();

            string sFileOut=enctypt?"enc_":"des_";
            sFileOut=sFileOut+file.Substring(file.LastIndexOf("\\")+1);
            sFileOut = file.Substring(0, file.LastIndexOf("\\")+1)+sFileOut;
            if (enctypt)
            {
                rsa2.Encrypt(file, sFileOut, "<a>65537</a>");
            }
            else
            {
                string privateKey = "<RSAKeyValue><Modulus>65537</Modulus></RSAKeyValue>";
                rsa2.Decrypt(file, sFileOut, privateKey/*"<a>65537</a>"*/);
            }
            label4.Text = (enctypt ? "加密" : "解密") + "成功!";
        }
        
    }
}

⌨️ 快捷键说明

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