C#簡(jiǎn)單生成隨機(jī)密碼的方法示例
本文實(shí)例講述了C#簡(jiǎn)單生成隨機(jī)密碼的方法。分享給大家供大家參考,具體如下:
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) { //判斷隨機(jī)密碼的長(zhǎng)度 string tmpstr = ""; int iRandNum; if (pwdlen > pwdchars.Length) { return "長(zhǎng)度過長(zhǎng)!" + 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("指定密碼失??!"); }//判斷指定的密碼長(zhǎng)度是否符合 else { string randomchars = "abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+|0123456789~!@#$%^&*()_+|ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|"; string password = MakePassword(randomchars, Convert.ToInt32(TextBox1.Text)); TextBox2.Text = password; }//產(chǎn)生隨機(jī)的密碼值 }//判斷輸入的密碼是否符合隨機(jī)產(chǎn)生的密碼 protected void Button2_Click(object sender, EventArgs e) { if (TextBox3.Text.Trim()==TextBox2.Text.Trim()) { MessageBox.Show("成功!"); Response.Write("您現(xiàn)在的密碼是:"+"<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>無標(biāo)題頁</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="指定密碼長(zhǎng)度(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="產(chǎn)生密碼" /></td> <td width="100px"> </td> </tr> <tr> <td width="100px"> <asp:Label ID="Label2" runat="server" Text="輸入產(chǎn)生的密碼"></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="驗(yàn)證" /></td> <td width="100px"> </td> </tr> </table> <br /> <br /> <br /> </div> </form> </body> </html>
PS:這里再為大家提供兩款功能類似的在線工具供大家參考:
在線隨機(jī)數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高強(qiáng)度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#字符串操作技巧總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密
- C#生成指定范圍內(nèi)的不重復(fù)隨機(jī)數(shù)
- 用C#生成不重復(fù)的隨機(jī)數(shù)的代碼
- C#中獲取、生成隨機(jī)數(shù)的三種方法
- C#生成不重復(fù)隨機(jī)字符串類
- c#循環(huán)中產(chǎn)生偽隨機(jī)數(shù)
- C# Guid.NewGuid獲得隨機(jī)數(shù)
- C#生成互不相同隨機(jī)數(shù)的實(shí)現(xiàn)方法
- C# 生成驗(yàn)證碼取隨機(jī)數(shù)字加字母(改進(jìn)版)
- C#隨機(jī)生成不重復(fù)字符串的兩個(gè)不錯(cuò)方法
- C#生成隨機(jī)字符串的實(shí)例
相關(guān)文章
C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#/VB.NET實(shí)現(xiàn)將XML轉(zhuǎn)為PDF
可擴(kuò)展標(biāo)記語言(XML)文件是一種標(biāo)準(zhǔn)的文本文件,它使用特定的標(biāo)記來描述文檔的結(jié)構(gòu)以及其他特性。本文將利用C#實(shí)現(xiàn)XML文件轉(zhuǎn)PDF?,需要的可以參考一下2022-03-03C#實(shí)現(xiàn)將類的內(nèi)容寫成JSON格式字符串的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將類的內(nèi)容寫成JSON格式字符串的方法,涉及C#針對(duì)json格式數(shù)據(jù)轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08