0

パスワードジェネレータに問題があります。

    var listOfCharacters = "abcdefghijklmnopqrstuvwxyz"
    chars = listOfCharacters.ToCharArray();
} // RB: I've reformatted the code, but left this brace in. 
  //     I think it should be removed though... 

string password = string.Empty;
for (int i = 0; i < length; i++) // length = 64
{
    int x = random.Next(0, chars.Length); // chars.Lenght = 26 

    if (!password.Contains(chars.GetValue(x).ToString()))
        password += chars.GetValue(x);
    else
        i--;
 }

 if (length < password.Length) password = password.Substring(0, length); //stucks here at 26 because all the 26 chars from the list are used one time so there are no more chars to use, but i want to use a char more than one time

 return password;

私の問題は、64文字のパスワードを作成したいときに、例26の文字のリストを使用すると、リストから26文字すべてを1回だけ取得するため、26で生成を停止することです。上記のコードでは、1文字を複数取る方法が必要なので、各文字を1回だけでなく、例として、文字「a」を3回取ることができます。

4

4 に答える 4

3

コードには、文字を最大 1 回しか使用しないようにするための明示的なチェックがあります。

if (!password.Contains(chars.GetValue(x).ToString()))
    password += chars.GetValue(x);
else
    i--;

このチェックを外せば大丈夫です!

password += chars.GetValue(x);

編集

あなたが持つべき正確なコードを以下に見つけてください。

for (int i = 0; i < length; i++) // length = 64
{
    int x = random.Next(0, chars.Length); // chars.Lenght = 26 

    password += chars.GetValue(x);
}
于 2013-03-14T09:56:13.617 に答える
1

コードを更新して、すべての文字を一度に追加し、さらに文字が必要な場合 (長さ > 26)、各文字の追加を再度開始します。したがって、最大 26 文字のパスワードには一意の文字が含まれ、最大 52 文字のパスワードには可能な文字がそれぞれ 2 回含まれます。

var listOfCharacters = "abcdefghijklmnopqrstuvwxyz";
var chars = listOfCharacters.ToList();

string password = string.Empty;
for (int i = 0; i < length; i++) {
    int x = random.Next(0, chars.Count);

    password += chars[x];

    chars.RemoveAt(x);
    if (chars.Count == 0)
        chars = listOfCharacters.ToList();
 }

 if (length < password.Length) password = password.Substring(0, length);

 return password;
于 2013-03-14T10:03:36.220 に答える
0

パスワードにまだ追加されていない場合にのみ、文字を追加します。

if (!password.Contains(chars.GetValue(x).ToString()))

26文字すべてが追加されると、それ以上追加されなくなります。

于 2013-03-14T09:55:39.890 に答える
0

これを試して:

string password = string.Empty;

for (int i = 0; i < length; i++)
{
    int x = random.Next(0, chars.Length);

    password += chars.GetValue(x);
}
于 2013-03-14T09:56:23.450 に答える