• C#-DES加解密


    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.Security.Cryptography;
    using System.IO;
    using System.Threading;
    
    namespace DES加密
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
            //加密
            private void button1_Click(object sender, EventArgs e)
            {
                label1.Visible = true;
                progressBar1.Visible = true;
                if (mima1.Text != mima2.Text)
                {
                    MessageBox.Show("两次输入的密码不一致");
                }
                else if (mima1.Text.Length < 6 || mima1.Text.Length > 8)
                {
                    MessageBox.Show("密码输入的长度不符合要求(6-8位)");
                }
                else
                {
                    Thread tt = new Thread(star);
                    tt.IsBackground = true;
                    kg = 1;
                    myKey = mima1.Text;
                    noww = 1;
                    myInFileName = shuru.Text;
                    myOutFileName = shuchu.Text + '\' + mingzi.Text + "." + houzhui.Text;
                    tt.Start();
                   
                }
            }
            //解密
            private void button2_Click(object sender, EventArgs e)
            {
                progressBar1.Visible = true;
                label1.Visible = true;
                if (mima1.Text != mima2.Text)
                {
                    MessageBox.Show("两次输入的密码不一致");
                }
                else if (mima1.Text.Length < 6 || mima1.Text.Length > 8)
                {
                    MessageBox.Show("密码输入的长度不符合要求(6-8位)");
                }
                else
                {
                    Thread tt = new Thread(star);
                    tt.IsBackground = true;
                    kg = 2;
                    myKey = mima1.Text;
                    noww = 1;
                    myInFileName = shuru.Text;
                    myOutFileName = shuchu.Text + '\' + mingzi.Text + "." + houzhui.Text;
                    tt.Start();
                }
            }
            //加密解密函数
            int kg ,noww = 0;//0没事干,1正在执行,2失败,3成功
            string myInFileName, myOutFileName ,myKey ;
            int pp = 0;
            string ll = "0";
            public void star()
            {           
                try
                {
                    //设定秘钥初始值和偏移量
                    byte[] myDESIV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
                    byte[] myDESKey = { };
    
                    //根据密码长度设置秘钥
                    if (myKey.Length == 6)
                        myDESKey = new byte[] { (byte)myKey[0], (byte)myKey[1], (byte)myKey[2], (byte)myKey[3], (byte)myKey[4], (byte)myKey[5], 0x07, 0x08 };
                    if (myKey.Length == 7)
                        myDESKey = new byte[] { (byte)myKey[0], (byte)myKey[1], (byte)myKey[2], (byte)myKey[3], (byte)myKey[4], (byte)myKey[5], (byte)myKey[6], 0x08 };
                    if (myKey.Length == 8)
                        myDESKey = new byte[] { (byte)myKey[0], (byte)myKey[1], (byte)myKey[2], (byte)myKey[3], (byte)myKey[4], (byte)myKey[5], (byte)myKey[6], (byte)myKey[7] };
    
                    //创建输入输出文件流
                    FileStream myInFileStream = new FileStream(myInFileName, FileMode.Open, FileAccess.Read);
                    FileStream myOutFileStream = new FileStream(myOutFileName, FileMode.OpenOrCreate, FileAccess.Write);
                    myOutFileStream.SetLength(0);//这个暂时不清楚
    
                    //每次的中间流
                    byte[] tmp = new byte[100];
    
                    //已经传输的长度
                    long nowLength = 0;
    
                    //总长度,也是输入的文件流的长度
                    long allLength = myInFileStream.Length;
    
                    //创建DES对象
                    DES myDES = new DESCryptoServiceProvider();
    
                    CryptoStream myCryptoStream;
                    if (kg == 1)//加密流
                        myCryptoStream = new CryptoStream(myOutFileStream, myDES.CreateEncryptor(myDESKey, myDESIV), CryptoStreamMode.Write);
                    else//解密流
                        myCryptoStream = new CryptoStream(myOutFileStream, myDES.CreateDecryptor(myDESKey, myDESIV), CryptoStreamMode.Write);
    
                    //从输入文件中读取流,然后加入到输出文件流,中间用中间流传输
                    double nowjd = 0;
                    double gg = 0;
                    pp = 0;
                    ll = "0";
                    while (nowLength < allLength)
                    {
                        //每次写入加密(解密)文件数据的大小
                        int length = myInFileStream.Read(tmp, 0, 100);
                        myCryptoStream.Write(tmp, 0, length);
                        nowLength += length;
                        nowjd = (double)nowLength / (double)allLength;
                        if (nowjd - gg >= 0.01)
                        {             
                            gg = nowjd;
                            pp = int.Parse((nowLength * 100 / allLength).ToString());
                            ll = (nowjd * 100).ToString(); 
                        }
         
                   }
    
                    int mk = 0;
                    //关闭流
                    try
                    {
                        myCryptoStream.Close();
                    }
                    catch
                    {
                        mk = 1;
                    }
                    try
                    {
                        myInFileStream.Close();
                    }
                    catch
                    {
                        mk = 1;
                    }
                    try
                    {
                        myOutFileStream.Close();
                    }
                    catch
                    {
                        mk = 1;
                    }
    
                    if (mk == 1) noww = 2;
    
                    //更新100%
                    nowjd = 100;
                    //label1.Text = nowjd.ToString();
                    //label1.Refresh();
    
                    noww = 3;
                }
                catch
                {
                    noww = 2;
                }
    
            }
    
            private void label2_Click(object sender, EventArgs e)
            {
                //文件浏览
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    shuru .Text  = openFileDialog1.FileName; 
                }
            }
    
            private void label3_Click(object sender, EventArgs e)
            {
                //文件夹浏览
                folderBrowserDialog1.SelectedPath = "";
                folderBrowserDialog1.ShowDialog();
                if (folderBrowserDialog1.SelectedPath != "")
                    shuchu.Text = folderBrowserDialog1.SelectedPath;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                label1.Text = ll;
                progressBar1.Value = pp;
                label1.Refresh();
                if (noww == 2)
                {
                   noww = 0;
                   if(kg == 1)
                   {
                        try
                        {
                            File.Delete(shuchu.Text + '\' + mingzi.Text + "." + houzhui.Text);
                        }
                        catch { }
                        MessageBox.Show("加密失败");
                        label1.Text = "0";
                        progressBar1.Visible = false ;
                        label1.Visible = false ;
                   }
                   else 
                   {
                       try
                        {  //删除没有价值的失败文件
                                 File.Delete(shuchu.Text + '\' + mingzi.Text + "." + houzhui.Text);
                        }
                        catch { }
                        MessageBox.Show("解密失败");
                        label1.Text = "0";
                        label1.Visible = false ;
                        progressBar1.Visible = false ;
                   
                   }
                   
                }
                if(noww == 3)
                {
                    noww = 0;
                    if(kg == 1)
                     {
                        try
                        {
                            if (checkBox1.Checked)
                                File.Delete(@shuru.Text);
                        }
                        catch { }
                        MessageBox.Show("加密成功");
                        label1.Text = "0";
                        progressBar1.Visible = false ;
                        label1.Visible = false ;
                       }
                       else 
                       {
                           try
                            {
                                if(checkBox2.Checked)
                                     File.Delete(@shuru.Text);
                            }
                            catch { }
                            MessageBox.Show("解密成功");
                            label1.Text = "0";
                            label1.Visible = false ;
                            progressBar1.Visible = false ;
                       
                       }
                }
    
            }
    
           
    
           
            
        }
    }
    

  • 相关阅读:
    MySQL存储过程和函数
    MySQL数据类型
    MySQL—基础(SQL语句)
    如何将一串字符串按照某个特定的字符分割后倒叙输出,如:www.baidu.com输出为com.baidu.www
    JAVA WEB数据中文编码问题
    如何用一条SQL语句从登录日志表中查询统计出每个人登录的次数
    JAVA WEB tomcat启动关闭问题
    thinkphp知识点
    smarty模板内容
    smarty基础
  • 原文地址:https://www.cnblogs.com/csnd/p/12062163.html
Copyright © 2020-2023  润新知