SRE/02-Easy3/genKey.c
2025-03-12 11:12:06 +01:00

83 lines
2.2 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
const uint16_t NBR_PASSWORDS_REQUESTED = 100;
const char TARGET = 'j';
const char list[] = {
'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',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'/', '+'
};
char give_char(uint8_t index){
if (index >= sizeof(list)) {
printf("Error: Index out of bounds, max %zu\n", sizeof(list));
exit(1);
}
return list[index];
}
char sum_pw(char *pw) {
return pw[11] +
pw[10] +
pw[9] +
pw[8] +
pw[7] +
pw[6] +
pw[5] +
pw[4] +
pw[3] +
pw[2] +
pw[1] +
pw[0];
}
int main(int argc, char *argv[]) {
srand(time(NULL));
char password[13];
const uint8_t max_char = sizeof(list);
password[0] = 'r';
password[1] = 'e';
password[2] = 'm';
password[3] = 'i';
for(uint16_t n = 0; n < NBR_PASSWORDS_REQUESTED; n++) {
password[4] = give_char(rand() % max_char);
password[5] = give_char(rand() % max_char);
password[6] = give_char(rand() % max_char);
password[7] = give_char(rand() % max_char);
password[8] = give_char(rand() % max_char);
uint16_t i = 0;
do {
password[9] = give_char(rand() % max_char);
password[10] = give_char(rand() % max_char);
password[11] = give_char(rand() % max_char);
if(i++ > 1000){
password[4] = give_char(rand() % max_char);
password[5] = give_char(rand() % max_char);
password[6] = give_char(rand() % max_char);
password[7] = give_char(rand() % max_char);
password[8] = give_char(rand() % max_char);
i = 0;
}
} while (sum_pw(password) != TARGET);
password[12] = '\0';
if (sum_pw(password) == TARGET) {
printf("%s\n", password);
} else {
printf("Fuck\n");
}
}
return 0;
}