-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguessID.c
398 lines (347 loc) · 12 KB
/
guessID.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <regex.h>
#define MAX_ID_LENGTH 19
#define MAX_CITIES 4000
#define MAX_OPTIONS 1000000
// 全局变量
char id_input[MAX_ID_LENGTH];
char cities[MAX_CITIES][7];
int city_count = 0;
char valid_cities[MAX_CITIES][7];
int valid_city_count = 0;
int birth_year_options[3000];
int birth_year_count = 0;
char birth_month_options[13][3];
int birth_month_count = 0;
char birth_day_options[13][32][3];
int birth_day_count[13];
char tail_options[10000][5];
int tail_count = 0;
char all_options[MAX_OPTIONS][19];
int all_count = 0;
char cache[MAX_OPTIONS][19];
int cache_count = 0;
// 函数声明
int is_valid_id_number(const char* id_number);
void load_cities();
void generate_valid_cities();
void generate_birth_year_options();
void generate_birth_month_options();
void generate_birth_day_options();
void generate_tail_options();
void generate_all_options();
void verify_all_options();
int main() {
printf("请输入身份证号码,用星号 (*) 替代未知数字:");
scanf("%18s", id_input);
if (strlen(id_input) != 18) {
printf(">>> 请输入18位身份证号码,并用星号 (*) 替代缺失的数字。\n");
return 1;
}
int count_asterisks = 0;
for (int i = 0; i < 18; i++) {
if (id_input[i] == '*') count_asterisks++;
}
if (count_asterisks > 18) {
printf(">>> 星号的数量不能超过18个。\n");
return 1;
}
if (count_asterisks == 0) {
printf(">>> 没有缺失的数字。\n");
return 1;
}
printf(">>> 城市代码: %.6s\n", id_input);
printf(">>> 出生年份: %.4s\n", id_input + 6);
printf(">>> 出生月份: %.2s\n", id_input + 10);
printf(">>> 出生日期: %.2s\n", id_input + 12);
printf(">>> 顺序号: %.4s\n", id_input + 14);
load_cities();
generate_valid_cities();
generate_birth_year_options();
generate_birth_month_options();
generate_birth_day_options();
generate_tail_options();
generate_all_options();
clock_t start_time = clock();
verify_all_options();
clock_t end_time = clock();
double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf(">>> 验证完成\n");
printf(">>> 耗时: %f 秒\n", time_spent);
FILE* f = fopen("resultID.txt", "a");
for (int i = 0; i < cache_count; i++) {
fprintf(f, "%s\n", cache[i]);
printf("%s\n", cache[i]);
}
fclose(f);
return 0;
}
int is_valid_id_number(const char* id_number) {
if (strlen(id_number) != 18) return 0;
for (int i = 0; i < 17; i++) {
if (!isdigit(id_number[i])) return 0;
}
int factors[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char id_remainders[] = {'1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2'};
int total_sum = 0;
for (int i = 0; i < 17; i++) {
total_sum += (id_number[i] - '0') * factors[i];
}
int remainder = total_sum % 11;
return id_remainders[remainder] == id_number[17];
}
void load_cities() {
FILE* f = fopen("citycodes.txt", "r");
if (f == NULL) {
printf("无法打开citycodes.txt文件\n");
exit(1);
}
while (fscanf(f, "%6s", cities[city_count]) != EOF && city_count < MAX_CITIES) {
city_count++;
}
fclose(f);
}
void generate_valid_cities() {
regex_t regex;
char city_pattern[7];
strcpy(city_pattern, id_input);
for (int i = 0; i < 6; i++) {
if (city_pattern[i] == '*') city_pattern[i] = '.';
}
if (regcomp(®ex, city_pattern, REG_EXTENDED) != 0) {
printf("正则表达式编译失败\n");
exit(1);
}
for (int i = 0; i < city_count; i++) {
if (regexec(®ex, cities[i], 0, NULL, 0) == 0) {
strcpy(valid_cities[valid_city_count++], cities[i]);
}
}
regfree(®ex);
if (valid_city_count == 0) {
printf(">>> 错误:无效的城市代码\n");
exit(1);
}
printf(">>> 有效的城市代码: ");
for (int i = 0; i < valid_city_count; i++) {
printf("%s ", valid_cities[i]);
}
printf("\n");
}
void generate_birth_year_options() {
char birth_year[5];
strncpy(birth_year, id_input + 6, 4);
birth_year[4] = '\0';
int current_year = 2024; // 假设当前年份为2024
if (strcmp(birth_year, "****") == 0) {
for (int i = 1949; i <= current_year; i++) {
birth_year_options[birth_year_count++] = i;
}
} else {
regex_t regex;
char year_pattern[5];
strcpy(year_pattern, birth_year);
for (int i = 0; i < 4; i++) {
if (year_pattern[i] == '*') year_pattern[i] = '.';
}
if (regcomp(®ex, year_pattern, REG_EXTENDED) != 0) {
printf("正则表达式编译失败\n");
exit(1);
}
char year_str[5];
for (int i = 1949; i <= current_year; i++) {
sprintf(year_str, "%04d", i);
if (regexec(®ex, year_str, 0, NULL, 0) == 0) {
birth_year_options[birth_year_count++] = i;
}
}
regfree(®ex);
}
printf(">>> 有效的出生年份: ");
for (int i = 0; i < birth_year_count; i++) {
printf("%d ", birth_year_options[i]);
}
printf("\n");
}
void generate_birth_month_options() {
char birth_month[3];
strncpy(birth_month, id_input + 10, 2);
birth_month[2] = '\0';
if (strcmp(birth_month, "00") == 0) {
printf(">>> 错误:无效的月份\n");
exit(1);
}
if (strcmp(birth_month, "**") == 0) {
for (int i = 1; i <= 12; i++) {
sprintf(birth_month_options[birth_month_count++], "%02d", i);
}
} else if (birth_month[0] == '*' && birth_month[1] != '*') {
for (int i = 1; i <= 12; i++) {
if (i % 10 == birth_month[1] - '0') {
sprintf(birth_month_options[birth_month_count++], "%02d", i);
}
}
} else if (birth_month[0] != '*' && birth_month[1] == '*') {
for (int i = 10; i <= 12; i++) {
if (i / 10 == birth_month[0] - '0') {
sprintf(birth_month_options[birth_month_count++], "%02d", i);
}
}
} else {
strcpy(birth_month_options[birth_month_count++], birth_month);
}
printf(">>> 有效的出生月份: ");
for (int i = 0; i < birth_month_count; i++) {
printf("%s ", birth_month_options[i]);
}
printf("\n");
}
void generate_birth_day_options() {
char birth_day[3];
strncpy(birth_day, id_input + 12, 2);
birth_day[2] = '\0';
int year_days[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int m = 0; m < birth_month_count; m++) {
int month = atoi(birth_month_options[m]);
if (strcmp(birth_day, "**") == 0) {
for (int d = 1; d <= year_days[month]; d++) {
sprintf(birth_day_options[month][birth_day_count[month]++], "%02d", d);
}
} else if (birth_day[0] == '*' && birth_day[1] != '*') {
for (int d = 1; d <= year_days[month]; d++) {
if (d % 10 == birth_day[1] - '0') {
sprintf(birth_day_options[month][birth_day_count[month]++], "%02d", d);
}
}
} else if (birth_day[0] != '*' && birth_day[1] == '*') {
int start = (birth_day[0] - '0') * 10;
int end = start + 9;
if (end > year_days[month]) end = year_days[month];
for (int d = start; d <= end; d++) {
sprintf(birth_day_options[month][birth_day_count[month]++], "%02d", d);
}
} else {
strcpy(birth_day_options[month][birth_day_count[month]++], birth_day);
}
}
printf(">>> 有效的出生日期: ");
for (int m = 1; m <= 12; m++) {
if (birth_day_count[m] > 0) {
printf("%02d: ", m);
for (int d = 0; d < birth_day_count[m]; d++) {
printf("%s ", birth_day_options[m][d]);
}
printf(", ");
}
}
printf("\n");
}
void generate_tail_options() {
char tail_digits[5];
strncpy(tail_digits, id_input + 14, 4);
tail_digits[4] = '\0';
if (strcmp(tail_digits, "****") == 0) {
for (int i = 0; i < 10000; i++) {
sprintf(tail_options[tail_count++], "%04d", i);
}
} else {
int asterisk_count = 0;
for (int i = 0; i < 4; i++) {
if (tail_digits[i] == '*') asterisk_count++;
}
if (asterisk_count == 0) {
strcpy(tail_options[tail_count++], tail_digits);
} else if (asterisk_count == 1) {
for (int i = 0; i < 10; i++) {
char temp[5];
strcpy(temp, tail_digits);
for (int j = 0; j < 4; j++) {
if (temp[j] == '*') {
temp[j] = '0' + i;
break;
}
}
strcpy(tail_options[tail_count++], temp);
}
} else if (asterisk_count == 2) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
char temp[5];
strcpy(temp, tail_digits);
int replaced = 0;
for (int k = 0; k < 4; k++) {
if (temp[k] == '*') {
temp[k] = replaced == 0 ? '0' + i : '0' + j;
replaced++;
if (replaced == 2) break;
}
}
strcpy(tail_options[tail_count++], temp);
}
}
} else if (asterisk_count == 3) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
char temp[5];
strcpy(temp, tail_digits);
int replaced = 0;
for (int l = 0; l < 4; l++) {
if (temp[l] == '*') {
temp[l] = replaced == 0 ? '0' + i : (replaced == 1 ? '0' + j : '0' + k);
replaced++;
if (replaced == 3) break;
}
}
strcpy(tail_options[tail_count++], temp);
}
}
}
} else {
printf(">>> 错误:无效的后四位数字\n");
exit(1);
}
}
}
void generate_all_options() {
for (int c = 0; c < valid_city_count; c++) {
for (int y = 0; y < birth_year_count; y++) {
for (int m = 0; m < birth_month_count; m++) {
int month = atoi(birth_month_options[m]);
for (int d = 0; d < birth_day_count[month]; d++) {
for (int t = 0; t < tail_count; t++) {
sprintf(all_options[all_count], "%s%04d%s%s%s",
valid_cities[c],
birth_year_options[y],
birth_month_options[m],
birth_day_options[month][d],
tail_options[t]);
all_count++;
if (all_count >= MAX_OPTIONS) {
printf(">>> 警告:超出最大选项数量限制\n");
return;
}
}
}
}
}
}
printf(">>> 数据集大小: %d\n", all_count);
}
void verify_all_options() {
for (int i = 0; i < all_count; i++) {
if (is_valid_id_number(all_options[i])) {
strcpy(cache[cache_count++], all_options[i]);
}
// 每处理1000个选项打印一次进度
if ((i + 1) % 1000 == 0 || i == all_count - 1) {
printf("\r>>> 进度: %.2f%%", (float)(i + 1) / all_count * 100);
fflush(stdout);
}
}
printf("\n");
}