我需要生成一个随机密码,该密码将在OpenPGP.js中用于对称加密。用户永远不必触摸或看到密码(所有事情都发生在幕后)。因此,理想情况下,密码只是一些随机字节。不幸的是,OpenPGP.js不支持这个(据我所知)。它只支持字符串作为密码。
所以我需要生成一个随机密码字符串。我希望它尽可能的随机,排除尽可能少的字符。
如何生成安全的随机密码字符串?
我现在有这样的情况:
String.fromCharCode.apply(null, crypto.getRandomValues(new Uint8Array(32)));然而,我有点担心,当某些随机字节出现时,它可能会破坏UTF-16代理项对,并且在其他浏览器上可能会根据Unicode实现而对密码进行不同的解释。
这种解决方案在浏览器之间使用安全吗?
发布于 2018-05-22 20:53:25
来回答我自己的问题:
我的系统的一部分加密和解密密码(例如问题中的随机密码)。在用OpenPGP.js测试我的解决方案时,从解密操作(加密后)返回的字符串并不完全等于原始字符串(可能是十分之一)。
这表明,当输入不正确时,OpenPGP.js不正确地序列化或反序列化UTF-8 (或它使用的任何编码)。我猜我正在生成的字符串是无效的Unicode --至少在Chrome中是这样。
很明显,当我定义一个有限的已知字符集时,我就会工作。
总括而言:
String.fromCharCode.apply(null, crypto.getRandomValues(new Uint8Array(32)));使用起来不安全。
发布于 2018-05-22 11:48:53
我会随心所欲地使用这样的东西:
function generatePass() {
var pass = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var passLength = (Math.random() * 15) + 5;
for (var i = 0; i < passLength; i++)
pass += possible.charAt(Math.floor(Math.random() * possible.length));
return pass;
}
console.log(generatePass());
Das anpassen和variieren des Codes sehr leicht und l uftüberall。
发布于 2020-01-13 05:17:16
来看看小提琴
function CreateRandomPassword(Length, isUpperAlpha, isLowerAlpha, isNumaric ,SpecialChars)
{
var _allowedChars = "";
if (isUpperAlpha != false)
_allowedChars += "ABCDEFGHJKLMNOPQRSTUVWXYZ";
if (isLowerAlpha != false)
_allowedChars += "abcdefghijkmnopqrstuvwxyz";
if (isNumaric != false)
_allowedChars += "0123456789";
_allowedChars += SpecialChars;
if(!Length)
Length = 8
var chars = "";
allowedCharCount = _allowedChars.length;
if(allowedCharCount == 0)
return " ";
for (var i = 0; i < Length; i++)
{
chars += _allowedChars[Math.floor(Math.random() * Math.floor(allowedCharCount))];
}
return chars;
}我开发了一个容易生成密码的函数。
https://stackoverflow.com/questions/50465925
复制相似问题