当前位置: 首页>编程语言>正文

32位的对称加密算法 对称加密解密算法

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

首先是程序的运行界面。

32位的对称加密算法 对称加密解密算法,32位的对称加密算法 对称加密解密算法_解密,第1张

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


    1. using
    2. using
    3. using
    4. using
    5. using
    6. namespace
    7. {
    
    8. class
    9.     {
    
    10. private
    11. private
    12. private
    13. private const int
    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
    28.         {
    
    29. int[] Int_Key = new int[4] { 0,0,0,0};           
    30. switch
    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
    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
    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
    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
    63.                     } 
    64.             }
    65. return
    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
    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
    91.         {
    
    92. //建立文件流用来处理源文件与目的文件 
    93. new
    94. new
    95.             fout.SetLength(0);
    96. 
    97. //建立变量用来处理文件的读写 
    98. byte[] bin = new byte[per_length]; //加密的长度 
    99. long rdlen = 0;              //读取数据的总长度 
    100. long totlen = fin.Length;    //输入文件的总长度 
    101. int len;                    //每次读写数据的长度 
    102.             
    103. new
    104. 
    105. //开始加密的过程 
    106. while
    107.             {
    
    108.                 len = fin.Read(bin, 0, per_length);
    109.                 encStream.Write(bin, 0, len);
    110.                 rdlen = rdlen + len;               
    111.             }
    112.             encStream.Flush();
    113.             encStream.Close();
    114.             fout.Close();
    115.             fin.Close();         
    116.         }
    117. /// <summary> 
    118. /// 解密文件的算法 
    119. /// </summary> 
    120. /// <param name="inName">源文件名</param> 
    121. /// <param name="outName">目标文件名</param> 
    122. public void
    123.         {
    
    124. //建立文件流用来处理源文件与目的文件,源文件是加密后的文件 
    125. new
    126. new
    127.             fout.SetLength(0);
    128. 
    129. //建立变量用来处理文件的读写 
    130. byte[] bin = new byte[per_length]; //加密的长度 
    131. long rdlen = 0;              //读取数据的总长度 
    132. long totlen = fin.Length;    //输入文件的总长度 
    133. int len;                    //每次读写数据的长度 
    134. 
    135. new
    136. 
    137. //开始解密的过程 
    138. while
    139.             {
    
    140.                 len = fin.Read(bin, 0, per_length);
    141.                 decStream.Write(bin, 0, len);
    142.                 rdlen = rdlen + len;
    143.             }
    144.             decStream.Flush();
    145.             decStream.Close();            
    146.             fout.Close();
    147.             fin.Close();         
    148.         }
    149. 
    150. 
    151.     }
    152. }


    下面是界面的设计

    32位的对称加密算法 对称加密解密算法,32位的对称加密算法 对称加密解密算法_解密_02,第2张

     

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

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

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

    控件类别

    控件名称

    控件的用途

    下拉式列表框

    pass_cb_style

    用来选择加密的算法

    文本框

    pass_tb_first

    让用户输入密码

    文本框

    pass_tb_second

    让用户输入密码,验证密码

    按钮

    pass_but_encrty

    加密按钮

    按钮

    pass_but_decrty

    解密按钮

    标签

    label5

    指示密码强度

    复选框

    del_file_checkBox

    是否删除源文件复选框

     

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

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

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


    最后的一点补充说明

    加密算法名称

    密钥位数(KEY)

    初始化向量(IV)

    DES

    64位

    64位

    RC2

    40位到128位,位数递增8

    64位

    Rijndael

    128,192,256

    128--256 每次递增64位

    TripleDES

    128位到192位

    64位


    https://www.xamrdz.com/lan/5ng1938521.html

    相关文章: