Java-Generate-Password-Dict-1

Java 穷举生成密码字典

穷举法生成 4位、6位 数字密码

第二个穷举法比较慢,你可以去除一些字符不要的字符加快速度以及减少要生成的位数

我这边设置了 6 导致了我内存空间不够使用,所以看你电脑能不能实现吧

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
import java.io.*;
import java.util.HashSet;

public class PasswordDict {
/**
* 包含了所有字符的 String 数组
* @Author Sevattal
* */
final public static String [] baseStr = {
"0","1","2","3","4","5","6","7","8","9",
"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",
"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",
"!","@","#","$","%","^","&","*","(",")","-","+","_","=","\\","|",":","'",
";","\"","<",">","?",",","."," ","~","`"
};
/**
* 用于存储 密码字典数据
* @Author Sevattal
* */
public HashSet<String> resultSet= new HashSet<>();

/**
* 用于判断当前的长度是否超过
* */
public int passLength = 1;

/**
* 由于生成的密码是数字类型的密码,不做 递归 循环的方式了
* 穷举法生成数据面膜 主要用于暴力破解 4位 6位 PIN 密码
* 这个生成方式比较蠢,用了 6 层 For 循环,
* 对于生成字典来说已经够用了
* 状态为:0 表示 生成 4 位密码 以及 6 位密码
* 状态为:1 表示 只生成 4 位密码
* 状态为:2 表示 只生成 6 位密码
* @Author Sevattal
*/
public void FourOrSixNumberDict(String fileName,int status){
try{
// 定义文件对象
File fileObj = new File(fileName);
// 如果文件存在则删除原文件,重新创建文件
if(fileObj.exists()){
fileObj.delete();
fileObj.createNewFile();
System.out.println("system info: 文件已重新创建");
}
FileOutputStream fos = new FileOutputStream(fileName,true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
BufferedWriter bw = new BufferedWriter(osw);
for(int a = 0 ; a<10; a++){
for(int b = 0; b<10;b++){
for(int c = 0;c<10;c++){
for(int d=0;d<10;d++) {
if (status == 0 || status == 1) {
bw.write(baseStr[a] + baseStr[b] + baseStr[c] + baseStr[d] + "\n");
}
if (status == 0 || status == 2) {
for (int e = 0; e < 10; e++) {
for (int f = 0; f < 10; f++) {
bw.write(baseStr[a] + baseStr[b] + baseStr[c] + baseStr[d] +
baseStr[e] + baseStr[f] + "\n");
}
}
}
}
}
}
}
bw.flush();
osw.flush();
fos.flush();
if (status == 0) {
System.out.println("system info: 4 位、6位密码字典生成 Success");
} else if (status == 1) {
System.out.println("system info: 4 位密码字典生成 Success");
} else if (status == 2) {
System.out.println("system info: 6 位密码字典生成 Success");
}
}catch (FileNotFoundException e){
System.out.println("system info: 打开文件出错");
}catch (IOException e){
System.out.println("system info: 文件读写出错");

}
}

/**
* 穷举法生成 密码
* 要解决 密码穷举的办法,递归
* 解决方法
* 1、在初始的外层 HashSet 中 定义所有的 单个 字符串
* 2、递归中建立 临时的 HashSet,用于存放当前循环获得的字符串
* 3、内层与 数组 进行 循环拼接(判断最大长度,超过则结束),即多加了一位数据,并存储到临时的 HashSet 中
* 4、将当前临时的 HashSet 中的值 添加到 外层 HashSet 成员变量中
* 5、进行下一次递归,传参位 当前临时的 HashSet,最大长度
* */

public void recursionDict(HashSet<String> tempHashSet_1 ,int maxLength){
if(passLength <= maxLength){
// 判断 tempHashSet 是否为 NUll
if (tempHashSet_1 == null) {
tempHashSet_1 = new HashSet<>();
for (int i = 0; i < baseStr.length; i++) {
tempHashSet_1.add(baseStr[i]);
}
resultSet.addAll(tempHashSet_1);
passLength ++;
recursionDict(tempHashSet_1, maxLength);
}else {
HashSet<String> tempHashSet_2 = new HashSet<>();
for (String tempHashSet : tempHashSet_1){
for (int i = 0; i<baseStr.length;i++){
tempHashSet_2.add(tempHashSet + baseStr[i]);
}
}
resultSet.addAll(tempHashSet_2);
passLength ++;
recursionDict(tempHashSet_2, maxLength);
}
} else {
System.out.println("system info: 数据生成完成");
}
}

/**
* 将生成的数据写入到某一文件中
* @Author Sevattal
* */
public void outFile(String fileName){
try {
// 定义文件对象
File fileObj = new File(fileName);
// 如果文件存在则删除原文件,重新创建文件
if (fileObj.exists()) {
fileObj.delete();
fileObj.createNewFile();
System.out.println("system info: 文件已重新创建");
}
FileOutputStream fos = new FileOutputStream(fileName, true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
BufferedWriter bw = new BufferedWriter(osw);
for (String i : resultSet) {
bw.write(i + "\n");
}
bw.flush();
osw.flush();
fos.flush();
System.out.println("system info: 穷举法密码字典生成 Success");
}catch (FileNotFoundException e){
System.out.println("system info: 打开文件出错");
}catch (IOException e){
System.out.println("system info: 文件读写出错");
}
}

public static void main(String[] args) {

PasswordDict pd = new PasswordDict();
// 生成 四位 六位 数字密码字典函数
pd.FourOrSixNumberDict("D:\\FourNumberDict.txt",1);

// 穷举生成密码,由于数据比较多,所以非常的慢
pd.passLength = 1;
// 第一个参数为: null,第二个参数为: 生成密码的最大位数
pd.recursionDict(null,6);
pd.outFile("D:\\MorePassWord.txt");

}

}

Contents
  1. 1. Java 穷举生成密码字典
|