PHP批量生成密码功能

algorain

平时注册账号的时候需要设置密码,而普通的密码太过容易被暴力破解,所以想着自己写一个能生成密码的功能,可以自己选择密码所包含的字符类型,密码的长度等,并且还加入了密码强度检测功能,基本可以实现一些小的需求。我已经将功能放到博客中,在顶部的扩展功能即可访问。

2017-4-26进行了一些更新

1. 文本框和多选框可以保存上一次输入的值,不必每次重新输入。

2.过滤字符功能也可以使用,直接输入要过滤的字符串即可。

3.生成密码字符串放到了文本框中,可以更好的复制和查看。

4. 重新写了整个算法,以前是直接随机生成字符串,而可能导致有些字符类型没有出现在字符串中,使得密码强度降低。这次我先计算出每种字符类型出现的比例,根据比例生成固定的字符串,每个字符串都会先按照字符的比例随机填充,如果不够就重新运行一遍,并且对要过滤的字符串做了处理,这样就保证了所有字符类型都会出现。然后再对固定的字符串的位置进行随机分配,这样生成的密码更加合理,安全。

这是效果图

下面是源代码,有需要的可以参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<style>
span{
font-size: 14px;
}
li{
list-style: none;
/*text-align: center;*/
}
.state{
font-size: 12px;
color: red;
}
.content{
width: 700px;
height: 200px;
margin: auto;
/*text-align: center;*/
}
.left{
widows: 400px;
height: 200px;
float: left;
}
.right{
width: 300px;
height:200px;
float: left;
}
.footer{
width: 700px;
height: 500px;
float: right;
}
</style>
<div class="content">
<h1 align="center">密码生成功能</h1>
<div>
<form action="test.php" method="post">
<div class="left">
<div>
1. 密码长度(<span class="state">默认为6位</span>):
</div>
<div>
2. 要生成的密码数量(<span class="state">默认为一个</span>):
</div>
<div>
3. 密码字符类型(<span class="state">默认为全选</span>):
</div>
<div>
4. 要过滤的字符:
</div>
<br>
<div align="center"><input type="submit" value="生成"></div>
</div>
<div class="right">
<input type="hidden" name="flag" value="flag">
<div><input type="number" name="length" min="6" max="20" size="5" value="<?php if(empty($_POST['flag'])){echo '6';}else{echo $_POST['length'];}?>"></div>
<div><input type="number" min="1" size="5" max="20" name="number" value="<?php if(empty($_POST['flag'])){echo '1';}else{echo $_POST['number'];}?>" ></div>
<div><input type="checkbox" <?php if(empty($_POST['flag'])){echo 'checked';}else{$type = $_POST['type'];if(in_array('1',$type)){echo 'checked';}}?> name="type[]" value="1">A-Z
<input type="checkbox" <?php if(empty($_POST['flag'])){echo 'checked';}else{$type = $_POST['type'];if(in_array('20',$type)){echo 'checked';}}?> name="type[]" value="20">a-z
<input type="checkbox" <?php if(empty($_POST['flag'])){echo 'checked';}else{$type = $_POST['type'];if(in_array('40',$type)){echo 'checked';}}?> name="type[]" value="40">9-0
<input type="checkbox" <?php if(empty($_POST['flag'])){}else{$type = $_POST['type'];if(in_array('80',$type)){echo 'checked';}}?> name="type[]" value="80">!@#$%&*/.</div>
<div><input type="text" size="5" value="<?php if(empty($_POST['flag'])){echo '';}else{ if(empty($_POST['filter'])){echo '';}else{echo $_POST['filter'];}}?>" name="filter" ></div>
<br>
<div ><input type="reset" value="重置"></div>
</div>
</form>
<div class="footer">
<?php
// echo "4324";
// $type = $_POST['type'];
// $text = '43324';
// echo strlen($text);
// echo rand(1,20);
// exit();
// echo "12321";
if(empty($_POST['flag'])){
exit();
}else{
$number = $_POST['number'];
$length = $_POST['length'];
if(empty($_POST['filter'])){
$filter = '';
}else{
$filter = $_POST['filter'];
}
$type = $_POST['type'];
$A_letter = 'ABCDEFGHIJKLMNOPQISTUVWXYZ';
$a_letter = 'abcdefghijklmnopqistuvwxyz';
$S_figure = '0123456789';
$S_string = '!@#$%&*/.';
if($number<1) $number = 1;
if($length<6) $length = 6;
$size = sizeof($type);
//计算字符比例
$scale = $length / $size;
// echo (int)$scale;
$scale = (int)$scale;
$text_arr = array();
/**
*
* 根据比例生成固定的字符串,每个字符串都会先按照字符的比例随机填充,如果不够就重新运行一遍
* 并且对要过滤的字符串做了处理
**/
for($l=1;$l<=$number;$l++){
$text = '';
$sum = 0;
while(1){
if($sum==$length){
break;
}
for($i=1;$i<=$size;$i++){
if($sum==$length){
break;
}
if($type[$i-1]==1){
for ($j=1;$j<=$scale;$j++){
$rand_str = $A_letter[rand(0,25)];
while(1){
if(strpos($filter,$rand_str)===FALSE){
break;
}
$rand_str = $A_letter[rand(0,25)];
}
$text = $text.$rand_str;
$sum = $sum + 1;
if($sum==$length){
break;
}
}
}else if($type[$i-1]==20){
for ($j=1;$j<=$scale;$j++){
$rand_str = $a_letter[rand(0,25)];
while(1){
if(strpos($filter,$rand_str)===FALSE){
break;
}
$rand_str = $a_letter[rand(0,25)];
}
$text = $text.$rand_str;
$sum = $sum + 1;
if($sum==$length){
break;
}
}
}else if($type[$i-1]==40){
for ($j=1;$j<=$scale;$j++){
$rand_str = $S_figure[rand(0,9)];
while(1){
if(strpos($filter,$rand_str)===FALSE){
break;
}
$rand_str = $S_figure[rand(0,9)];
}
$text = $text.$rand_str;
$sum = $sum + 1;
if($sum==$length){
break;
}
}
}else if($type[$i-1]==80){
for ($j=1;$j<=$scale;$j++){
$rand_str = $S_string[rand(0,8)];
while(1){
if(strpos($filter,$rand_str)===FALSE){
break;
}
$rand_str = $S_string[rand(0,8)];
}
$text = $text.$rand_str;
$sum = $sum + 1;
if($sum==$length){
break;
}
}
}
}
}
$text_arr[$l-1] = $text;
}
// var_dump($text_arr);
/**
* 使用得到的字符串来随机位置
**/
// $text = '';
// $text[2] = '1';
// if(!isset($text[2])) echo "34";
echo '<h3 align="">生成结果</h3>';
for ($i=1;$i<=$number;$i++){
// echo $text_arr[$i-1];
$text = '';
for ($j=1;$j<=$length;$j++){
while(1){
$rand_num = rand(0,50);
if(!isset($text[$rand_num])){
$text[$rand_num] = $text_arr[$i-1][$j-1];
break;
}
}
}
$password = '';
for($l=0;$l<=50;$l++){
if(isset($text[$l])){
$password = $password.$text[$l];
}
}
echo $i.': <textarea rows="1" cols="20" style="font-size="11px";">'.$password.'</textarea> 密码强度:';
$pass_num = testPassword($password);
echo $pass_num.'/5 ';
if($pass_num==1){
echo '<span style="color: maroon">检测提示:密码过于简单(很容易就能破解你的密码)</span><br>';
}else if($pass_num==2){
echo '<span style="color: red">检测提示:密码强度差(建议在设置复杂点)</span><br>';
}else if($pass_num==3){
echo '<span style="color: #98bc1b">检测提示:密码强度中(呵呵,标准安全密码)</span><br>';
}else if($pass_num==4){
echo '<span style="color: green">检测提示:密码强度高(你的密码很安全)</span><br>';
}else if($pass_num==5){
echo '<span style="color: blue">检测提示:密码强度极高(暴力破解需要1万年以上)</span><br>';
}
}

// if($sum==1){
// $text = $A_letter;
// }else if($sum==20){
// $text = $a_letter;
// }else if($sum==40){
// $text = $S_figure;
// }else if($sum==80){
// $text = $S_string;
// }else if($sum==21){
// $text = $A_letter.$a_letter;
// }else if($sum==41){
// $text = $A_letter.$S_figure;
// }else if($sum==81){
// $text = $A_letter.$S_string;
// }else if($sum==61){
// $text = $A_letter.$a_letter.$S_figure;
// }else if($sum==101){
// $text = $A_letter.$a_letter.$S_string;
// }else if($sum==121){
// $text = $A_letter.$S_figure.$S_string;
// }else if($sum==60){
// $text = $a_letter.$S_figure;
// }else if($sum==100){
// $text = $a_letter.$S_string;
// }else if($sum==140){
// $text = $a_letter.$S_figure.$S_string;
// }else if($sum==120){
// $text = $S_figure.$S_string;
// }else{
// $text = $A_letter.$a_letter.$S_figure.$S_string;
// }




// $str_num = strlen($text) - 1;
//
// for($i=1;$i<=$number;$i++){
// $password = '';
// for($j=1;$j<=$length;$j++){
// $rand_num = rand(1,$str_num);
// $password = $password.$text[$rand_num];
// }
// echo $i.': '.$password.' 密码强度:';
// $pass_num = testPassword($password);
// echo $pass_num.'/5 ';
// if($pass_num==1){
// echo '<span style="color: maroon">检测提示:密码过于简单(只要花点时间,就能破解你的密码了)</span><br>';
// }else if($pass_num==2){
// echo '<span style="color: red">检测提示:密码强度差(建议在设置复杂点)</span><br>';
// }else if($pass_num==3){
// echo '<span style="color: #98bc1b">检测提示:密码强度良好(呵呵,标准安全密码了)</span><br>';
// }else if($pass_num==4){
// echo '<span style="color: green">检测提示:密码强度高(嘎嘎,你的密码很安全了)</span><br>';
// }else if($pass_num==5){
// echo '<span style="color: blue">检测提示:密码强度极高(啊~~,暴力破解你的密码至少要1万年以上)</span><br>';
// }
// }
// echo $type[0];
// echo implode('.',$type);
exit();
}
/**
*
* @检测密码强度
* @param string $password
* @return int
* @edit www.jbxue.com
*/
function testPassword($password)
{
if ( strlen( $password ) == 0 )
{
return 1;
}
$strength = 0;
/*** get the length of the password ***/
$length = strlen($password);
/*** check if password is not all lower case ***/
if(strtolower($password) != $password)
{
$strength += 1;
}
/*** check if password is not all upper case ***/
if(strtoupper($password) == $password)
{
$strength += 1;
}
/*** check string length is 8 -15 chars ***/
if($length >= 8 && $length <= 15)
{
$strength += 1;
}
/*** check if lenth is 16 - 35 chars ***/
if($length >= 16 && $length <=35)
{
$strength += 2;
}
/*** check if length greater than 35 chars ***/
if($length > 35)
{
$strength += 3;
}
/*** get the numbers in the password ***/
preg_match_all('/[0-9]/', $password, $numbers);
$strength += count($numbers[0]);
/*** check for special chars ***/
preg_match_all('/[!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars);
$strength += sizeof($specialchars[0]);
/*** get the number of unique chars ***/
$chars = str_split($password);
$num_unique_chars = sizeof( array_unique($chars) );
$strength += $num_unique_chars * 2;
/*** strength is a number 1-10; ***/
$strength = $strength > 99 ? 99 : $strength;
$strength = floor($strength / 10 + 1);
return $strength;
}
/*** 调用示例 ***/
?>
</div>
</div>
</div>
</body>
</html>
  • Title: PHP批量生成密码功能
  • Author: algorain
  • Created at: 2017-04-27 09:21:14
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/04/27/php-article72/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments