Detailed description of code cases in which C # bulk generation random passwords must contain numbers and letters and encrypted with an encryption algorithm

Source: Internet
Author: User
Tags md5 encryption
This article mainly describes the C # batch generation random password must contain numbers and letters and encryption algorithm encryption, need to refer to the friend

Requirements:

Passwords must contain numbers and letters

Ideas:

1. List numbers and characters. Composition string: Chars

2. Use Randrom. Next (int i) returns a nonnegative random number that is less than the specified maximum value.

3. Randomly take a random number A, not less than chars length, and take the chars character of the string.

4. Loop 8 times, get 8-digit password

5. Loop n times to get the password in bulk.

The code implements the following main function:

static void Main (string[] args)    {      string chars = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ ";      Random randrom = new Random ((int) DateTime.Now.Ticks);      String path1 = @ "C:\Users\lenovo\Desktop\pws.txt";      for (int j = 0; J < 10000;j++)      {        string str = "";        for (int i = 0; i < 8; i++)        {          str + = Chars[randrom. Next (chars. Length)];//randrom. Next (int i) returns a nonnegative random number that is less than the specified maximum value        }        if (Isnumber (str))//Determines whether it is all digital          continue;        if (Isletter (str))//judgment is all the letter          continue;        File.appendalltext (path1, str);        string PWS = Md5 (str,32),//MD5 encryption        file.appendalltext (path1, "," + PWS + "\ r \ n");      }      Console.WriteLine ("OK");      Console.read ();    }

Skillfully use String.Trim function, judge whether all is the number, all is the letter.

Description: String.Trim removes leading and trailing white space characters from a string object.

Returns: a copy of a string in which all whitespace characters are removed from the beginning and end of the string.

There is an overload:string.Trim(params char[] trimChars)

Removes all leading and trailing occurrences of a set of characters specified in an array from the current System.String object

TrimChars: Array of characters to delete

method to implement the following code:

Determines whether the digital    static bool Isnumber (String str)    {      if (str) is all. Trim ("0123456789". ToCharArray ()) = = "")        return true;      return false;    }    Determines whether all the letters are    static bool Isletter (String str)    {      if (str). Trim ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz". ToCharArray ()) = = "")        return true;      return false;    }

With MD5 encryption, the algorithm code is implemented as follows:

<summary>    //MD5 encryption    ///</summary>//<param name= "str" > Cryptographic characters </param>/ <param name= "code" > Encryption bits 16/32</param>///    <returns></returns> public    static String Md5 (string str, int code)    {      string strencrypt = string. Empty;      MD5 MD5 = new MD5CryptoServiceProvider ();      byte[] Fromdata = encoding.getencoding ("GB2312"). GetBytes (str);      byte[] Targetdata = Md5.computehash (fromdata);      for (int i = 0; i < targetdata.length; i++)      {        Strencrypt + = Targetdata[i]. ToString ("X2");      }      if (code = =)      {        Strencrypt = strencrypt.substring (8, +);      }      return strencrypt;    }

Generate bulk passwords, and encrypted passwords such as:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.