-1

I am trying to solve RSA for 2 small numbers. I can calculate n,phi and e but I always get stuck when I have to calculate d. Please help me with the same. example.

        p = 3,      q = 7,
        n =  3*7 = 21,
        phi(21)= 2*6 = 12, 
        e = 5

        d = (5^-1) (mod 21) 

        or

        d * 5 = k * 12 + 1   (where k is some number)

I tried to figure out the calculation of d * 5 = 25 = 5 * 12 + 1 but this is for small number is there any other way to calculate d with simple approach

Kunj
  • 77
  • 9

1 Answers1

3

The following pseudo code may help you (with heavy borrowing from this link

// choose prime factors:
p = 3;
q = 7;

n = p * q; // =21
phi = (p-1)*(q-1); // = 12
// Choose e such that 1 < e < phi and e and n are coprime:
e = 5; 
// Compute a value for d such that (d * e) % phi = 1. 
// in other words, solve 5 * d % 12 = 1
d = 5; // since 5 * 5 = 25; modulo 12 = 1. How odd: d == e...

Public key is (e, n) => (5, 21) 
Private key is (d, n) => (5, 21) 

Testing this out on a "message" with the value of '2':
The encryption of m = 2 is 
    c = 2^5 % 21 = 32 % 21 = 11 
The decryption of c = 11 is 
    m = 11^5 % 21 = 161051 % 21 = 2

As you can see, we got the "message" back after the encryption / decryption step.

Note that since e==d, this is (unfortunately) a symmetric cypher: if you apply the encryption again, you get back the message as well. This shows that the choice of e was poor. That's a problem with these toy RSA problems...

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Floris
  • 45,857
  • 6
  • 70
  • 122
  • ok thanks for the help... also plz check the following – Kunj Mar 12 '13 at 15:32
  • @Kunj - "plz check the following"??? What do you mean? – Floris Mar 12 '13 at 15:40
  • 1
    ok thanks for the help... also plz check the following m = c^d mod 77 m = 10^37 mod 77 m = (67*67*67*7) mod 77 m = 7 ??? is this correct ? – Kunj Mar 12 '13 at 15:41
  • 1
    You might find [this calculator](https://www.cs.drexel.edu/~jpopyack/IntroCS/HW/RSAWorksheet.html) instructive. How did `10^37 mod 77` turn into `(67*67*67*7)mod 77`? You can use Wolfram Alpha's `PowerMod` [function](http://www.wolframalpha.com/input/?i=PowerMod%5B10%2C37%2C77%5D) -which gives `10` as the answer. – Floris Mar 12 '13 at 16:00