fix: rsa key is now always large enough
This commit is contained in:
parent
0f93258a88
commit
d72905c887
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue