fix: rsa key is now always large enough

This commit is contained in:
gbrochar 2024-08-13 10:23:36 +02:00
parent 0f93258a88
commit d72905c887
2 changed files with 7 additions and 4 deletions

View File

@ -38,8 +38,8 @@ unsigned long encrypt(char *file, unsigned long int offset, unsigned long int si
for (size_t i = 0; i < padded_len; i++) { for (size_t i = 0; i < padded_len; i++) {
printf("block : %x\n", padded[i]); printf("block : %x\n", padded[i]);
padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n);
printf("encrypted block : %x\n\n", padded[i]); printf("encrypted block : %x\n", padded[i]);
//printf("decipher block : %lu (%lx)\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i, pow_mod(padded[i], rsa.d, rsa.n) - 42 - i); printf("decipher block : %lx\n\n", pow_mod(padded[i], rsa.d, rsa.n) - 42 - i);
} }
memcpy(&file[offset], padded, padded_len * sizeof(uint32_t)); memcpy(&file[offset], padded, padded_len * sizeof(uint32_t));
printf("\nENCRYPTION : \n"); printf("\nENCRYPTION : \n");

View File

@ -23,15 +23,19 @@ int64_t euler(int64_t r0, int64_t r1) {
} }
rsa_t rsa_generate_keys(void) { rsa_t rsa_generate_keys(void) {
int64_t n = 0;
int64_t p = (uint64_t)generate_prime(); int64_t p = (uint64_t)generate_prime();
int64_t q = (uint64_t)generate_prime(); int64_t q = (uint64_t)generate_prime();
int64_t ln = (p - 1) * (q - 1); int64_t ln = (p - 1) * (q - 1);
int64_t e = 11317; int64_t e = 11317;
while (ln % e == 0 || p == q) {
while (ln % e == 0 || p == q || !(n & (1 << 31))) {
p = generate_prime(); p = generate_prime();
q = generate_prime(); q = generate_prime();
ln = (p - 1) * (q - 1); ln = (p - 1) * (q - 1);
n = p * q;
} }
if (q > p) { if (q > p) {
@ -40,7 +44,6 @@ rsa_t rsa_generate_keys(void) {
q = tmp; q = tmp;
} }
int64_t n = p * q;
int64_t d = euler(e, ln) + ln; int64_t d = euler(e, ln) + ln;
if (d > n) { if (d > n) {
d -= ln; d -= ln;