Efficient random string about C #

zhaozj2021-02-16  47

Using RNGCryptoServiceProvider, the random number of random number generated in one second can be very low, and the method for use in the past is a upgrade, the code is as follows:

Using system.seircurity.cryptography;

Namespace arli.commonprj {///

/// random password /// public sealed class randomstr {

Public const string myversion = "1.2";

/ ******** * const and function * ******** /

Private static readonly int defaultLength = 8;

private static int GetNewSeed () {byte [] rndBytes = new byte [4]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider (); rng.GetBytes (rndBytes); return BitConverter.ToInt32 (rndBytes, 0);}

/ ******** * getrndcode of all char. * ******** /

private static string BuildRndCodeAll (int strLen) {System.Random RandomObj = new System.Random (GetNewSeed ()); string buildRndCodeReturn = null; for (int i = 0; i

Public static string getrndStrofall () {Return BuildRndCodeAll (DefaultLength);

Public Static String GtrndStrofall (int Lenof) {Return BuildrndCodeAll (Lenof);

/ ******** * getRndCode of only * ******** / private static string sCharLow = "abcdefghijklmnopqrstuvwxyz";. Private static string sCharUpp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static string sNumber = "0123456789" ;

private static string BuildRndCodeOnly (string StrOf, int strLen) {System.Random RandomObj = new System.Random (GetNewSeed ()); string buildRndCodeReturn = null; for (int i = 0; i

Public Static String Gtrndstronlyfor (int Lenof) {Return BuildrndCodeonly (Scharlow snumber, lenof);

Public Static String GtrndStronlyfor (Bool Buseupper) {string stramp = schallow; if (buseUpper) stramp = scharupp; if (busenumber) stratmp = snumber;

return BuildRndCodeOnly (strTmp, defaultLength);} public static string GetRndStrOnlyFor (int LenOf, bool bUseUpper, bool bUseNumber) {string strTmp = sCharLow; if (bUseUpper) strTmp = sCharUpp; if (bUseNumber) strTmp = sNumber;

Return buildrndcodeonly (strtmp, lenof);}}}

转载请注明原文地址:https://www.9cbs.com/read-23302.html

New Post(0)