DEV Community

Cover image for RSA & How to Create encryption key?
Ankur Rohilla
Ankur Rohilla

Posted on

RSA & How to Create encryption key?

RSA (Rivest–Shamir–Adleman) is a public-key cryptosystem that is widely used for secure data transmission.
RSA method is a widely used encryption algorithm. You cannot discuss cryptography without at least some discussion of RSA. This public key method was developed in 1977 by three mathematicians: Ron Rivest, Adi Shamir, and Len Adleman. The name RSA is derived from the first letter of each mathematician’s last name. One significant advantage of RSA is that it is a public key encryption method. That means there are no concerns with distributing the keys for the encryption. However, RSA is much slower than symmetric ciphers. In fact, in general, asymmetric ciphers are slower than symmetric ciphers.

The steps to create the key are as follow :

  1. Generate two large random primes, p and q, of approximately equal size.
  2. Pick two numbers so that when they are multiplied together the product will be the size you want (that is, 2048 bits, 4096 bits, etc.).
  3. Now multiply p and q to get n.
  4. Let n = pq.
  5. Multiply Euler’s totient for each of these primes. If you are not familiar with this concept, the Euler’s Totient is the total number of coprime numbers. Two numbers are considered co-prime if they have no common factors. 

    For example, if the original number is 7, then 5 and 7 would be coprime. It just so happens that for prime numbers, this is always the number minus 1. For example, 7 has 6 numbers that are co-prime to it (if you think about this a bit you will see that 1, 2, 3, 4, 5, 6 are all coprime with 7).

  6. Let m = (p – 1)(q – 1).

  7. Select another number; call this number e. You want to pick e so that it is co-prime to m.

  8. Find a number d that when multiplied by e and modulo m would yield 1. (Note: Modulo means to divide two numbers and return the remainder. For example, 8 modulo 3 would be 2.)

  9. Find d, such that de mod m ≡ 1. Now you publish e and n as the public key and keep d and n as the secret key.

To encrypt you simply take your message raised to the e power and modulo 
n = Me % n

To decrypt you take the ciphertext, and raise it to the d power modulo 

n: P = Cd % n

RSA has become a popular encryption method. It is considered quite secure and is often used in situations where a high level of security is needed.

Top comments (0)