C Simple Method Example for Generating Random Passwords

  • 2021-12-21 04:49:07
  • OfStack

In this paper, an example is given to describe the simple method of generating random passwords in C #. Share it for your reference, as follows:


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)
  {
    // Determine the length of the density 
    string tmpstr = "";
    int iRandNum;
    if (pwdlen > pwdchars.Length)
    {
      return " Grow too long! " + 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(" Designate the secret and lose the defeat! ");
    }// Determine whether the specified density length meets the requirements 
    else
    {
      string randomchars = "abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+|0123456789~!@#$%^&*()_+|ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|";
      string password = MakePassword(randomchars, Convert.ToInt32(TextBox1.Text));
      TextBox2.Text = password;
    }// Secret value of mining machine 
  }// Determine whether the secret sources entering the machine conform to the secret sources generated by the machine 
  protected void Button2_Click(object sender, EventArgs e)
  {
    if (TextBox3.Text.Trim()==TextBox2.Text.Trim())
    {
      MessageBox.Show(" Success! ");
      Response.Write(" Your secret is: "+"<font color=red>"+TextBox3.Text+"</font>");
    }
    else
    {
      MessageBox.Show(" Failure! ");
    }
  }
  protected void TextBox1_TextChanged(object sender, EventArgs e)
  {
  }
}

HTML code:


<%@ 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> Untitled page </title>
</head>
<body bgcolor="#66ffcc">
  <form id="form1" runat="server" >
  <div>
    &nbsp; &nbsp; &nbsp;<br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <table bgcolor=SpringGreen>
      <tr>
        <td width="100px">
    <asp:Label ID="Label1" runat="server" Text=" Specify password length (10~20 Between) " 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=" Zengshengmi " /></td>
        <td width="100px">
        </td>
      </tr>
      <tr>
        <td width="100px">
    <asp:Label ID="Label2" runat="server" Text=" Enter the generated password "></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=" Validation " /></td>
        <td width="100px">
        </td>
      </tr>
    </table>
    <br />
    <br />
    <br />
    </div>
  </form>
</body>
</html>

PS: Here are two online tools with similar functions for your reference:

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

High Strength Password Generator:
http://tools.ofstack.com/password/CreateStrongPassword

For more information about C #, you can also see the topics on this site: "C # String Operation Skills Summary", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Use Skills Summary", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: