生成没有这些字符“l1o0”的随机密码


Generate Random Password without these characters "l1o0"

我需要生成带有条件的随机密码:

  • 允许使用除">l1o0"以外的所有字符
  • 长度为 8 到 12 个字符

我尝试过的代码:

function generateRandomPassword() {
  //Initialize the random password
  $password = '';
  //Initialize a random desired length
  $desired_length = rand(8, 12);
  for($length = 0; $length < $desired_length; $length++) {
    //Append a random ASCII character (including symbols)
    $password .= chr(rand(32, 126));
  }
  return $password;
}

如何避免这4个字符=>">l1o0"?

原因:

  • 这 4 个字符有时会让用户感到困惑。

谢谢!

请不要使用当前提供的任何其他答案来生成密码。无论如何,它们都不安全。

  • rand() -> 否
  • mt_rand() -> 绝对不是

我将从一篇标题为"如何在PHP中安全地生成随机字符串和整数"的博客文章中提取此解决方案。

/**
 * Note: See https://paragonie.com/b/JvICXzh_jhLyt4y3 for an alternative implementation
 */
function random_string($length = 26, $alphabet = 'abcdefghijklmnopqrstuvwxyz234567')
{
    if ($length < 1) {
        throw new InvalidArgumentException('Length must be a positive integer');
    }
    $str = '';
    $alphamax = strlen($alphabet) - 1;
    if ($alphamax < 1) {
        throw new InvalidArgumentException('Invalid alphabet');
    }
    for ($i = 0; $i < $length; ++$i) {
        $str .= $alphabet[random_int(0, $alphamax)];
    }
    return $str;
}

用法:

// Every ASCII alphanumeric except "loIO01":
$alphabet = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$string = random_string(12, $alphabet);

你可能没有random_int(),除非你将来在 PHP 7 发布时阅读这篇文章。对于我们这些活在当下的人来说,使用random_compat。

试试这个:

function generateRandomPassword($length = 8) {
    $characters = '23456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomPassword = '';
    for ($i = 0; $i < $length; $i++) {
        $randomPassword .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomPassword;
}
你不需要

更改你的代码。 只需使用str_replace替换那些单词的 您可以尝试此解决方案:)。刚刚编辑了您的代码

function generateRandomPassword($length = 8) {
    $characters = '23456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomPassword = '';
    for ($i = 0; $i < $length; $i++) {
        $randomPassword .= $characters[rand(0, $charactersLength - 1)];
    }
    return str_replace(['l','1','o','0'], ['A','B','C','D'], $randomPassword);
}
$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$req_pword_len = 20;
$char_count = 0;
$password='';
$chars=str_split($string);
while ( $char_count < $req_pword_len ) {
    $char = mt_rand(0,61);
    $password .= (string) $chars[$char];
    $char_count++;
}

更改的值

  • $string只是您想要允许的角色
  • $req_pword_len 到所需的密码长度

试试这个:

function generateRandomPassword($length = 8) {
    $randomPassword = '';
    for ($i = 0; $i < $length; $i++) {
      while (true) {
          //remove 0,1,I,O,l,o
          while(in_array(($number = rand(65, 122)), array(48, 49, 73, 79, 108, 111)));         
          if ($number <= 90 or $number >= 97) {
              $randomPassword .= chr($number);                                      
              break ;
           }
     }
  }
    return $randomPassword;
}
echo generateRandomPassword();