0% found this document useful (0 votes)
6 views2 pages

cse week 2

The document outlines the RSA algorithm, an asymmetric cryptography method that uses a public key for encryption and a private key for decryption. It details the steps for generating keys, including selecting prime numbers, calculating modulus and totient, and deriving the public and private keys. Additionally, it provides a Python implementation of the RSA algorithm with test cases for encryption and decryption.

Uploaded by

tejaswini reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

cse week 2

The document outlines the RSA algorithm, an asymmetric cryptography method that uses a public key for encryption and a private key for decryption. It details the steps for generating keys, including selecting prime numbers, calculating modulus and totient, and deriving the public and private keys. Additionally, it provides a Python implementation of the RSA algorithm with test cases for encryption and decryption.

Uploaded by

tejaswini reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment :02

Aim: Implement RSA algorithm to demonstrate the concept of public and private keys

RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it works on two different
keys i.e. Public Key and Private Key. As the name describes that the Public Key is given to everyone and the Private
key is kept private.

An example of asymmetric cryptography:

1. A client (for example browser) sends its public key to the server and requests some data.

2. The server encrypts the data using the client’s public key and sends the encrypted data.

3. The client receives this data and decrypts it.

Since this is asymmetric, nobody else except the browser can decrypt the data even if a third party has the public key
of the browser.

RSA algorithm uses the following procedure to generate public and private keys:

• Select two large prime numbers, p and q.

• Multiply these numbers to find n = p x q, where n is called the modulus for encryption and decryption.

• Choose a number e less than n, such that n is relatively prime to (p - 1) x (q -1). It means that e and (p - 1) x (q
- 1) have no common factor except 1. Choose "e" such that 1<e < φ (n), e is prime to φ (n),
gcd (e,d(n)) =1

• If n = p x q, then the public key is <e, n>. A plaintext message m is encrypted using public key <e, n>. To find
ciphertext from the plain text following formula is used to get ciphertext C.
C = me mod n
Here, m must be less than n. A larger message (>n) is treated as a concatenation of messages, each of which
is encrypted separately.

• To determine the private key, we use the following formula to calculate the d such that:
De mod {(p - 1) x (q - 1)} = 1
Or
De mod φ (n) = 1

• The private key is <d, n>. A ciphertext message c is decrypted using private key <d, n>. To calculate plain text
m from the ciphertext c following formula is used to get plain text m.
m = cd mod n

The whole RSA algorithm in simple words,

1. Select p, q (p and q both prime and p not equal to q)

2. Calculate n = p * q

3. Calculate totient, t = (p -1) * (q — 1)

4. Select e using gcd(t, e) = 1 where 1 < e < t

5. Calculate d using (d * e % t = 1)

6. Consider e as Public Key and d as a Private Key.

7. For encryption, Cipher Text = (Message ^ e) % n (where, Message < n)

8. For decryption, Message = (Cipher Text ^ d) % n


Program1:

from math import gcd

# defining a function to perform RSA approch


def RSA(p: int, q: int, message: int):
# calculating n
n=p*q

# calculating totient, t
t = (p - 1) * (q - 1)

# selecting public key, e


for i in range(2, t):
if gcd(i, t) == 1:
e=i
break

# selecting private key, d


j=0
while True:
if (j * e) % t == 1:
d=j
break
j += 1

# performing encryption
ct = (message ** e) % n
print(f"Encrypted message is {ct}")

# performing decryption
mes = (ct ** d) % n
print(f"Decrypted message is {mes}")

# Testcase - 1
RSA(p=53, q=59, message=89)

# Testcase - 2
RSA(p=3, q=7, message=12)

You might also like