C # the random password used to generate random strings for batch account application is quite good.

Source: Internet
Author: User

// The main functions of the random string generator are as follows:
// 1. Supports custom String Length
// 2. Support for custom numbers or not
// 3. You can customize whether the bucket contains lowercase letters.
// 4. Whether or not the User-Defined uppercase letters are supported
// 5. Support for custom symbols
// 6. Support for custom character sets
/// <Summary>
/// Generate a random string
/// </Summary>
/// <Param name = "length"> length of the target string </param>
/// <Param name = "useNum"> whether to include a number; 1 = include; default value: Include </param>
/// <Param name = "useLow"> whether it contains lowercase letters; 1 = contained; default value: Include </param>
/// <Param name = "useUpp"> whether to include uppercase letters, 1 = include, default: Include </param>
/// <Param name = "useSpe"> whether it contains special characters; 1 = contained; not included by default </param>
/// <Param name = "custom"> enter a list of custom characters to include. </param>
/// <Returns> A random string of the specified length </returns>
Public string GetRnd (int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
{
Byte [] B = new byte [4];
New System. Security. Cryptography. RNGCryptoServiceProvider (). GetBytes (B );
Random r = new Random (BitConverter. ToInt32 (B, 0 ));
String s = null, str = custom;

If (useNum = true) {str + = "0123456789 ";}
If (useLow = true) {str + = "abcdefghijklmnopqrstuvwxyz ";}
If (useUpp = true) {str + = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";}
If (useSpe = true) {str + = "! \ "# $ % & '() * +,-./:; <=>? @ [\] ^ _ '{| }~ ";}

For (int I = 0; I <length; I ++)
{
S + = str. Substring (r. Next (0, str. Length-1), 1 );
}

Return s;
}

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.