本文实例讲述了C#简单生成随机密码的方法。分享给大家供大家参考,具体如下:
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Windows.Forms;public partial class _Default : System.Web.UI.Page{ public static int counter = 0; protected void Page_Load(object sender, EventArgs e) { } public static string MakePassword(string pwdchars, int pwdlen) { //判 密 的 度 string tmpstr = ""; int iRandNum; if (pwdlen > pwdchars.Length) { return "长度过长!" + pwdchars.Length; } Random rnd = new Random(); for (int i = 0; i < pwdlen; i++) { iRandNum = rnd.Next(pwdchars.Length); tmpstr += pwdchars[iRandNum]; } return tmpstr; } protected void Button1_Click(object sender, EventArgs e) { int t1 = Convert.ToInt32(TextBox1.Text); if (t1 < 10 || t1 > 20 || t1 < 0) { MessageBox.Show("指定密 失 !"); }//判 指定的密 度是否符合 else { string randomchars = "abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+|0123456789~!@#$%^&*()_+|ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|"; string password = MakePassword(randomchars, Convert.ToInt32(TextBox1.Text)); TextBox2.Text = password; }// 生 的密 值 }//判 入的密 是否符合 生的密 protected void Button2_Click(object sender, EventArgs e) { if (TextBox3.Text.Trim()==TextBox2.Text.Trim()) { MessageBox.Show("成功!"); Response.Write("您 在的密 是:"+"<font color=red>"+TextBox3.Text+"</font>"); } else { MessageBox.Show("失败!"); } } protected void TextBox1_TextChanged(object sender, EventArgs e) { }}
HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server"> <title>无标题页</title></head><body bgcolor="#66ffcc"> <form id="form1" runat="server" > <div> <br /> <br /> <br /> <br /> <br /> <br /> <table bgcolor=SpringGreen> <tr> <td width="100px"> <asp:Label ID="Label1" runat="server" Text="指定密码长度(10~20之 )" Width="129px"></asp:Label></td> <td width="159px"> <asp:TextBox ID="TextBox1" runat="server" Width="39px" OnTextChanged="TextBox1_TextChanged">10</asp:TextBox> <asp:TextBox ID="TextBox2" runat="server" Width="195px" ReadOnly="true"></asp:TextBox></td> <td width="103px"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" 生密 " /></td> <td width="100px"> </td> </tr> <tr> <td width="100px"> <asp:Label ID="Label2" runat="server" Text="输入产生的密码"></asp:Label></td> <td width="159px"> <asp:TextBox ID="TextBox3" runat="server" Width="261px" TextMode="Password"></asp:TextBox></td> <td width="103px"> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="验证" /></td> <td width="100px"> </td> </tr> </table> <br /> <br /> <br /> </div> </form></body></html>
PS:这里再为大家提供两款功能类似的在线工具供大家参考:
在线随机数字/字符串生成工具:
http://tools.VeVB.COm/aideddesign/suijishu
高强度密码生成器:
http://tools.VeVB.COm/password/CreateStrongPassword
更多关于C#相关内容还可查看本站专题:《C#字符串操作技巧总结》、《C#数据结构与算法教程》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#程序设计之线程使用技巧总结》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。
新闻热点
疑难解答