c#随机生成强密码


c#随机生成强密码 至少包含一位数字、一位大写字母和一位小写字母

string chars = "0123456789ABCDEFGHIJKLMNOPQSTUVWXYZabcdefghijklmnpqrstuvwxyz";
Random randrom = new Random(getNewSeed());

string str = "";
for (int j = 0; j < 50; j++)
{
str = "";
for (int i = 0; i < 8; i++)
{
str += chars[randrom.Next(chars.Length)];//randrom.Next(int i)返回一个小于所指定最大值的非负随机数
}
//不符合正则,重新生成
if (!Regex.IsMatch(str, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$"))
{
continue;
}
else {
break;
}
}

 

 

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM