5 min read
Solving Cryptography Problems - 2

Following up from my previous post, here’s the next problem:

The public key is (n,e)=(221,11)(n, e) = (221, 11). What is the RSA Decryption of 77?

  • What is RSA? wikipedia: RSA (Rivest–Shamir–Adleman) is one of the first public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and distinct from the decryption key which is kept secret (private). In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the “factoring problem”.

  • How does RSA work?
    Let’s look at an example from wikipedia:

  1. Choose two distinct prime numbers, such as
    p=61p = 61 and q=53q = 53
  2. Compute n=pqn = pq giving
    n=61×53=3233n = 61 \times 53 = 3233
  3. Compute the Carmichael’s totient function of the product as
    λ(n)=lcm(p1,q1)\lambda (n)=\operatorname {lcm} (p-1,q-1) giving
    λ(3233)=lcm(60,52)=780\lambda (3233)=\operatorname {lcm} (60,52)=780
  4. Choose any number 1<e<7801 \lt e \lt 780 that is coprime to 780780. Choosing a prime number for ee leaves us only to check that ee is not a divisor of 780780. Let e=17e = 17.
  5. Compute dd, the modular multiplicative inverse of emodλ(n)e \bmod \lambda(n) yielding, d=413d = 413
    Worked example for the modular multiplicative inverse:
    d×e1modλ(n)d\times e \equiv 1{\bmod {\lambda }}(n)
    413×171mod780413\times 17 \equiv 1{\bmod {7}}80

The public key is (n=3233,e=17)(n = 3233, e = 17). For a padded plaintext message m, the encryption function is c(m)=m17mod3233c(m)=m^{17} \bmod 3233

The private key is (n=3233,d=413)(n = 3233, d = 413). For an encrypted ciphertext c, the decryption function is m(c)=c413mod3233m(c)=c^{413} \bmod 3233

For this problem we have nn, ee, and cc. We have to find dd and calculate mm using the above decryption function.
n=221=13×17n = 221 = 13 \times 17. Carmichael’s totient function, λ(221)=lcm(12,16)=48\lambda (221)=\operatorname {lcm} (12,16)=48. We already have e=11e = 11.
dd is the modular multiplicative inverse of emodλ(n)e \bmod \lambda(n) i.e.,
d×e1modλ(n)d \times e \equiv 1 \bmod \lambda (n) or d×111mod48d \times 11 \equiv 1 \bmod 48.

We can write a simple program to calculate d:

d = 0
while (11 * d) % 48 != 1: d+=1
print(d)

35

Now we can find mm as: m=cdmodn=735mod221=54m = c ^ d \bmod n = 7 ^{35} \bmod 221 = 54

Thus, 54 is our final answer.

Further reading:

  • What is Carmichael’s totient function?
    wikipedia: The Carmichael function associates to every positive integer nn a positive integer λ(n)\lambda (n), defined as the smallest positive integer mm such that
    am1modna^m \equiv 1 \bmod n
    for every integer aa between 11 and nn that is coprime to nn.

wikipedia: The exponent of the multiplicative group of integers mod n (previous post), that is, the least common multiple of the orders in the cyclic groups, is given by the Carmichael function λ(n)\lambda (n). In other words, λ(n)\lambda (n) is the smallest number such that for each aa coprime to nn, aλ(n)1(modn)a^{\lambda(n)} \equiv 1 \pmod n holds.

  • How can we find the modular multiplicative inverse using pen and paper? By making use of the Extended Euclidean Algorithm. Here’s a youtube video that walks you through the procedure and here’s a Khan Academy article that explains it well. Adding these here for myself for future reference.

Now let’s try another one:

The public key is (n,e)=(437,13)(n, e) = (437, 13). What is the RSA Digital Signature of 1717?

  • What is RSA Digital Signature?
    This article explained it very well. From the article, here is the “textbook” definition of RSA signing compared with the Encryption and Decryption functions.

    • Enc(m;e)=R(m,e)Enc(m; e) = R(m,e)
    • Dec(c;d)=R(c,d)Dec(c; d) = R(c,d)
    • Sign(m;d)=R(m,d)Sign(m; d) = R(m,d)
    • Ver(m;s;e)=R(s,e)==mVer(m; s; e) = R(s,e) == m

We are basically applying the sender’s private key on the message they are sending to generate a signature. That means, for this problem, we have to find mdmodnm^d \bmod n.

We can follow the procedure from the previous problem now. dd, or the private key, is the modular multiplicative inverse of emodλ(n)e \bmod \lambda (n). Here, λ(n)\lambda (n) is the Carmichael totient function, calculated as λ(n)=lcm(p1,q1)\lambda (n) = lcm (p-1, q-1), where pp and qq are the prime factors of NN.

Through trial and error, we find that the prime factors of n=437n = 437 are 1919 and 2323. Of course this would be a very difficult problem if the prime numbers were sufficiently large; that’s the whole idea behind RSA encryption. Anyways, now that we have pp and qq, we can find λ(437)=lcm(18,22)=198\lambda (437) = lcm (18, 22) = 198. Now, using the same algorithm I used for the previous problem,

d = 0;
while (d * 13) % 198 != 1: d+=1
print(d)

61

Therefore, the solution to this problem is 1761mod437=19517^{61}\bmod 437 = 195.

Until next time!