-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
132 lines (110 loc) · 3.1 KB
/
main.c
File metadata and controls
132 lines (110 loc) · 3.1 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to generate all combinations of passwords
void generatePasswords(int min_length, int max_length, const char *chars, const char *filename)
{
FILE *file = fopen(filename, "w");
if (!file)
{
printf("File could not be created. Check the file path and try again.\n");
return;
}
int chars_len = strlen(chars);
// Generate combinations for each length
for (int length = min_length; length <= max_length; length++)
{
int *indices = calloc(length, sizeof(int));
if (!indices)
{
printf("Memory allocation failed.\n");
fclose(file);
return;
}
while (1)
{
// Generate a password based on the indices
char *password = calloc(length + 1, sizeof(char));
if (!password)
{
printf("Memory allocation failed.\n");
free(indices);
fclose(file);
return;
}
for (int i = 0; i < length; i++)
{
password[i] = chars[indices[i]];
}
fprintf(file, "%s\n", password);
free(password);
// Increment indices
int i;
for (i = length - 1; i >= 0; i--)
{
if (indices[i] < chars_len - 1)
{
indices[i]++;
break;
}
indices[i] = 0;
}
if (i < 0)
break; // If all combinations are exhausted
}
free(indices);
}
fclose(file);
printf("All password combinations have been generated and saved to %s\n", filename);
}
int main()
{
int min_length, max_length;
char chars[256];
char filename[256];
printf("Enter minimum password length: ");
if (scanf("%d", &min_length) != 1)
{
printf("Invalid input for minimum password length.\n");
return 1;
}
printf("Enter maximum password length: ");
if (scanf("%d", &max_length) != 1)
{
printf("Invalid input for maximum password length.\n");
return 1;
}
if (min_length > max_length)
{
printf("Error: Minimum password length cannot be greater than maximum password length.\n");
return 1;
}
printf("Enter characters to use for password (leave blank for all): ");
getchar(); // Consume the newline left by scanf
fgets(chars, sizeof(chars), stdin);
// Remove newline character from fgets
size_t len = strlen(chars);
if (len > 0 && chars[len - 1] == '\n')
{
chars[len - 1] = '\0';
}
if (strlen(chars) == 0)
{
strcpy(chars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;':,.<>?/~`"); // Default chars
}
printf("Enter filename to save passwords (e.g. pass.txt): ");
fgets(filename, sizeof(filename), stdin);
// Remove newline character from fgets
len = strlen(filename);
if (len > 0 && filename[len - 1] == '\n')
{
filename[len - 1] = '\0';
}
if (strlen(filename) == 0)
{
strcpy(filename, "pass.txt");
printf("No filename provided. Using default name: pass.txt\n");
}
generatePasswords(min_length, max_length, chars, filename);
return 0;
}