C # generate random passwords in batches, which must contain numbers and letters and be encrypted using encryption algorithms,

Source: Internet
Author: User

C # generate random passwords in batches, which must contain numbers and letters and be encrypted using encryption algorithms,

Required: the password must contain numbers and letters.

Idea: 1. List numbers and characters. Composition string: chars

2. Use randrom. Next (int I) to return a non-negative random number smaller than the specified maximum value.

3. Random Number a, which is not less than the chars length, and the character a of the character string chars.

4. Loop 8 times to get the 8-bit password

5. Loop N times to obtain passwords in batches.

The Code implements the following Main function:

Static void Main (string [] args) {string chars = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 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 non-negative random number that is less than the specified maximum value} if (IsNumber (str) // determines whether all are numerical continue; if (IsLetter (str )) // determine whether all are letters continue; File. appendAllText (path1, str); string pws = Md5 (str, 32); // MD5 encrypted File. appendAllText (path1, "," + pws + "\ r \ n");} Console. writeLine ("OK"); Console. read ();}

 

Use the String. trim function to determine whether all numbers are full of letters.

Description: string. trim slaveStringRemoves leading and trailing white spaces from the object.

Return: a copy of the string. All white spaces are removed from the beginning and end of the string.

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

// Remove all leading and trailing matches of the specified group of characters from the current System. string object

TrimChars: array of characters to be deleted

The following code is implemented:

// Determine whether all the numbers are static bool IsNumber (string str) {if (str. 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 ;}

Use MD5 encryption. The algorithm code is implemented as follows:

/// <Summary> /// MD5 encryption /// </summary> /// <param name = "str"> encrypted character </param> /// <param name = "code"> 16/32 bits </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 = 16) {strEncrypt = strEncrypt. substring (8, 16);} return strEncrypt ;}


Generate a batch password and an encrypted password, for example:

 

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.