diff --git a/srcs/encrypt.c b/srcs/encrypt.c index 39b3c9b..d5ba3b2 100644 --- a/srcs/encrypt.c +++ b/srcs/encrypt.c @@ -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++) { printf("block : %x\n", padded[i]); padded[i] = pow_mod(padded[i] + 42 + i, 11317, rsa.n); - printf("encrypted block : %x\n\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("encrypted block : %x\n", padded[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)); printf("\nENCRYPTION : \n"); diff --git a/srcs/rsa.c b/srcs/rsa.c index ada2d73..b9e02de 100644 --- a/srcs/rsa.c +++ b/srcs/rsa.c @@ -23,15 +23,19 @@ int64_t euler(int64_t r0, int64_t r1) { } rsa_t rsa_generate_keys(void) { + + int64_t n = 0; int64_t p = (uint64_t)generate_prime(); int64_t q = (uint64_t)generate_prime(); int64_t ln = (p - 1) * (q - 1); int64_t e = 11317; - while (ln % e == 0 || p == q) { + + while (ln % e == 0 || p == q || !(n & (1 << 31))) { p = generate_prime(); q = generate_prime(); ln = (p - 1) * (q - 1); + n = p * q; } if (q > p) { @@ -40,7 +44,6 @@ rsa_t rsa_generate_keys(void) { q = tmp; } - int64_t n = p * q; int64_t d = euler(e, ln) + ln; if (d > n) { d -= ln;