看了张子阳的博客,自己也看了一些资料,于是动手写了一个对文件加密与机密的小玩意,贴出来供大家参考,呵呵,可能对于高手来说,这个东西显的十分粗糙,希望大侠能够指点一二。
首先是程序的运行界面。
在该程序中自己写了一个类CryptoHelper,用来实现对文件的加密与解密操作。下面是这个类的源代码。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace PassWord
- {
- class CryptoHelper
- {
- private SymmetricAlgorithm provider;
- private ICryptoTransform encryptor;
- private ICryptoTransform decryptor;
- private const int per_length = 100;
- /// <summary>
- /// 返回每个加密方法的KEY的长度
- /// </summary>
- /// <param name="algrorithmName">算法名</param>
- /// <returns>整型数组</returns>
- /// 如果数组的第四个数据是0,那么
- /// 数组的第一个数据是KEY的最少位
- /// 数组的第二个数据是KEY的最大位,如果为0,说说明只支持一种位数
- /// 数组的第三个数据是KEY的跳跃值
- /// 如果数组的第四个数据是-1
- /// 数组的第一个数据是KEY可以支持的第一种位数
- /// 数组的第二个数据是KEY可以支持的第二种位数
- /// 数组的第三个数据是KEY可以支持的第三种位数
- public static int [] GetKeyLength(string algrorithmName)
- {
- int[] Int_Key = new int[4] { 0,0,0,0};
- switch (algrorithmName)
- {
- case "DES":
- {
- Int_Key[0] = 64;
- Int_Key[1] = 0;
- Int_Key[2] = 0;
- Int_Key[3] = 0;
- return Int_Key;
- }
- case "RC2":
- {
- Int_Key[0] = 40;
- Int_Key[1] = 128;
- Int_Key[2] = 8;
- Int_Key[3] = 0;
- return Int_Key;
- }
- case "Rijndael":
- {
- Int_Key[0] = 128;
- Int_Key[1] = 192;
- Int_Key[2] = 256;
- Int_Key[3] = -1;
- return Int_Key;
- }
- case "TripleDES":
- {
- Int_Key[0] = 128;
- Int_Key[1] = 192;
- Int_Key[2] = 64;
- Int_Key[3] = 0;
- return Int_Key;
- }
- }
- return Int_Key;
- }
- /// <summary>
- /// 构造函数,用来生成具体的加密与解密的对象
- /// </summary>
- /// <param name="algrorithmName">加密或者解密的算法名称</param>
- /// <param name="Key">加密或解密的密钥</param>
- /// <param name="IV">加密或者解密的初始化向量</param>
- public CryptoHelper(string algrorithmName, string Key, string IV)
- {
- provider = SymmetricAlgorithm.Create(algrorithmName);
- provider.Key = Encoding.UTF8.GetBytes(Key);
- provider.IV = Encoding.UTF8.GetBytes(IV);
- encryptor = provider.CreateEncryptor();
- decryptor = provider.CreateDecryptor();
- }
- ~CryptoHelper()
- {
- provider.Clear();
- }
- /// <summary>
- /// 加密文件的方法
- /// </summary>
- /// <param name="inName">源文件名</param>
- /// <param name="outName">目的文件名</param>
- public void EncryptFile(String inName, String outName)
- {
- //建立文件流用来处理源文件与目的文件
- FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength(0);
- //建立变量用来处理文件的读写
- byte[] bin = new byte[per_length]; //加密的长度
- long rdlen = 0; //读取数据的总长度
- long totlen = fin.Length; //输入文件的总长度
- int len; //每次读写数据的长度
- CryptoStream encStream = new CryptoStream(fout,encryptor, CryptoStreamMode.Write);
- //开始加密的过程
- while (rdlen < totlen)
- {
- len = fin.Read(bin, 0, per_length);
- encStream.Write(bin, 0, len);
- rdlen = rdlen + len;
- }
- encStream.Flush();
- encStream.Close();
- fout.Close();
- fin.Close();
- }
- /// <summary>
- /// 解密文件的算法
- /// </summary>
- /// <param name="inName">源文件名</param>
- /// <param name="outName">目标文件名</param>
- public void DecryptFile(String inName, String outName)
- {
- //建立文件流用来处理源文件与目的文件,源文件是加密后的文件
- FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength(0);
- //建立变量用来处理文件的读写
- byte[] bin = new byte[per_length]; //加密的长度
- long rdlen = 0; //读取数据的总长度
- long totlen = fin.Length; //输入文件的总长度
- int len; //每次读写数据的长度
- CryptoStream decStream = new CryptoStream(fout,decryptor, CryptoStreamMode.Write);
- //开始解密的过程
- while (rdlen < totlen)
- {
- len = fin.Read(bin, 0, per_length);
- decStream.Write(bin, 0, len);
- rdlen = rdlen + len;
- }
- decStream.Flush();
- decStream.Close();
- fout.Close();
- fin.Close();
- }
- }
- }
下面是界面的设计
下面是界面的一些设置的代码,这些代码由系统自己生成
- namespace PassWord
- {
- partial class MainForm
- {
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
- this.pass_cb_style = new System.Windows.Forms.ComboBox();
- this.label1 = new System.Windows.Forms.Label();
- this.pass_tb_first = new System.Windows.Forms.TextBox();
- this.pass_tb_second = new System.Windows.Forms.TextBox();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.pass_but_encrty = new System.Windows.Forms.Button();
- this.pass_but_decrty = new System.Windows.Forms.Button();
- this.del_file_checkBox = new System.Windows.Forms.CheckBox();
- this.label4 = new System.Windows.Forms.Label();
- this.timer1 = new System.Windows.Forms.Timer(this.components);
- this.label5 = new System.Windows.Forms.Label();
- this.SuspendLayout();
- //
- // pass_cb_style
- //
- this.pass_cb_style.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
- this.pass_cb_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
- this.pass_cb_style.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.pass_cb_style.FormattingEnabled = true;
- this.pass_cb_style.Items.AddRange(new object[] {
- "DES",
- "RC2",
- "Rijndael",
- "TripleDES"});
- this.pass_cb_style.Location = new System.Drawing.Point(174, 16);
- this.pass_cb_style.Name = "pass_cb_style";
- this.pass_cb_style.Size = new System.Drawing.Size(167, 30);
- this.pass_cb_style.TabIndex = 0;
- this.pass_cb_style.SelectedIndexChanged += new System.EventHandler(this.pass_cb_style_SelectedIndexChanged);
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.BackColor = System.Drawing.Color.Transparent;
- this.label1.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.label1.Location = new System.Drawing.Point(18, 20);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(149, 20);
- this.label1.TabIndex = 1;
- this.label1.Text = "选择加密的算法";
- //
- // pass_tb_first
- //
- this.pass_tb_first.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.pass_tb_first.Location = new System.Drawing.Point(174, 71);
- this.pass_tb_first.Name = "pass_tb_first";
- this.pass_tb_first.PasswordChar = '*';
- this.pass_tb_first.Size = new System.Drawing.Size(167, 30);
- this.pass_tb_first.TabIndex = 2;
- this.pass_tb_first.Leave += new System.EventHandler(this.pass_tb_first_Leave);
- this.pass_tb_first.KeyUp += new System.Windows.Forms.KeyEventHandler(this.pass_tb_first_KeyUp);
- this.pass_tb_first.Enter += new System.EventHandler(this.pass_tb_first_Enter);
- //
- // pass_tb_second
- //
- this.pass_tb_second.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.pass_tb_second.Location = new System.Drawing.Point(176, 164);
- this.pass_tb_second.Name = "pass_tb_second";
- this.pass_tb_second.PasswordChar = '*';
- this.pass_tb_second.Size = new System.Drawing.Size(165, 30);
- this.pass_tb_second.TabIndex = 3;
- this.pass_tb_second.Leave += new System.EventHandler(this.pass_tb_second_Leave);
- this.pass_tb_second.KeyUp += new System.Windows.Forms.KeyEventHandler(this.pass_tb_second_KeyUp);
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.BackColor = System.Drawing.Color.Transparent;
- this.label2.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.label2.Location = new System.Drawing.Point(18, 76);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(129, 20);
- this.label2.TabIndex = 4;
- this.label2.Text = "初次输入密码";
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.BackColor = System.Drawing.Color.Transparent;
- this.label3.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.label3.Location = new System.Drawing.Point(18, 169);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(129, 20);
- this.label3.TabIndex = 5;
- this.label3.Text = "再次输入密码";
- //
- // pass_but_encrty
- //
- this.pass_but_encrty.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.pass_but_encrty.ForeColor = System.Drawing.Color.Red;
- this.pass_but_encrty.Location = new System.Drawing.Point(40, 212);
- this.pass_but_encrty.Name = "pass_but_encrty";
- this.pass_but_encrty.Size = new System.Drawing.Size(77, 34);
- this.pass_but_encrty.TabIndex = 6;
- this.pass_but_encrty.Text = "加密";
- this.pass_but_encrty.UseVisualStyleBackColor = true;
- this.pass_but_encrty.Click += new System.EventHandler(this.pass_but_encrty_Click);
- //
- // pass_but_decrty
- //
- this.pass_but_decrty.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.pass_but_decrty.ForeColor = System.Drawing.Color.Lime;
- this.pass_but_decrty.Location = new System.Drawing.Point(201, 212);
- this.pass_but_decrty.Name = "pass_but_decrty";
- this.pass_but_decrty.Size = new System.Drawing.Size(77, 34);
- this.pass_but_decrty.TabIndex = 7;
- this.pass_but_decrty.Text = "解密";
- this.pass_but_decrty.UseVisualStyleBackColor = true;
- this.pass_but_decrty.Click += new System.EventHandler(this.pass_but_decrty_Click);
- //
- // del_file_checkBox
- //
- this.del_file_checkBox.AutoSize = true;
- this.del_file_checkBox.BackColor = System.Drawing.Color.Transparent;
- this.del_file_checkBox.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.del_file_checkBox.Location = new System.Drawing.Point(40, 269);
- this.del_file_checkBox.Name = "del_file_checkBox";
- this.del_file_checkBox.Size = new System.Drawing.Size(128, 24);
- this.del_file_checkBox.TabIndex = 8;
- this.del_file_checkBox.Text = "删除源文件";
- this.del_file_checkBox.UseVisualStyleBackColor = false;
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.BackColor = System.Drawing.Color.Transparent;
- this.label4.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.label4.Location = new System.Drawing.Point(18, 119);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(119, 20);
- this.label4.TabIndex = 10;
- this.label4.Text = "密 码 强 度";
- //
- // timer1
- //
- this.timer1.Interval = 500;
- this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
- //
- // label5
- //
- this.label5.AutoSize = true;
- this.label5.BackColor = System.Drawing.Color.White;
- this.label5.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.label5.ForeColor = System.Drawing.Color.Red;
- this.label5.Location = new System.Drawing.Point(172, 119);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(69, 20);
- this.label5.TabIndex = 11;
- this.label5.Text = "非常弱";
- //
- // MainForm
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
- this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
- this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.ClientSize = new System.Drawing.Size(366, 314);
- this.Controls.Add(this.label5);
- this.Controls.Add(this.label4);
- this.Controls.Add(this.del_file_checkBox);
- this.Controls.Add(this.pass_but_decrty);
- this.Controls.Add(this.pass_but_encrty);
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.pass_tb_second);
- this.Controls.Add(this.pass_tb_first);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.pass_cb_style);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
- this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
- this.MaximizeBox = false;
- this.MinimizeBox = false;
- this.Name = "MainForm";
- this.Text = "加密与解密";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.ComboBox pass_cb_style;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.TextBox pass_tb_first;
- private System.Windows.Forms.TextBox pass_tb_second;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Button pass_but_encrty;
- private System.Windows.Forms.Button pass_but_decrty;
- private System.Windows.Forms.CheckBox del_file_checkBox;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Timer timer1;
- private System.Windows.Forms.Label label5;
- }
- }
其中控件的一些名称如表所示:
控件类别 | 控件名称 | 控件的用途 |
下拉式列表框 | pass_cb_style | 用来选择加密的算法 |
文本框 | pass_tb_first | 让用户输入密码 |
文本框 | pass_tb_second | 让用户输入密码,验证密码 |
按钮 | pass_but_encrty | 加密按钮 |
按钮 | pass_but_decrty | 解密按钮 |
标签 | label5 | 指示密码强度 |
复选框 | del_file_checkBox | 是否删除源文件复选框 |
其中还放置了一个Timer控件,主要用来测试密码强度。
下面就是界面部分编制的代码了
- 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 PassWord
- {
- public partial class MainForm : Form
- {
- private string InputFile = string.Empty;
- private string OutputFile = string.Empty;
- private string algrorithmName = "DES";
- private int lowerChar = 0;//用来标志小写字母
- private int upperChar = 0;//用来标志大写字母
- private int numChar = 0;//用来标志数字
- private int otherChar = 0;//用来标志其他字符
- private int userPassLen = 0;
- private int userPassScore = 0;
- public MainForm()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- pass_cb_style.Text = "DES";
- pass_but_encrty.Enabled = false;
- pass_but_decrty.Enabled = false;
- timer1.Enabled = false;
- }
- private void pass_but_encrty_Click(object sender, EventArgs e)
- {
- string Key = GetKey();
- string IV = GetIV();
- CryptoHelper encFile = new CryptoHelper(algrorithmName, Key, IV);
- GetIOFileName();
- if (InputFile != null && OutputFile != null)
- {
- try
- {
- encFile.EncryptFile(InputFile, OutputFile);
- MessageBox.Show("加密成功!!!", "恭喜!成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- if (del_file_checkBox.Checked == true)
- DeleteFile(InputFile);
- }
- catch
- {
- MessageBox.Show("加密失败", "十分遗憾", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- private void pass_but_decrty_Click(object sender, EventArgs e)
- {
- string Key = GetKey();
- string IV = GetIV();
- CryptoHelper decFile = new CryptoHelper(algrorithmName, Key, IV);
- GetIOFileName();
- if (InputFile != null && OutputFile != null)
- {
- try
- {
- decFile.DecryptFile(InputFile, OutputFile);
- MessageBox.Show("解密成功!!!", "恭喜!成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- if (del_file_checkBox.Checked == true)
- DeleteFile(InputFile);
- }
- catch
- {
- MessageBox.Show("解密失败", "十分遗憾", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- /// <summary>
- /// 删除文件的函数
- /// </summary>
- /// <param name="filename">文件名</param>
- public void DeleteFile(string filename)
- {
- try
- {
- File.Delete(filename);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- /// <summary>
- /// 得到输入/输出的文件名
- /// </summary>
- public void GetIOFileName()
- {
- OpenFileDialog openFile = new OpenFileDialog();
- openFile.Filter = "All files (*.*)|*.*";
- openFile.Title = "请你选择源文件";
- if (openFile.ShowDialog() == DialogResult.OK)
- {
- InputFile = openFile.FileName;
- SaveFileDialog saveFile = new SaveFileDialog();
- saveFile.Filter = "All files (*.*)|*.*";
- saveFile.Title = "请你选择目标文件";
- if (saveFile.ShowDialog() == DialogResult.OK)
- {
- OutputFile = saveFile.FileName;
- if (OutputFile == InputFile)
- {
- OutputFile = null;
- MessageBox.Show("源文件不能与目标文件为同一个文件", "错误", MessageBoxButtons.OK);
- }
- }
- else
- OutputFile = null;
- }
- else
- InputFile = null;
- }
- /// <summary>
- /// 得到算法名称,默认为DES
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pass_cb_style_SelectedIndexChanged(object sender, EventArgs e)
- {
- algrorithmName = pass_cb_style.Text;
- }
- /// <summary>
- /// 设置密码强度
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pass_tb_first_KeyUp(object sender, KeyEventArgs e)
- {
- }
- private void pass_tb_second_Leave(object sender, EventArgs e)
- {
- if (pass_tb_first.Text != pass_tb_second.Text)
- {
- MessageBox.Show("两次输入的密码不一致", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- pass_tb_second.Text = "";
- }
- }
- private void pass_tb_second_KeyUp(object sender, KeyEventArgs e)
- {
- if (pass_tb_first.Text == pass_tb_second.Text)
- {
- pass_but_decrty.Enabled = true;
- pass_but_encrty.Enabled = true;
- }
- else
- {
- pass_but_decrty.Enabled = false;
- pass_but_encrty.Enabled = false;
- }
- }
- /// <summary>
- /// 得到密码
- /// </summary>
- /// <returns></returns>
- private string GetKey()
- {
- string passWord = pass_tb_first.Text;
- int[] LegalLength = new int[4];
- LegalLength = CryptoHelper.GetKeyLength(algrorithmName);
- int plength = passWord.Length;
- switch (algrorithmName)
- {
- case "DES":
- {
- if (plength == LegalLength[0] / 8)
- passWord = pass_tb_first.Text;
- if (plength > LegalLength[0] / 8)
- passWord = pass_tb_first.Text.Substring(0, 8);
- if (plength < LegalLength[0] / 8)
- passWord = RepeatStr(pass_tb_first.Text, 8);
- break;
- }
- case "RC2":
- {
- if (plength >= LegalLength[0] / 8 && plength <= LegalLength[1] / 8)
- passWord = pass_tb_first.Text;
- if (plength > LegalLength[1] / 8)
- passWord = pass_tb_first.Text.Substring(0, 16);
- if (plength < LegalLength[0] / 8)
- passWord = RepeatStr(pass_tb_first.Text, 5);
- break;
- }
- case "TripleDES":
- {
- if (plength >= LegalLength[0] / 8 && plength <= LegalLength[1] / 8)
- {
- passWord = pass_tb_first.Text;
- if (plength % 2 != 0)
- {
- passWord += passWord.Substring(0, 1);
- plength = passWord.Length;
- }
- }
- if (plength > LegalLength[1] / 8)
- passWord = pass_tb_first.Text.Substring(0, 24);
- if (plength < LegalLength[0] / 8)
- passWord = RepeatStr(pass_tb_first.Text, 16);
- break;
- }
- case "Rijndael":
- {
- passWord = pass_tb_first.Text;
- if (plength < LegalLength[0] / 8)
- passWord = RepeatStr(passWord, 16);
- if (plength < LegalLength[1] / 8)
- passWord = RepeatStr(passWord, 24);
- if (plength < LegalLength[2] / 8)
- passWord = RepeatStr(passWord, 32);
- break;
- }
- }
- return passWord;
- }
- /// <summary>
- /// 得到加密的块
- /// </summary>
- /// <returns>符合要求的块</returns>
- private string GetIV()
- {
- string iv = pass_tb_first.Text;
- int len = iv.Length;
- switch (algrorithmName)
- {
- case "Rijndael":
- {
- if (len < 16)
- iv = RepeatStr(pass_tb_first.Text, 16);
- else
- iv = pass_tb_first.Text.Substring(0, 16);
- break;
- }
- default:
- {
- if (len < 8)
- iv = RepeatStr(pass_tb_first.Text, 8);
- else
- iv = pass_tb_first.Text.Substring(0, 8);
- break;
- }
- }
- return iv;
- }
- /// <summary>
- /// 重复字符串到指定的长度
- /// </summary>
- /// <param name="str">字符串</param>
- /// <param name="length">指定长度</param>
- /// <returns>符合要求字符串</returns>
- private string RepeatStr(string str, int length)
- {
- int lenold = str.Length;
- string temp = str;
- int len = lenold;
- if (len >= length)
- return str;
- while (len < length)
- {
- if (length - len < lenold)
- {
- str += str.Substring(0, length - len);
- len += length - len;
- }
- else
- {
- str += temp;
- len += lenold;
- }
- }
- return str;
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- // 一、密码长度:
- //5 分: 小于等于 4 个字符
- //10 分: 5 到 7 字符
- //25 分: 大于等于 8 个字符
- //二、字母:
- //0 分: 没有字母
- //10 分: 全都是小(大)写字母
- //20 分: 大小写混合字母
- //三、数字:
- //0 分: 没有数字
- //10 分: 1 个数字
- //20 分: 大于等于 3 个数字
- //四、符号:
- //0 分: 没有符号
- //10 分: 1 个符号
- //25 分: 大于 1 个符号
- //五、奖励:
- //2 分: 字母和数字
- //3 分: 字母、数字和符号
- //5 分: 大小写字母、数字和符号
- //最后的评分标准:
- //>= 90: 非常安全
- //>= 80: 安全(Secure)
- //>= 70: 非常强
- //>= 60: 强(Strong)
- //>= 50: 一般(Average)
- //>= 25: 弱(Weak)
- //>= 0: 非常弱
- userPassScore = 0;
- userPassLen = 0;
- userPassLen=pass_tb_first.Text.Length;
- lowerChar = 0;
- upperChar = 0;
- numChar = 0;
- otherChar = 0;
- foreach (char c in pass_tb_first.Text.ToCharArray())
- {
- if (Char.IsLower(c))
- {
- lowerChar++;
- }
- if (Char.IsUpper(c))
- {
- upperChar++;
- }
- if (Char.IsDigit(c))
- {
- numChar++;
- }
- if (Char.IsSymbol(c) || Char.IsPunctuation(c) || Char.IsSeparator(c) || Char.IsWhiteSpace(c))
- {
- otherChar++;
- }
- }
- //长度
- if (userPassLen <= 4 && userPassLen!=0)
- userPassScore = 5;
- if (userPassLen >= 5 && userPassLen <= 7)
- userPassScore = 10;
- if (userPassLen >= 8)
- userPassScore = 25;
- if ((lowerChar != 0 || upperChar != 0) && (userPassLen == lowerChar || userPassLen == upperChar))
- {
- userPassScore += 10;
- }
- if ((lowerChar != 0 ||upperChar != 0 )&& (userPassLen != lowerChar || userPassLen != upperChar))
- {
- userPassScore += 20;
- }
- if (numChar != 0)
- {
- userPassScore += 10;
- if (numChar > 3)
- userPassScore += 20;
- }
- if (otherChar != 0)
- {
- userPassScore += 10;
- if (otherChar > 3)
- userPassScore += 25;
- }
- if (lowerChar != 0 && upperChar != 0 && numChar != 0 && otherChar != 0)
- {
- userPassScore += 5;
- }
- if(userPassScore<25)
- label5.Text = "非常弱";
- if (userPassScore >= 25 && userPassScore < 50)
- {
- label5.Text = "弱";
- label5.ForeColor = System.Drawing.Color.Red;
- }
- if (userPassScore >= 50 && userPassScore < 60)
- {
- label5.Text = "一般";
- label5.ForeColor = System.Drawing.Color.Blue;
- }
- if (userPassScore >= 60 && userPassScore < 70)
- {
- label5.Text = "强";
- label5.ForeColor = System.Drawing.Color.YellowGreen;
- }
- if (userPassScore >= 70 && userPassScore < 80)
- {
- label5.Text = "非常强";
- label5.ForeColor = System.Drawing.Color.LightGreen;
- }
- if (userPassScore >= 80 && userPassScore < 90)
- {
- label5.Text = "安全";
- label5.ForeColor = System.Drawing.Color.GreenYellow;
- }
- if (userPassScore >= 90)
- {
- label5.Text = "非常安全";
- label5.ForeColor = System.Drawing.Color.Green;
- }
- }
- private void pass_tb_first_Enter(object sender, EventArgs e)
- {
- timer1.Enabled = true;
- }
- private void pass_tb_first_Leave(object sender, EventArgs e)
- {
- timer1.Enabled = false;
- }
- }
- }
最后的一点补充说明
加密算法名称 | 密钥位数(KEY) | 初始化向量(IV) |
DES | 64位 | 64位 |
RC2 | 40位到128位,位数递增8 | 64位 |
Rijndael | 128,192,256 | 128--256 每次递增64位 |
TripleDES | 128位到192位 | 64位 |