## DEV Community đź‘©â€Ťđź’»đź‘¨â€Ťđź’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

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.