0

In my RSA cryptosystem, I am trying to select an encryption key e that is relatively prime to phi(n).

phi(n) is the product of two numbers, p-1 & q-1, where p and q are both prime. Those primes are generated using the naive primality test (yes, I know it's inefficient).

When I execute my program, I get the following output:

***** p *****
878222789

***** q *****
851559637

***** n = p x q *****
3523942633

***** phi(n) = (p-1)x(q-1) *****
1794160208

***** e *****
99

***** d *****
47835

Problem is, every time I run this program, e always ends up equaling 99 or 97. It is the only constant in this output. I am almost positive my GCD algorithm is correct. But it's the only subroutine in select_e().

Relevant functions:

void select_e(uint &e, const uint phi_n)
{ // e = encryption key
    for(int i=3;i<100;i++) {
        if(gcd(phi_n,i) == 1) {
            e = i;
            break;
            }
    }
    printf("\n***** e *****\n%u\n",e);
}

uint gcd(uint a, uint b) {
    uint temp;
    while (b!=0) {
        temp = b;
        b = a%b;
        a = temp;
    }
    return a;
}

EDIT***********************

Okay, so I've added a break statement in select_e() which has caused the program to output a value of usually 3 or 5 for e. Now, as I understand it, the botched decryption is the result of integer overflow from multiplying two 32-bit integers and storing the value in another 32-bit integer. Therefore, I have modified my program to generate smaller p and q with a modulo. Here is that function:

void generate_pq(uint &p, uint &q)
{
    srand(time(NULL));
    bool repeat = false;
    printf("\nGenerating keys...\n");

    while(!repeat) {
        p = rand()%100;
        repeat = isPrime(p);
    }
    repeat = false;
    while(!repeat) {
        q = rand()%100;
        repeat = isPrime(q);
    }
    printf("\n***** p *****\n%u\n\n***** q *****\n%u\n",p,q);
}

The problem now is that sometimes, I luck out and the message is decrypted successfully. Other times it does not work. Here is an example of a botched decryption:

Generating keys...

***** p *****
79

***** q *****
49

***** n = p x q *****
3871

***** phi(n) = (p-1)x(q-1) *****
3744

***** e *****
5

***** d *****
749

Message: fdsa 

Cipher: �/�J

Decrypted Message: �a

I know the decryption and encryption functions work. I have tested them separately and examined the algorithms. They are not the source of the problem. I will provide them for posterity if requested.

7
  • You might want a break inside the if (gcd...)
    – erikkallen
    Dec 1, 2013 at 22:16
  • Still not resolved. select_e() modified to reflect your suggestion. Thanks!! Now e turns out to be 3 or some low integer. Dec 1, 2013 at 22:21
  • i can't remember the details here (is it all modulo something or other?), but in case it's important, there is NO WAY that n above is the product of p and q. just look at the values. Dec 1, 2013 at 22:37
  • @andrewcooke: There is also NO WAY that 99 is 3. I'm guessing the output, of which he states "..I get the following output ...", is the output of nothing but random typing on the keyboard. Dec 1, 2013 at 22:40
  • 2
    49 isn't prime. do i get some thanks this time? Dec 2, 2013 at 0:57

1 Answer 1

2

Well, you select the first possible number greater than two. Since the number of primes factors of any large number is extremely small compared to the number itself, you cannot expect the test if(gcd(phi_n,i) == 1) to fail many times. Maybe it fails once, maybe it fails twice, but I wouldn't even want to wait until it fails ten times...

So the answer would be to randomly pick an i in each iteration of the loop instead of simply counting up.


Btw: Your output is funny in another way: 3523942633 is definitely not the product of 878222788 and 851559636. Not sure why precisely, but it looks like an inadvertent truncation due to the use of 32 bit integers...

3
  • How could I fix this? I tried changing n & phi(n) to uint64_t in my program, but that doesn't work. Dec 2, 2013 at 0:30
  • Added %100 to the p and q I generate, so that they don't cause overflow. Any further advice? Dec 2, 2013 at 0:49
  • 1
    @baph0mt If you want the result of a multiplication not to overflow, you must cast the operands before multiplying, because otherwise the multiplication results in a 32 bit value. I. e. uint32_t a, b; uint64_t result = a*b; truncates while uint32_t a, b; uint64_t result = (uint64_t)a*(uint64_t)b; does not. Dec 2, 2013 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.