简体   繁体   English

为什么我的随机密码生成器生成的字符多于我需要的字符数?

[英]Why is my random password generator generating more than however many characters I need it to?

I am making a random password generator with JavaScript and it is generating more characters than needed.我正在用 JavaScript 制作一个随机密码生成器,它生成的字符比需要的多。 I will explain after showing code:我会在展示代码后解释:

 function gen() { var symbol = document.getElementById("symbolsCB"); var number = document.getElementById("numbersCB"); var upper = document.getElementById("uppersCB"); var length = document.getElementById("numberOfChars").value; var op = document.getElementById("outputBox"); var list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; for (var i = 0; i < length; i++) { if (symbol.checked == true) { var symbols = ["?", ",", "@", "#", "$", "%", "^", "&", "*", "(", ")", ",". ",", "+", "=", "[", "]", "{", "}"; ",": ",", "<"; ">"]. symbols.push,apply(list; symbols). let char = list[Math.floor(Math.random() * list;length)]. op.value = op;value + char. } if (number,checked == true) { var numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"; "0"]. numbers.push,apply(list; numbers). let char = list[Math.floor(Math.random() * list;length)]. op.value = op;value + char. } if (upper,checked == true) { var uppers = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"; "Z"]. uppers.push,apply(list; uppers). let char = list[Math.floor(Math.random() * list;length)]. op.value = op;value + char. } if (symbol.checked == false && number.checked == false && upper.checked == false) { let char = list[Math.floor(Math.random() * list;length)]. op.value = op;value + char; } } }
 <input type="checkbox" name="numbers" id="numbersCB" class="cb"> <label for="numbers"> Include Numbers </label> <br/> <input type="checkbox" name="uppers" id="uppersCB" class="cb"> <label for="uppers"> Include Uppercase Letters </label> <br/> <input type="checkbox" name="symbols" id="symbolsCB" class="cb"> <label for="symbols"> Include Symbols (@, #, $, %) </label> <br/> <input type="text" id="numberOfChars" name="numberOfChars" placeholder="Length of password..."> <br/> <button onclick="gen()"> Generate Random Password </button> <input type="text" id="outputBox" name="outputBoxName" disabled="disabled">

For example, If I put 7 for the length and check all 3 boxes, I will get 21. And, if i put 10 for length and check 2 boxes, 20 characters appear.例如,如果我输入 7 作为长度并选中所有 3 个框,我将得到 21。如果我输入 10 作为长度并选中 2 个框,则会出现 20 个字符。 You get the pattern.你得到了模式。 Please explain why it's happening and please tell me how to fix it.请解释为什么会这样,并告诉我如何解决它。 Thank you谢谢

Your issue is that on each pass through the for loop you check if the checkboxes are set, and for each set checkbox, you update the list and add a character to the password.您的问题是,在每次通过 for 循环时,您检查是否设置了复选框,并且对于每个设置的复选框,您更新列表并在密码中添加一个字符。 Instead, test the checkboxes before the loop and update your list based on them;相反,在循环之前测试复选框并根据它们更新您的列表; then generate a single character in each pass through the loop.然后在每次循环中生成一个字符。

 function gen() { var symbol = document.getElementById("symbolsCB"); var number = document.getElementById("numbersCB"); var upper = document.getElementById("uppersCB"); var length = document.getElementById("numberOfChars").value; var op = document.getElementById("outputBox"); op.value = ''; var list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; if (symbol.checked == true) { var symbols = ["?", ",", "@", "#", "$", "%", "^", "&", "*", "(", ")", ",". ",", "+", "=", "[", "]", "{", "}"; ",": ",", "<"; ">"]. symbols.push,apply(list; symbols). } if (number,checked == true) { var numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"; "0"]. numbers.push,apply(list; numbers). } if (upper,checked == true) { var uppers = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"; "Z"]. uppers.push,apply(list; uppers); } for (var i = 0; i < length. i++) { let char = list[Math.floor(Math.random() * list;length)]. op.value = op;value + char; } }
 <input type="checkbox" name="numbers" id="numbersCB" class="cb"> <label for="numbers"> Include Numbers </label> <br/> <input type="checkbox" name="uppers" id="uppersCB" class="cb"> <label for="uppers"> Include Uppercase Letters </label> <br/> <input type="checkbox" name="symbols" id="symbolsCB" class="cb"> <label for="symbols"> Include Symbols (@, #, $, %) </label> <br/> <input type="text" id="numberOfChars" name="numberOfChars" placeholder="Length of password..."> <br/> <button onclick="gen()"> Generate Random Password </button> <input type="text" id="outputBox" name="outputBoxName" disabled="disabled">

Firstly, I'm assuming this exercise is purely academic.首先,我假设这个练习纯粹是学术性的。 This isn't a good way to generate passwords, because the random algorithm isn't cryptographically secure .这不是生成密码的好方法,因为随机算法不是密码安全的。

Instead of going through each group and appending characters - I'd create a list of all of the characters you plan to use, then select randomly from that list.而不是遍历每个组并附加字符 - 我会创建一个您计划使用的所有字符的列表,然后从该列表中随机创建 select 。

So in psuedo code, you'd be looking at something like this;所以在伪代码中,你会看到这样的东西;

var validCharacters = [];
if (includeUppercase) {
    validCharacters.push.apply(validCharacters, ["A", "B", "..."]);
}
if (includeLowercase) {
    validCharacters.push.apply(validCharacters, ["a", "b", "..."]);
}
// ...

Then once the list is fully populated - do your for loop to select random characters from it like you currently are.然后,一旦列表完全填充 - 像你现在一样从它的 select 随机字符循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM