RSA Cryptography

RSA Key Pair

RSA Cryptography encrypts and decrypts data using pair of keys. One is the public key and the other is the private key

  • The public key is used for encryption and verifying digital signatures. It can be distributed to everyone.
  • The private key is used for decryption and creating digital signatures. It must be kept secret and only known to the owner of the key.

RSA public key

  • Consists of a modulus, n, and an exponent, e
  • The modulus is usually generated by multiplying two primes together, although three or four primes can be used
  • The best exponent values, e, are prime numbers close to a power of 2
  • Fermat primes are favoured as values of exponents and take the form 22r + 1
  • The first five fermat primes are: 3 (21+1), 5 (22+1), 17 (24+1), 257 (28+1), 65537 (216+1)
  • 65537 is commonly used as the exponent
  • A data value M, is encrypted using as the value C using the formula

C = Me mod n


RSA private key

  • Consists of the modulus from the public key, n and an exponent, d
  • d is mathematically generated from n and e
  • If n is sufficiently large, then it is almost impossible to determine the value of d
  • The mathematics used to determine d uses elements from number theory, primarily Euler's totient theorem and Bézout's Identity
  • The encoded value C, is deccrypted to M using the formula

M = Cd mod n


Calculating d

d can be calculated from n and e

  1. Find the value of the Euler's totient function; φ(n)
  2. If e is coprime to φ(n), then Bézout's Identity can be used:
    There exists integers d and k such that ed + kφ(n) = 1 mod n
  3. Use Euclid's Extended Algorithm to calculate d

Example: n = 21, e = 17

Calculating φ(n)

  • n = 3 × 7
  • φ(8) = 2 × 6 = 12

Using Bézout's Identity

  • 17d + 12k = 1
  • d = 5 and k = -7

d = 5


Checking for M = 4

  • C = 417 = 16 mod 21
  • C = 16
  • M = 165 = 4 mod 21
  • M = 4

Checking for M = 5

  • C = 517 = 17 mod 21
  • C = 17
  • M = 175 = 5 mod 21
  • M = 5

The Maths

Encrypting

The maths showing how RSA encryption works

Public Key: modulus n, exponent e

Private Key: modulus n, exponent d

  1. Data M encrypted as C

    C = Me mod n
  2. Bézout's Identity

    ed + kφ(n) = 1
    Med + kφ(n) = M
  3. Eulers Totient Theorem

    Mφ(n) = 1 mod n
    Mkφ(n) = 1 mod n
  4. Using the result from 1 and 3

    Med + kφ(n) = Med × Mkφ(n)
    = Med × 1 mod n
    = Med mod n
  5. Combining 2 and 4

    Med = M mod n
  6. Combining 1 and 5

    Med = (Me)d
    = Cd

    M = Cd mod n


Private Key Security

Using Euler's totient function

The public key contains the modulus n, which is usually created from the product or two primes.

If n is known, then it has to be factorised into the primes in oder to determine the value of φ(n)

Using large values of n

In reality, the primes used for RSA encryption are very large and the resulting value of the modulus n should contain at least 2048 bits.

If the public key is known, the the private key can be derived by factorising n into it's prime factors, p and q, to then generate the value of φ(n).

For such large values of n, it is so exceedingly difficult and time consuming to find the prime factors, that it is infeasible to find the private key from the resulting public key.

RSA Encryption and Decryption

This is an educational tool for demonstration purposes only. The primes used to generate the public and private keys offer encryption of between 7 and 28 bits of data.

First Prime Number: p

Second Prime Number: q

Exponent e

Common exponent values : 3 (21+1), 5 (22+1), 17 (24+1), 257 (28+1), 65537 (216+1). These are Fermat primes.

Public Key: n = 15, e = 65537

23 ≤ n < 24

A maximum of 3 bits of data can be encrypted at one time or 0 4-bit chararacters plus a 3-bit character .


Encrypt Data

Letter

Binary Value

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

Enter the data by either clicking on the character buttons or entering a data value less than 8

Data

Data Value

Binary

Private Key: n = 15, d = 1

A maximum value of 14 can be decrypted


Decrypt Data

Hello

Data Value

Binary