使用对称加密与解密算法对文件加密与解密

 看了张子阳的博客,自己也看了一些资料,于是动手写了一个对文件加密与机密的小玩意,贴出来供大家参考,呵呵,可能对于高手来说,这个东西显的十分粗糙,希望大侠能够指点一二。

首先是程序的运行界面。

使用对称加密与解密算法对文件加密与解密_第1张图片

在该程序中自己写了一个类CryptoHelper,用来实现对文件的加密与解密操作。下面是这个类的源代码。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6. namespace PassWord
  7. {
  8.     class CryptoHelper
  9.     {
  10.         private SymmetricAlgorithm provider;
  11.         private ICryptoTransform encryptor;
  12.         private ICryptoTransform decryptor;
  13.         private const int per_length = 100;
  14.         /// <summary>
  15.         /// 返回每个加密方法的KEY的长度
  16.         /// </summary>
  17.         /// <param name="algrorithmName">算法名</param>
  18.         /// <returns>整型数组</returns>
  19.         /// 如果数组的第四个数据是0,那么
  20.         /// 数组的第一个数据是KEY的最少位
  21.         /// 数组的第二个数据是KEY的最大位,如果为0,说说明只支持一种位数
  22.         /// 数组的第三个数据是KEY的跳跃值
  23.         /// 如果数组的第四个数据是-1
  24.         /// 数组的第一个数据是KEY可以支持的第一种位数
  25.         /// 数组的第二个数据是KEY可以支持的第二种位数
  26.         /// 数组的第三个数据是KEY可以支持的第三种位数
  27.         public static int [] GetKeyLength(string algrorithmName)
  28.         {
  29.             int[] Int_Key = new int[4] { 0,0,0,0};           
  30.             switch (algrorithmName)
  31.             {
  32.                 case "DES":
  33.                     {
  34.                         Int_Key[0] = 64;
  35.                         Int_Key[1] = 0;
  36.                         Int_Key[2] = 0;
  37.                         Int_Key[3] = 0;
  38.                         return Int_Key;
  39.                     }                   
  40.                 case "RC2":
  41.                     {
  42.                         Int_Key[0] = 40;
  43.                         Int_Key[1] = 128;
  44.                         Int_Key[2] = 8;
  45.                         Int_Key[3] = 0;
  46.                         return Int_Key;
  47.                     } 
  48.                 case "Rijndael":
  49.                     {
  50.                         Int_Key[0] = 128;
  51.                         Int_Key[1] = 192;
  52.                         Int_Key[2] = 256;
  53.                         Int_Key[3] = -1;
  54.                         return Int_Key;
  55.                     } 
  56.                 case "TripleDES":
  57.                     {
  58.                         Int_Key[0] = 128;
  59.                         Int_Key[1] = 192;
  60.                         Int_Key[2] = 64;
  61.                         Int_Key[3] = 0;
  62.                         return Int_Key;
  63.                     } 
  64.             }
  65.             return Int_Key;
  66.         }
  67.         /// <summary>
  68.         /// 构造函数,用来生成具体的加密与解密的对象
  69.         /// </summary>
  70.         /// <param name="algrorithmName">加密或者解密的算法名称</param>
  71.         /// <param name="Key">加密或解密的密钥</param>
  72.         /// <param name="IV">加密或者解密的初始化向量</param>
  73.         public CryptoHelper(string algrorithmName, string Key, string IV)
  74.         {
  75.             provider = SymmetricAlgorithm.Create(algrorithmName);
  76.             provider.Key = Encoding.UTF8.GetBytes(Key);
  77.             provider.IV = Encoding.UTF8.GetBytes(IV);
  78.             encryptor = provider.CreateEncryptor();
  79.             decryptor = provider.CreateDecryptor();
  80.         }
  81.         ~CryptoHelper()
  82.         {
  83.             provider.Clear();
  84.         }
  85.         /// <summary>
  86.         /// 加密文件的方法
  87.         /// </summary>
  88.         /// <param name="inName">源文件名</param>
  89.         /// <param name="outName">目的文件名</param>
  90.         public void EncryptFile(String inName, String outName)
  91.         {
  92.             //建立文件流用来处理源文件与目的文件
  93.             FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  94.             FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  95.             fout.SetLength(0);
  96.             //建立变量用来处理文件的读写
  97.             byte[] bin = new byte[per_length]; //加密的长度
  98.             long rdlen = 0;              //读取数据的总长度
  99.             long totlen = fin.Length;    //输入文件的总长度
  100.             int len;                    //每次读写数据的长度
  101.             
  102.             CryptoStream encStream = new CryptoStream(fout,encryptor, CryptoStreamMode.Write);           
  103.             //开始加密的过程
  104.             while (rdlen < totlen)
  105.             {
  106.                 len = fin.Read(bin, 0, per_length);
  107.                 encStream.Write(bin, 0, len);
  108.                 rdlen = rdlen + len;               
  109.             }
  110.             encStream.Flush();
  111.             encStream.Close();
  112.             fout.Close();
  113.             fin.Close();         
  114.         }
  115.         /// <summary>
  116.         /// 解密文件的算法
  117.         /// </summary>
  118.         /// <param name="inName">源文件名</param>
  119.         /// <param name="outName">目标文件名</param>
  120.         public void DecryptFile(String inName, String outName)
  121.         {
  122.             //建立文件流用来处理源文件与目的文件,源文件是加密后的文件
  123.             FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  124.             FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  125.             fout.SetLength(0);
  126.             //建立变量用来处理文件的读写
  127.             byte[] bin = new byte[per_length]; //加密的长度
  128.             long rdlen = 0;              //读取数据的总长度
  129.             long totlen = fin.Length;    //输入文件的总长度
  130.             int len;                    //每次读写数据的长度
  131.             CryptoStream decStream = new CryptoStream(fout,decryptor, CryptoStreamMode.Write);
  132.             //开始解密的过程
  133.             while (rdlen < totlen)
  134.             {
  135.                 len = fin.Read(bin, 0, per_length);
  136.                 decStream.Write(bin, 0, len);
  137.                 rdlen = rdlen + len;
  138.             }
  139.             decStream.Flush();
  140.             decStream.Close();            
  141.             fout.Close();
  142.             fin.Close();         
  143.         }
  144.     }
  145. }

下面是界面的设计

使用对称加密与解密算法对文件加密与解密_第2张图片

 

下面是界面的一些设置的代码,这些代码由系统自己生成

  1. namespace PassWord
  2. {
  3.     partial class MainForm
  4.     {
  5.         /// <summary>
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary>
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region Windows 窗体设计器生成的代码
  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.components = new System.ComponentModel.Container();
  29.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
  30.             this.pass_cb_style = new System.Windows.Forms.ComboBox();
  31.             this.label1 = new System.Windows.Forms.Label();
  32.             this.pass_tb_first = new System.Windows.Forms.TextBox();
  33.             this.pass_tb_second = new System.Windows.Forms.TextBox();
  34.             this.label2 = new System.Windows.Forms.Label();
  35.             this.label3 = new System.Windows.Forms.Label();
  36.             this.pass_but_encrty = new System.Windows.Forms.Button();
  37.             this.pass_but_decrty = new System.Windows.Forms.Button();
  38.             this.del_file_checkBox = new System.Windows.Forms.CheckBox();
  39.             this.label4 = new System.Windows.Forms.Label();
  40.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  41.             this.label5 = new System.Windows.Forms.Label();
  42.             this.SuspendLayout();
  43.             // 
  44.             // pass_cb_style
  45.             // 
  46.             this.pass_cb_style.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
  47.             this.pass_cb_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  48.             this.pass_cb_style.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  49.             this.pass_cb_style.FormattingEnabled = true;
  50.             this.pass_cb_style.Items.AddRange(new object[] {
  51.             "DES",
  52.             "RC2",
  53.             "Rijndael",
  54.             "TripleDES"});
  55.             this.pass_cb_style.Location = new System.Drawing.Point(174, 16);
  56.             this.pass_cb_style.Name = "pass_cb_style";
  57.             this.pass_cb_style.Size = new System.Drawing.Size(167, 30);
  58.             this.pass_cb_style.TabIndex = 0;
  59.             this.pass_cb_style.SelectedIndexChanged += new System.EventHandler(this.pass_cb_style_SelectedIndexChanged);
  60.             // 
  61.             // label1
  62.             // 
  63.             this.label1.AutoSize = true;
  64.             this.label1.BackColor = System.Drawing.Color.Transparent;
  65.             this.label1.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  66.             this.label1.Location = new System.Drawing.Point(18, 20);
  67.             this.label1.Name = "label1";
  68.             this.label1.Size = new System.Drawing.Size(149, 20);
  69.             this.label1.TabIndex = 1;
  70.             this.label1.Text = "选择加密的算法";
  71.             // 
  72.             // pass_tb_first
  73.             // 
  74.             this.pass_tb_first.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  75.             this.pass_tb_first.Location = new System.Drawing.Point(174, 71);
  76.             this.pass_tb_first.Name = "pass_tb_first";
  77.             this.pass_tb_first.PasswordChar = '*';
  78.             this.pass_tb_first.Size = new System.Drawing.Size(167, 30);
  79.             this.pass_tb_first.TabIndex = 2;
  80.             this.pass_tb_first.Leave += new System.EventHandler(this.pass_tb_first_Leave);
  81.             this.pass_tb_first.KeyUp += new System.Windows.Forms.KeyEventHandler(this.pass_tb_first_KeyUp);
  82.             this.pass_tb_first.Enter += new System.EventHandler(this.pass_tb_first_Enter);
  83.             // 
  84.             // pass_tb_second
  85.             // 
  86.             this.pass_tb_second.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  87.             this.pass_tb_second.Location = new System.Drawing.Point(176, 164);
  88.             this.pass_tb_second.Name = "pass_tb_second";
  89.             this.pass_tb_second.PasswordChar = '*';
  90.             this.pass_tb_second.Size = new System.Drawing.Size(165, 30);
  91.             this.pass_tb_second.TabIndex = 3;
  92.             this.pass_tb_second.Leave += new System.EventHandler(this.pass_tb_second_Leave);
  93.             this.pass_tb_second.KeyUp += new System.Windows.Forms.KeyEventHandler(this.pass_tb_second_KeyUp);
  94.             // 
  95.             // label2
  96.             // 
  97.             this.label2.AutoSize = true;
  98.             this.label2.BackColor = System.Drawing.Color.Transparent;
  99.             this.label2.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  100.             this.label2.Location = new System.Drawing.Point(18, 76);
  101.             this.label2.Name = "label2";
  102.             this.label2.Size = new System.Drawing.Size(129, 20);
  103.             this.label2.TabIndex = 4;
  104.             this.label2.Text = "初次输入密码";
  105.             // 
  106.             // label3
  107.             // 
  108.             this.label3.AutoSize = true;
  109.             this.label3.BackColor = System.Drawing.Color.Transparent;
  110.             this.label3.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  111.             this.label3.Location = new System.Drawing.Point(18, 169);
  112.             this.label3.Name = "label3";
  113.             this.label3.Size = new System.Drawing.Size(129, 20);
  114.             this.label3.TabIndex = 5;
  115.             this.label3.Text = "再次输入密码";
  116.             // 
  117.             // pass_but_encrty
  118.             // 
  119.             this.pass_but_encrty.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  120.             this.pass_but_encrty.ForeColor = System.Drawing.Color.Red;
  121.             this.pass_but_encrty.Location = new System.Drawing.Point(40, 212);
  122.             this.pass_but_encrty.Name = "pass_but_encrty";
  123.             this.pass_but_encrty.Size = new System.Drawing.Size(77, 34);
  124.             this.pass_but_encrty.TabIndex = 6;
  125.             this.pass_but_encrty.Text = "加密";
  126.             this.pass_but_encrty.UseVisualStyleBackColor = true;
  127.             this.pass_but_encrty.Click += new System.EventHandler(this.pass_but_encrty_Click);
  128.             // 
  129.             // pass_but_decrty
  130.             // 
  131.             this.pass_but_decrty.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  132.             this.pass_but_decrty.ForeColor = System.Drawing.Color.Lime;
  133.             this.pass_but_decrty.Location = new System.Drawing.Point(201, 212);
  134.             this.pass_but_decrty.Name = "pass_but_decrty";
  135.             this.pass_but_decrty.Size = new System.Drawing.Size(77, 34);
  136.             this.pass_but_decrty.TabIndex = 7;
  137.             this.pass_but_decrty.Text = "解密";
  138.             this.pass_but_decrty.UseVisualStyleBackColor = true;
  139.             this.pass_but_decrty.Click += new System.EventHandler(this.pass_but_decrty_Click);
  140.             // 
  141.             // del_file_checkBox
  142.             // 
  143.             this.del_file_checkBox.AutoSize = true;
  144.             this.del_file_checkBox.BackColor = System.Drawing.Color.Transparent;
  145.             this.del_file_checkBox.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  146.             this.del_file_checkBox.Location = new System.Drawing.Point(40, 269);
  147.             this.del_file_checkBox.Name = "del_file_checkBox";
  148.             this.del_file_checkBox.Size = new System.Drawing.Size(128, 24);
  149.             this.del_file_checkBox.TabIndex = 8;
  150.             this.del_file_checkBox.Text = "删除源文件";
  151.             this.del_file_checkBox.UseVisualStyleBackColor = false;
  152.             // 
  153.             // label4
  154.             // 
  155.             this.label4.AutoSize = true;
  156.             this.label4.BackColor = System.Drawing.Color.Transparent;
  157.             this.label4.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  158.             this.label4.Location = new System.Drawing.Point(18, 119);
  159.             this.label4.Name = "label4";
  160.             this.label4.Size = new System.Drawing.Size(119, 20);
  161.             this.label4.TabIndex = 10;
  162.             this.label4.Text = "密 码 强 度";
  163.             // 
  164.             // timer1
  165.             // 
  166.             this.timer1.Interval = 500;
  167.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  168.             // 
  169.             // label5
  170.             // 
  171.             this.label5.AutoSize = true;
  172.             this.label5.BackColor = System.Drawing.Color.White;
  173.             this.label5.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  174.             this.label5.ForeColor = System.Drawing.Color.Red;
  175.             this.label5.Location = new System.Drawing.Point(172, 119);
  176.             this.label5.Name = "label5";
  177.             this.label5.Size = new System.Drawing.Size(69, 20);
  178.             this.label5.TabIndex = 11;
  179.             this.label5.Text = "非常弱";
  180.             // 
  181.             // MainForm
  182.             // 
  183.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  184.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  185.             this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
  186.             this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
  187.             this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
  188.             this.ClientSize = new System.Drawing.Size(366, 314);
  189.             this.Controls.Add(this.label5);
  190.             this.Controls.Add(this.label4);
  191.             this.Controls.Add(this.del_file_checkBox);
  192.             this.Controls.Add(this.pass_but_decrty);
  193.             this.Controls.Add(this.pass_but_encrty);
  194.             this.Controls.Add(this.label3);
  195.             this.Controls.Add(this.label2);
  196.             this.Controls.Add(this.pass_tb_second);
  197.             this.Controls.Add(this.pass_tb_first);
  198.             this.Controls.Add(this.label1);
  199.             this.Controls.Add(this.pass_cb_style);
  200.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  201.             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  202.             this.MaximizeBox = false;
  203.             this.MinimizeBox = false;
  204.             this.Name = "MainForm";
  205.             this.Text = "加密与解密";
  206.             this.Load += new System.EventHandler(this.Form1_Load);
  207.             this.ResumeLayout(false);
  208.             this.PerformLayout();
  209.         }
  210.         #endregion
  211.         private System.Windows.Forms.ComboBox pass_cb_style;
  212.         private System.Windows.Forms.Label label1;
  213.         private System.Windows.Forms.TextBox pass_tb_first;
  214.         private System.Windows.Forms.TextBox pass_tb_second;
  215.         private System.Windows.Forms.Label label2;
  216.         private System.Windows.Forms.Label label3;
  217.         private System.Windows.Forms.Button pass_but_encrty;
  218.         private System.Windows.Forms.Button pass_but_decrty;
  219.         private System.Windows.Forms.CheckBox del_file_checkBox;
  220.         private System.Windows.Forms.Label label4;
  221.         private System.Windows.Forms.Timer timer1;
  222.         private System.Windows.Forms.Label label5;
  223.     }
  224. }

其中控件的一些名称如表所示:

控件类别 控件名称 控件的用途
下拉式列表框 pass_cb_style 用来选择加密的算法
文本框 pass_tb_first 让用户输入密码
文本框 pass_tb_second 让用户输入密码,验证密码
按钮 pass_but_encrty 加密按钮
按钮 pass_but_decrty 解密按钮
标签 label5 指示密码强度
复选框 del_file_checkBox

是否删除源文件复选框

 

其中还放置了一个Timer控件,主要用来测试密码强度。

下面就是界面部分编制的代码了

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace PassWord
  10. {
  11.     public partial class MainForm : Form
  12.     {
  13.         private string InputFile = string.Empty;
  14.         private string OutputFile = string.Empty;
  15.         private string algrorithmName = "DES";
  16.         private int lowerChar = 0;//用来标志小写字母
  17.         private int upperChar = 0;//用来标志大写字母
  18.         private int numChar = 0;//用来标志数字
  19.         private int otherChar = 0;//用来标志其他字符
  20.         private int userPassLen = 0;
  21.         private int userPassScore = 0;
  22.         public MainForm()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.         private void Form1_Load(object sender, EventArgs e)
  27.         {
  28.             
  29.             pass_cb_style.Text = "DES";
  30.             pass_but_encrty.Enabled = false;
  31.             pass_but_decrty.Enabled = false;
  32.             timer1.Enabled = false;
  33.         }
  34.         private void pass_but_encrty_Click(object sender, EventArgs e)
  35.         {
  36.             string Key = GetKey();
  37.             string IV = GetIV();
  38.             CryptoHelper encFile = new CryptoHelper(algrorithmName, Key, IV);
  39.             GetIOFileName();
  40.             if (InputFile != null && OutputFile != null)
  41.             {
  42.                 try
  43.                 {
  44.                     encFile.EncryptFile(InputFile, OutputFile);
  45.                     MessageBox.Show("加密成功!!!""恭喜!成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  46.                     if (del_file_checkBox.Checked == true)
  47.                         DeleteFile(InputFile);
  48.                 }
  49.                 catch
  50.                 {
  51.                     MessageBox.Show("加密失败""十分遗憾", MessageBoxButtons.OK, MessageBoxIcon.Error);
  52.                 }
  53.             }
  54.         }
  55.         private void pass_but_decrty_Click(object sender, EventArgs e)
  56.         {
  57.             string Key = GetKey();
  58.             string IV = GetIV();
  59.             CryptoHelper decFile = new CryptoHelper(algrorithmName, Key, IV);
  60.             GetIOFileName();
  61.             if (InputFile != null && OutputFile != null)
  62.             {
  63.                 try
  64.                 {
  65.                     decFile.DecryptFile(InputFile, OutputFile);
  66.                     MessageBox.Show("解密成功!!!""恭喜!成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  67.                     if (del_file_checkBox.Checked == true)
  68.                         DeleteFile(InputFile);
  69.                 }
  70.                 catch
  71.                 {
  72.                     MessageBox.Show("解密失败""十分遗憾", MessageBoxButtons.OK, MessageBoxIcon.Error);
  73.                 }
  74.             }
  75.         }
  76.         /// <summary>
  77.         /// 删除文件的函数
  78.         /// </summary>
  79.         /// <param name="filename">文件名</param>
  80.         public void DeleteFile(string filename)
  81.         {
  82.             try
  83.             {
  84.                 File.Delete(filename);
  85.             }
  86.             catch (Exception e)
  87.             {
  88.                 MessageBox.Show(e.ToString());
  89.             }
  90.         }
  91.         /// <summary>
  92.         /// 得到输入/输出的文件名
  93.         /// </summary>
  94.         public void GetIOFileName()
  95.         {
  96.             OpenFileDialog openFile = new OpenFileDialog();
  97.             openFile.Filter = "All files (*.*)|*.*";
  98.             openFile.Title = "请你选择源文件";
  99.             if (openFile.ShowDialog() == DialogResult.OK)
  100.             {
  101.                 InputFile = openFile.FileName;
  102.                 SaveFileDialog saveFile = new SaveFileDialog();
  103.                 saveFile.Filter = "All files (*.*)|*.*";
  104.                 saveFile.Title = "请你选择目标文件";
  105.                 if (saveFile.ShowDialog() == DialogResult.OK)
  106.                 {
  107.                     OutputFile = saveFile.FileName;
  108.                     if (OutputFile == InputFile)
  109.                     {
  110.                         OutputFile = null;
  111.                         MessageBox.Show("源文件不能与目标文件为同一个文件""错误", MessageBoxButtons.OK);
  112.                     }
  113.                 }
  114.                 else
  115.                     OutputFile = null;
  116.             }
  117.             else
  118.                 InputFile = null;
  119.         }
  120.         /// <summary>
  121.         /// 得到算法名称,默认为DES
  122.         /// </summary>
  123.         /// <param name="sender"></param>
  124.         /// <param name="e"></param>
  125.         private void pass_cb_style_SelectedIndexChanged(object sender, EventArgs e)
  126.         {
  127.             algrorithmName = pass_cb_style.Text;
  128.         }
  129.         /// <summary>
  130.         /// 设置密码强度
  131.         /// </summary>
  132.         /// <param name="sender"></param>
  133.         /// <param name="e"></param>
  134.         private void pass_tb_first_KeyUp(object sender, KeyEventArgs e)
  135.         {            
  136.         }
  137.         private void pass_tb_second_Leave(object sender, EventArgs e)
  138.         {
  139.             if (pass_tb_first.Text != pass_tb_second.Text)
  140.             {
  141.                 MessageBox.Show("两次输入的密码不一致""错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  142.                 pass_tb_second.Text = "";
  143.             }
  144.         }
  145.         private void pass_tb_second_KeyUp(object sender, KeyEventArgs e)
  146.         {
  147.             if (pass_tb_first.Text == pass_tb_second.Text)
  148.             {
  149.                 pass_but_decrty.Enabled = true;
  150.                 pass_but_encrty.Enabled = true;
  151.             }
  152.             else
  153.             {
  154.                 pass_but_decrty.Enabled = false;
  155.                 pass_but_encrty.Enabled = false;
  156.             }
  157.         }
  158.         /// <summary>
  159.         /// 得到密码
  160.         /// </summary>
  161.         /// <returns></returns>
  162.         private string GetKey()
  163.         {
  164.             string passWord = pass_tb_first.Text;
  165.             int[] LegalLength = new int[4];
  166.             LegalLength = CryptoHelper.GetKeyLength(algrorithmName);
  167.             int plength = passWord.Length;
  168.             switch (algrorithmName)
  169.             {
  170.                 case "DES":
  171.                     {
  172.                         if (plength == LegalLength[0] / 8)
  173.                             passWord = pass_tb_first.Text;
  174.                         if (plength > LegalLength[0] / 8)
  175.                             passWord = pass_tb_first.Text.Substring(0, 8);
  176.                         if (plength < LegalLength[0] / 8)
  177.                             passWord = RepeatStr(pass_tb_first.Text, 8);
  178.                         break;
  179.                     }
  180.                 case "RC2":
  181.                     {
  182.                         if (plength >= LegalLength[0] / 8 && plength <= LegalLength[1] / 8)
  183.                             passWord = pass_tb_first.Text;
  184.                         if (plength > LegalLength[1] / 8)
  185.                             passWord = pass_tb_first.Text.Substring(0, 16);
  186.                         if (plength < LegalLength[0] / 8)
  187.                             passWord = RepeatStr(pass_tb_first.Text, 5);
  188.                         break;
  189.                     }
  190.                 case "TripleDES":
  191.                     {
  192.                         if (plength >= LegalLength[0] / 8 && plength <= LegalLength[1] / 8)
  193.                         {
  194.                             passWord = pass_tb_first.Text;
  195.                             if (plength % 2 != 0)
  196.                             {
  197.                                 passWord += passWord.Substring(0, 1);
  198.                                 plength = passWord.Length;
  199.                             }
  200.                         }
  201.                         if (plength > LegalLength[1] / 8)
  202.                             passWord = pass_tb_first.Text.Substring(0, 24);
  203.                         if (plength < LegalLength[0] / 8)
  204.                             passWord = RepeatStr(pass_tb_first.Text, 16);
  205.                         break;
  206.                     }
  207.                 case "Rijndael":
  208.                     {
  209.                         passWord = pass_tb_first.Text;
  210.                         if (plength < LegalLength[0] / 8)
  211.                             passWord = RepeatStr(passWord, 16);
  212.                         if (plength < LegalLength[1] / 8)
  213.                             passWord = RepeatStr(passWord, 24);
  214.                         if (plength < LegalLength[2] / 8)
  215.                             passWord = RepeatStr(passWord, 32);
  216.                         break;
  217.                     }
  218.             }
  219.             return passWord;
  220.         }
  221.         /// <summary>
  222.         /// 得到加密的块
  223.         /// </summary>
  224.         /// <returns>符合要求的块</returns>
  225.         private string GetIV()
  226.         {
  227.             string iv = pass_tb_first.Text;
  228.             int len = iv.Length;
  229.             switch (algrorithmName)
  230.             {
  231.                 case "Rijndael":
  232.                     {
  233.                         if (len < 16)
  234.                             iv = RepeatStr(pass_tb_first.Text, 16);
  235.                         else
  236.                             iv = pass_tb_first.Text.Substring(0, 16);
  237.                         break;
  238.                     }
  239.                 default:
  240.                     {
  241.                         if (len < 8)
  242.                             iv = RepeatStr(pass_tb_first.Text, 8);
  243.                         else
  244.                             iv = pass_tb_first.Text.Substring(0, 8);
  245.                         break;
  246.                     }
  247.             }
  248.             return iv;
  249.         }
  250.         /// <summary>
  251.         /// 重复字符串到指定的长度
  252.         /// </summary>
  253.         /// <param name="str">字符串</param>
  254.         /// <param name="length">指定长度</param>
  255.         /// <returns>符合要求字符串</returns>
  256.         private string RepeatStr(string str, int length)
  257.         {
  258.             int lenold = str.Length;
  259.             string temp = str;
  260.             int len = lenold;
  261.             if (len >= length)
  262.                 return str;
  263.             while (len < length)
  264.             {
  265.                 if (length - len < lenold)
  266.                 {
  267.                     str += str.Substring(0, length - len);
  268.                     len += length - len;
  269.                 }
  270.                 else
  271.                 {
  272.                     str += temp;
  273.                     len += lenold;
  274.                 }
  275.             }
  276.             return str;
  277.         }
  278.         private void timer1_Tick(object sender, EventArgs e)
  279.         {
  280.             // 一、密码长度:
  281.             //5 分: 小于等于 4 个字符 
  282.             //10 分: 5 到 7 字符 
  283.             //25 分: 大于等于 8 个字符 
  284.             //二、字母:
  285.             //0 分: 没有字母 
  286.             //10 分: 全都是小(大)写字母 
  287.             //20 分: 大小写混合字母 
  288.             //三、数字:
  289.             //0 分: 没有数字 
  290.             //10 分: 1 个数字 
  291.             //20 分: 大于等于 3 个数字 
  292.             //四、符号:
  293.             //0 分: 没有符号 
  294.             //10 分: 1 个符号 
  295.             //25 分: 大于 1 个符号 
  296.             //五、奖励:
  297.             //2 分: 字母和数字 
  298.             //3 分: 字母、数字和符号 
  299.             //5 分: 大小写字母、数字和符号 
  300.             //最后的评分标准:
  301.             //>= 90: 非常安全 
  302.             //>= 80: 安全(Secure) 
  303.             //>= 70: 非常强 
  304.             //>= 60: 强(Strong) 
  305.             //>= 50: 一般(Average) 
  306.             //>= 25: 弱(Weak) 
  307.             //>= 0: 非常弱 
  308.             userPassScore = 0;
  309.             userPassLen = 0;
  310.             userPassLen=pass_tb_first.Text.Length;
  311.             lowerChar = 0;
  312.             upperChar = 0;
  313.             numChar = 0;
  314.             otherChar = 0;            
  315.             foreach (char c in pass_tb_first.Text.ToCharArray())
  316.             {
  317.                 if (Char.IsLower(c))
  318.                 {
  319.                     lowerChar++;
  320.                 }
  321.                 if (Char.IsUpper(c))
  322.                 {
  323.                     upperChar++;
  324.                 }
  325.                 if (Char.IsDigit(c))
  326.                 {
  327.                     numChar++;
  328.                 }
  329.                 if (Char.IsSymbol(c) || Char.IsPunctuation(c) || Char.IsSeparator(c) || Char.IsWhiteSpace(c))
  330.                 {
  331.                     otherChar++;
  332.                 }
  333.             }
  334.             //长度
  335.             if (userPassLen <= 4 && userPassLen!=0)
  336.                 userPassScore = 5;
  337.             if (userPassLen >= 5 && userPassLen <= 7)
  338.                 userPassScore = 10;
  339.             if (userPassLen >= 8)
  340.                 userPassScore = 25;
  341.             if ((lowerChar != 0 || upperChar != 0) && (userPassLen == lowerChar || userPassLen == upperChar))
  342.             {
  343.                 userPassScore += 10;
  344.             }
  345.             if ((lowerChar != 0 ||upperChar != 0 )&& (userPassLen != lowerChar || userPassLen != upperChar))
  346.             {
  347.                 userPassScore += 20;
  348.             }
  349.             if (numChar != 0)
  350.             {
  351.                 userPassScore += 10;
  352.                 if (numChar > 3)
  353.                     userPassScore += 20;
  354.             }
  355.             if (otherChar != 0)
  356.             {
  357.                 userPassScore += 10;
  358.                 if (otherChar > 3)
  359.                     userPassScore += 25;
  360.             }
  361.             if (lowerChar != 0 && upperChar != 0 && numChar != 0 && otherChar != 0)
  362.             {
  363.                 userPassScore += 5;
  364.             }
  365.             if(userPassScore<25)
  366.                 label5.Text = "非常弱";
  367.             if (userPassScore >= 25 && userPassScore < 50)
  368.             {
  369.                 label5.Text = "弱";
  370.                 label5.ForeColor = System.Drawing.Color.Red;
  371.             }
  372.             if (userPassScore >= 50 && userPassScore < 60)
  373.             {
  374.                 label5.Text = "一般";
  375.                 label5.ForeColor = System.Drawing.Color.Blue;
  376.             }
  377.             if (userPassScore >= 60 && userPassScore < 70)
  378.             {
  379.                 label5.Text = "强";
  380.                 label5.ForeColor = System.Drawing.Color.YellowGreen;
  381.             }
  382.             if (userPassScore >= 70 && userPassScore < 80)
  383.             {
  384.                 label5.Text = "非常强";
  385.                 label5.ForeColor = System.Drawing.Color.LightGreen;
  386.             }
  387.             if (userPassScore >= 80 && userPassScore < 90)
  388.             {
  389.                 label5.Text = "安全";
  390.                 label5.ForeColor = System.Drawing.Color.GreenYellow;
  391.             }
  392.             if (userPassScore >= 90)
  393.             {
  394.                 label5.Text = "非常安全";
  395.                 label5.ForeColor = System.Drawing.Color.Green;
  396.             }
  397.         }
  398.         private void pass_tb_first_Enter(object sender, EventArgs e)
  399.         {
  400.             timer1.Enabled = true;
  401.         }
  402.         private void pass_tb_first_Leave(object sender, EventArgs e)
  403.         {
  404.             timer1.Enabled = false;
  405.         }
  406.     }
  407. }

最后的一点补充说明

加密算法名称 密钥位数(KEY) 初始化向量(IV)
DES 64位 64位
RC2 40位到128位,位数递增8 64位
Rijndael 128,192,256 128--256 每次递增64位
TripleDES 128位到192位 64位

你可能感兴趣的:(加密,算法,timer,解密,File,byte)