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

📄 form1.cs

📁 DES加密软件 自己写的 还不错的 密码学
💻 CS
📖 第 1 页 / 共 2 页
字号:
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 DES
{
    public partial class Form1 : Form
    {
        static String inFileName = null;
        static String outFileName;
        static UInt64 userkey=0 ,userkey2=0,userkey3= 0;
        static UInt64 mi;
        static UInt64 co;
        static UInt64[] key = new UInt64[17];
        static UInt64[] c = new UInt64[17];
        static UInt64[] d = new UInt64[17];
        static UInt64[] l = new UInt64[17];
        static UInt64[] r = new UInt64[17];
        static int[] c0 ={  57,49,41,33,25,17, 9,
                             1,58,50,42,34,26,18,
                            10, 2,59,51,43,35,27,
                            19,11, 3,60,52,44,36};
        static int[] d0 ={  63,55,47,39,31,23,15,
                             7,62,54,46,38,30,22,
                            14, 6,61,53,45,37,29,
                            21,13, 5,28,20,12, 4};
        static int[] cd ={  14,17,11,24, 1, 5,
                             3,28,15, 6,21,10,
                            23,19,12, 4,26, 8,
                            16, 7,27,20,13, 2,
                            41,52,31,37,47,55,
                            30,40,51,45,33,48,
                            44,49,39,56,34,53,
                            46,42,50,36,29,32};
        static int[] ip ={  58,50,42,34,26,18,10, 2,
                            60,52,44,36,28,20,12, 4,
                            62,54,46,38,30,22,14, 6,
                            64,56,48,40,32,24,16, 8,
                            57,49,41,33,25,17, 9, 1,
                            59,51,43,35,27,19,11, 3,
                            61,53,45,37,29,21,13, 5,
                            63,55,47,39,31,23,15, 7};
        static int[] ip1 ={ 40, 8,48,16,56,24,64,32,
                            39, 7,47,15,55,23,63,31,
                            38, 6,46,14,54,22,62,30,
                            37, 5,45,13,53,21,61,29,
                            36, 4,44,12,52,20,60,28,
                            35, 3,43,11,51,19,59,27,
                            34, 2,42,10,50,18,58,26,
                            33, 1,41, 9,49,17,57,25,};
        static int[] p ={   16, 7,20,21,29,12,28,17,
                             1,15,23,26, 5,18,31,10,
                             2, 8,24,14,32,27, 3, 9,
                            19,13,30, 6,22,11, 4,25};
        static int[] e = {  32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
                             8, 9,10,11,12,13,12,13,14,15,16,17,
                            16,17,18,19,20,21,20,21,22,23,24,25,
                            24,25,26,27,28,29,28,29,30,31,32, 1};
        static int[] s0 ={  14, 0, 4,15,13, 7, 1, 4, 2,14,15, 2,11,13, 8, 1,
                             3,10,10, 6, 6,12,12,11, 5, 9, 9, 5, 0, 3, 7, 8,
                             4,15, 1,12,14, 8, 8, 2,13, 4, 6, 9, 2, 1,11, 7,
                            15, 5,12,11, 9, 3, 7,14, 3,10,10, 0, 5, 6, 0,13};
        static int[] toLeft ={ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };


        //测试用变量
        //UInt64 ftemp = 0, ftemp2=0, ftemp3 = 0, fresult = 0,ftemp4;
        //int ftemp5;
        //UInt64 temp1,temp2;
        //int temp5,tempj,temp6;

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //密钥长64位
            if (textBox1.Text.Length == 8)
            {
                int i;
                byte[] strkey = System.Text.Encoding.Default.GetBytes(textBox1.Text);
                userkey = 0;
                for (i = 0; i < 8; i++)
                {
                    userkey = userkey << 8;
                    userkey += strkey[i];
                }
                creatKey(userkey);
                textBox2.Focus();

            }
            else return;

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //密钥长64位
            if (textBox2.Text.Length == 8)
            {
                int i;
                byte[] strkey2 = System.Text.Encoding.Default.GetBytes(textBox2.Text);
                userkey2 = 0;
                for (i = 0; i < 8; i++)
                {
                    userkey2 = userkey2 << 8;
                    userkey2 += strkey2[i];
                }
                textBox3.Focus();
            }
            else return;

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            //密钥长64位
            if (textBox3.Text.Length == 8)
            {
                int i;
                byte[] strkey3 = System.Text.Encoding.Default.GetBytes(textBox3.Text);
                userkey3 = 0;
                for (i = 0; i < 8; i++)
                {
                    userkey3 = userkey3 << 8;
                    userkey3 += strkey3[i];
                }

                textBox4.Focus();
            }
            else return;

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            inFileName = textBox4.Text;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            outFileName = textBox5.Text;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            byte[] strkey = System.Text.Encoding.Default.GetBytes(textBox1.Text);
            if (textBox1.Text.Length < 8)
            {
                MessageBox.Show("key少于8位字符", "Error");
                return;
            }
            if (inFileName == null)
            {
                MessageBox.Show("输入文件为空!", "Error:");
                return;
            }

            if (outFileName == null)
            {
                MessageBox.Show("输出路径为空!", "Error");
                return;
            }
            if (inFileName == outFileName)
            {
                MessageBox.Show("输入输出不能为同一文件!","Error");
                return;
            }
            else
            {

                //byte[] buffer = new byte[100];
                FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader reader = new BinaryReader(inFile);
                FileStream outFile = new FileStream(outFileName, FileMode.Create);
                BinaryWriter writer = new BinaryWriter(outFile);
                //reader.Read(buffer, 0, 1);
                long fileLength = inFile.Length;
                long fileLength2 = fileLength / 1024;
                long fileLength3 = fileLength2 / 1024;
                long n = fileLength / 8;
                MessageBox.Show("文件长度" + fileLength.ToString() + "字节(约"
                 + fileLength2.ToString() + "k," + fileLength3.ToString() + "M)", "文件信息:");

                DateTime time = new DateTime();
                time = System.DateTime.Now;
                label7.Text = time.ToString("T");

                for (int i = 0; i < n; i++)
                {
                    mi = reader.ReadUInt64();
                    co = des(mi);
                    writer.Write(co);
                    progressBar1.Value = Convert.ToInt16(i * 100 / n);

                }

                //短块处理
                progressBar1.Value = 100;
                UInt64 shortmi = 0, shortco;
                int n1 = Convert.ToInt16(fileLength - n * 8);   //短块长度 字节为单位
                if (n1 > 0)
                {
                    byte[] buffer = new byte[7];
                    reader.Read(buffer, 0, n1);
                    for (int i = n1 - 1; i >= 0; i--)
                    {
                        shortmi <<= 8;
                        shortmi += Convert.ToUInt64(buffer[i]);
                    }
                    shortmi <<= (8 - n1) * 8;
                    shortmi += co >> n1 * 8;
                    //temp1 = shortmi;
                    shortco = des(shortmi);
                    writer.Seek(n1 - 8, SeekOrigin.Current);
                    writer.Write(shortco);

                }

                writer.Close();

                //计算,处理信息

                DateTime time2 = new DateTime();
                time2 = System.DateTime.Now;
                label9.Text = time2.ToString("T");
                long diff = (time2.Hour - time.Hour) * 3600000 + (time2.Minute - time.Minute) * 60000 +
                    (time2.Second - time.Second) * 1000 + time2.Millisecond - time.Millisecond+1;
                long v = fileLength * 1000 / (diff*1024);
                label11.Text = v.ToString() + "K/S";
                label13.Text = fileLength2.ToString() + "K";



                //maindes();
                MessageBox.Show("文件加密完成", "信息:");
                //reset();


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] strkey = System.Text.Encoding.Default.GetBytes(textBox1.Text);
            if (textBox1.Text.Length < 8)
            {
                MessageBox.Show("key少于8位字符", "Error");
                return;
            }
            if (inFileName == null)
            {
                MessageBox.Show("输入文件为空!", "Error:");
                return;
            }

            if (outFileName == null)
            {
                MessageBox.Show("输出路径为空!", "Error");
                return;
            }
            if (inFileName == outFileName)
            {
                MessageBox.Show("输入输出不能为同一文件!", "Error");
                return;
            }
            else
            {
                UInt64 temp1;
                for (int i = 1; i <= 8; i++)
                {
                    temp1 = key[i];
                    key[i] = key[17 - i];
                    key[17 - i] = temp1;

                }

                //byte[] buffer = new byte[100];
                FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader reader = new BinaryReader(inFile);
                FileStream outFile = new FileStream(outFileName, FileMode.Create);
                BinaryWriter writer = new BinaryWriter(outFile);
                //reader.Read(buffer, 0, 1);
                long fileLength = inFile.Length;
                long fileLength2 = fileLength / 1024;
                long fileLength3 = fileLength2 / 1024;
                long n = fileLength / 8;
                MessageBox.Show("文件长度" + fileLength.ToString() + "字节(约"
                    + fileLength2.ToString() + "k," + fileLength3.ToString() + "M)", "文件信息:");

                DateTime time = new DateTime();
                time = System.DateTime.Now;
                label7.Text = time.ToString("T");

                for (int i = 0; i < n - 1; i++)
                {
                    mi = reader.ReadUInt64();
                    co = des(mi);
                    writer.Write(co);
                    progressBar1.Value = Convert.ToInt16(i * 100 / n);

                }

                //短块处理
                progressBar1.Value = 100;
                UInt64 shortmi1 = 0, shortco1, shortmi2 = 0, shortco2;
                int n1 = Convert.ToInt16(fileLength - n * 8);   //短块长度 字节为单位
                shortmi1 = reader.ReadUInt64();
                byte[] buffer = new byte[7];
                reader.Read(buffer, 0, n1);
                for (int i = n1 - 1; i >= 0; i--)
                {
                    shortmi2 <<= 8;
                    shortmi2 += Convert.ToUInt64(buffer[i]);
                }

⌨️ 快捷键说明

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