C#簡單生成隨機密碼的方法示例
本文實例講述了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;
}//產(chǎn)生隨機的密碼值
}//判斷輸入的密碼是否符合隨機產(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>無標題頁</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="產(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="驗證" /></td>
<td width="100px">
</td>
</tr>
</table>
<br />
<br />
<br />
</div>
</form>
</body>
</html>
PS:這里再為大家提供兩款功能類似的在線工具供大家參考:
在線隨機數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高強度密碼生成器:
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è)計之線程使用技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
欄 目:C#教程
下一篇:C# 日歷類功能的實例代碼
本文標題:C#簡單生成隨機密碼的方法示例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5644.html
您可能感興趣的文章
- 01-10C#實現(xiàn)簡單的Login窗口實例
- 01-10Winform消除button按下出現(xiàn)的虛線簡單實現(xiàn)方法
- 01-10winform簡單緩存類實例
- 01-10C#一個簡單的定時小程序?qū)崿F(xiàn)代碼
- 01-10C#圓角窗體簡單實現(xiàn)方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法
- 01-10C#實現(xiàn)簡單合并word文檔的方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10C#簡單實現(xiàn)子窗體向父窗體傳值的方法
- 01-10基于C#實現(xiàn)簡單離線注冊碼生成與驗證


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法


