我正在开发一个C#项目,我需要生成随机密码。
任何人都可以为密码生成提供一些代码或高级方法吗?
应该可以指定以下内容:
答案 0 :(得分:23)
您可以使用此方法并根据需要进行修改
private static string CreateRandomPassword(int passwordLength)
{
string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
char[] chars = new char[passwordLength];
Random rd = new Random();
for (int i = 0; i < passwordLength; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new string(chars);
}
答案 1 :(得分:5)
我很久以前写了一个简单的数据生成库来模拟我们需要的一些数据,你可以看看它来获得一些想法。您为它提供了一个您想要生成的模式,它将创建随机数据以匹配该模式。
模式如下:
它不完全是您正在寻找的,但您应该能够根据您的需要进行修改。
This is the main logic
编辑:再次阅读您的要求我认为您应该能够改变我的代码以使其工作。您需要创建一个匹配有效密码的最小字符/数量要求的模式,添加逻辑以通过添加随机字符来改变生成的密码的长度,并且可能在末尾添加一些随机排序逻辑以混合字符这样他们并不总是处于相同的模式。
编辑(2):将代码移动到GitHub,更新了链接。
编辑(3):改为链接到主Github仓库。
答案 2 :(得分:3)
您是否看过codeproject示例?
答案 3 :(得分:2)
<强> A C# Password Generator 强>
namespace WorkingCode.CodeProject.PwdGen
{
using System;
using System.Security.Cryptography;
using System.Text;
public class PasswordGenerator
{
public PasswordGenerator()
{
this.Minimum = DefaultMinimum;
this.Maximum = DefaultMaximum;
this.ConsecutiveCharacters = false;
this.RepeatCharacters = true;
this.ExcludeSymbols = false;
this.Exclusions = null;
rng = new RNGCryptoServiceProvider();
}
protected int GetCryptographicRandomNumber(int lBound, int uBound)
{
// Assumes lBound >= 0 && lBound < uBound
// returns an int >= lBound and < uBound
uint urndnum;
byte[] rndnum = new Byte[4];
if (lBound == uBound-1)
{
// test for degenerate case where only lBound can be returned
return lBound;
}
uint xcludeRndBase = (uint.MaxValue -
(uint.MaxValue%(uint)(uBound-lBound)));
do
{
rng.GetBytes(rndnum);
urndnum = System.BitConverter.ToUInt32(rndnum,0);
} while (urndnum >= xcludeRndBase);
return (int)(urndnum % (uBound-lBound)) + lBound;
}
protected char GetRandomCharacter()
{
int upperBound = pwdCharArray.GetUpperBound(0);
if ( true == this.ExcludeSymbols )
{
upperBound = PasswordGenerator.UBoundDigit;
}
int randomCharPosition = GetCryptographicRandomNumber(
pwdCharArray.GetLowerBound(0), upperBound);
char randomChar = pwdCharArray[randomCharPosition];
return randomChar;
}
public string Generate()
{
// Pick random length between minimum and maximum
int pwdLength = GetCryptographicRandomNumber(this.Minimum,
this.Maximum);
StringBuilder pwdBuffer = new StringBuilder();
pwdBuffer.Capacity = this.Maximum;
// Generate random characters
char lastCharacter, nextCharacter;
// Initial dummy character flag
lastCharacter = nextCharacter = '\n';
for ( int i = 0; i < pwdLength; i++ )
{
nextCharacter = GetRandomCharacter();
if ( false == this.ConsecutiveCharacters )
{
while ( lastCharacter == nextCharacter )
{
nextCharacter = GetRandomCharacter();
}
}
if ( false == this.RepeatCharacters )
{
string temp = pwdBuffer.ToString();
int duplicateIndex = temp.IndexOf(nextCharacter);
while ( -1 != duplicateIndex )
{
nextCharacter = GetRandomCharacter();
duplicateIndex = temp.IndexOf(nextCharacter);
}
}
if ( ( null != this.Exclusions ) )
{
while ( -1 != this.Exclusions.IndexOf(nextCharacter) )
{
nextCharacter = GetRandomCharacter();
}
}
pwdBuffer.Append(nextCharacter);
lastCharacter = nextCharacter;
}
if ( null != pwdBuffer )
{
return pwdBuffer.ToString();
}
else
{
return String.Empty;
}
}
public string Exclusions
{
get { return this.exclusionSet; }
set { this.exclusionSet = value; }
}
public int Minimum
{
get { return this.minSize; }
set
{
this.minSize = value;
if ( PasswordGenerator.DefaultMinimum > this.minSize )
{
this.minSize = PasswordGenerator.DefaultMinimum;
}
}
}
public int Maximum
{
get { return this.maxSize; }
set
{
this.maxSize = value;
if ( this.minSize >= this.maxSize )
{
this.maxSize = PasswordGenerator.DefaultMaximum;
}
}
}
public bool ExcludeSymbols
{
get { return this.hasSymbols; }
set { this.hasSymbols = value;}
}
public bool RepeatCharacters
{
get { return this.hasRepeating; }
set { this.hasRepeating = value;}
}
public bool ConsecutiveCharacters
{
get { return this.hasConsecutive; }
set { this.hasConsecutive = value;}
}
private const int DefaultMinimum = 6;
private const int DefaultMaximum = 10;
private const int UBoundDigit = 61;
private RNGCryptoServiceProvider rng;
private int minSize;
private int maxSize;
private bool hasRepeating;
private bool hasConsecutive;
private bool hasSymbols;
private string exclusionSet;
private char[] pwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFG" +
"HIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:'\",<" +
".>/?".ToCharArray();
}
}
我也找到了
http://www.codeproject.com/KB/cs/password-generator.aspx
http://www.codesnipr.com/snippet/504/c-Random-password-generator
http://www.yetanotherchris.me/home/2009/3/15/c-pronounceable-password-generator.html
答案 4 :(得分:1)
这有帮助吗?你可以用谷歌自己找到它。) C# Password Generator
答案 5 :(得分:1)
从代码项目中取出密码生成器,清理代码并修复Maximum属性设置器中的逻辑错误。
public static class PasswordGenerator
{
private const int DefaultMinimum = 6;
private const int DefaultMaximum = 10;
private const int UBoundDigit = 61;
private readonly static char[] PwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:'\",./?".ToCharArray();
private static readonly RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider();
private static int _minSize=DefaultMinimum;
private static int _maxSize=DefaultMaximum;
public static int Minimum
{
get { return _minSize; }
set
{
_minSize = value;
if (DefaultMinimum > _minSize)
{
_minSize = DefaultMinimum;
}
}
}
public static int Maximum
{
get { return _maxSize; }
set
{
_maxSize = value;
if (_minSize >= _maxSize)
{
_maxSize = _minSize + 2;
}
}
}
public static string Exclusions { get; set; }
public static bool ExcludeSymbols { get; set; }
public static bool RepeatCharacters { get; set; }
public static bool ConsecutiveCharacters { get; set; }
static PasswordGenerator()
{
Minimum = DefaultMinimum;
Maximum = DefaultMaximum;
ConsecutiveCharacters = false;
RepeatCharacters = true;
ExcludeSymbols = false;
Exclusions = null;
}
private static int GetCryptographicRandomNumber(int lBound, int uBound)
{
// Assumes lBound >= 0 && lBound < uBound
// returns an int >= lBound and < uBound
uint urndnum;
var rndnum = new Byte[4];
if (lBound == uBound - 1)
{
// test for degenerate case where only lBound can be returned
return lBound;
}
uint xcludeRndBase = (uint.MaxValue -
(uint.MaxValue % (uint)(uBound - lBound)));
do
{
_rng.GetBytes(rndnum);
urndnum = BitConverter.ToUInt32(rndnum, 0);
} while (urndnum >= xcludeRndBase);
return (int)(urndnum % (uBound - lBound)) + lBound;
}
private static char GetRandomCharacter()
{
var upperBound = PwdCharArray.GetUpperBound(0);
if (ExcludeSymbols)
{
upperBound = UBoundDigit;
}
int randomCharPosition = GetCryptographicRandomNumber(
PwdCharArray.GetLowerBound(0), upperBound);
char randomChar = PwdCharArray[randomCharPosition];
return randomChar;
}
public static string Generate()
{
// Pick random length between minimum and maximum
var pwdLength = GetCryptographicRandomNumber(Minimum,Maximum);
var pwdBuffer = new StringBuilder {Capacity = Maximum};
// Initial dummy character flag
char lastCharacter = '\n';
for (var i = 0; i < pwdLength; i++)
{
var nextCharacter = GetRandomCharacter();
while (nextCharacter == lastCharacter)
{
nextCharacter = GetRandomCharacter();
}
if (false == RepeatCharacters)
{
var temp = pwdBuffer.ToString();
var duplicateIndex = temp.IndexOf(nextCharacter);
while (-1 != duplicateIndex)
{
nextCharacter = GetRandomCharacter();
duplicateIndex = temp.IndexOf(nextCharacter);
}
}
if ((null != Exclusions))
{
while (-1 != Exclusions.IndexOf(nextCharacter))
{
nextCharacter = GetRandomCharacter();
}
}
pwdBuffer.Append(nextCharacter);
lastCharacter = nextCharacter;
}
return pwdBuffer.ToString();
}
}