0% found this document useful (0 votes)
64 views4 pages

Exp 2 - Rsa

The document outlines the implementation and analysis of the RSA cryptosystem, an asymmetric cryptography algorithm that uses a public and private key for secure communication. It includes a detailed explanation of the RSA algorithm, code for key generation, encryption, and decryption, as well as the importance of key size for security. The conclusion emphasizes the robustness of RSA while noting the need for efficiency and security considerations in practical applications.

Uploaded by

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

Exp 2 - Rsa

The document outlines the implementation and analysis of the RSA cryptosystem, an asymmetric cryptography algorithm that uses a public and private key for secure communication. It includes a detailed explanation of the RSA algorithm, code for key generation, encryption, and decryption, as well as the importance of key size for security. The conclusion emphasizes the robustness of RSA while noting the need for efficiency and security considerations in practical applications.

Uploaded by

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

T.

E AI-DS / SEM 6 / T22 / Shrey Shah / 2101098

EXPERIMENT – 2
AIM: Implementation and Analysis of RSA cryptosystem.

THEORY:
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.
The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer.
The public key consists of two numbers where one number is a multiplication of two large
prime numbers. And private key is also derived from the same two prime numbers. So if
somebody can factorize the large number, the private key is compromised. Therefore
encryption strength totally lies on the key size and if we double or triple the key size, the
strength of encryption increases exponentially. RSA keys can be typically 1024 or 2048 bits
long, but experts believe that 1024-bit keys could be broken in the near future. But till now it
seems to be an infeasible task.

CODE:
import random
import math

prime = set()
public_key = None
private_key = None
n = None

def primefiller():
seive = [True] * 250
seive[0] = False
seive[1] = False
for i in range(2, 250):
for j in range(i * 2, 250, i):

1
T.E AI-DS / SEM 6 / T22 / Shrey Shah / 2101098

seive[j] = False

# Filling the prime numbers


for i in range(len(seive)):
if seive[i]:
prime.add(i)

# Picking a random prime number and erasing that prime


# number from list because p!=q
def pickrandomprime():
global prime
k = random.randint(0, len(prime) - 1)
it = iter(prime)
for _ in range(k):
next(it)

ret = next(it)
prime.remove(ret)
return ret

def setkeys():
global public_key, private_key, n
prime1 = pickrandomprime() # First prime number
prime2 = pickrandomprime() # Second prime number
n = prime1 * prime2
fi = (prime1 - 1) * (prime2 - 1)
e=2
while True:
if math.gcd(e, fi) == 1:
break
e += 1
# d = (k*Φ(n) + 1) / e for some integer k
public_key = e
d=2
while True:
if (d * e) % fi == 1:
break
d += 1
private_key = d

# To encrypt the given number


def encrypt(message):
global public_key, n
e = public_key
encrypted_text = 1
while e > 0:
encrypted_text *= message
encrypted_text %= n
e -= 1
return encrypted_text

# To decrypt the given number


def decrypt(encrypted_text):

2
T.E AI-DS / SEM 6 / T22 / Shrey Shah / 2101098

global private_key, n
d = private_key
decrypted = 1
while d > 0:
decrypted *= encrypted_text
decrypted %= n
d -= 1
return decrypted

# First converting each character to its ASCII value and


# then encoding it then decoding the number to get the
# ASCII and converting it to character
def encoder(message):
encoded = []
# Calling the encrypting function in encoding function
for letter in message:
encoded.append(encrypt(ord(letter)))
return encoded

def decoder(encoded):
s = ''
# Calling the decrypting function decoding function
for num in encoded:
s += chr(decrypt(num))
return s

if __name__ == '__main__':
primefiller()
setkeys()
message = "My name is Shrey"
# Uncomment below for manual input
# message = input("Enter the message\n")
# Calling the encoding function
coded = encoder(message)

print("Initial message:")
print(message)
print("\n\nThe encoded message(encrypted by public key)\n")
print(''.join(str(p) for p in coded))
print("\n\nThe decoded message(decrypted by public key)\n")
print(''.join(str(p) for p in decoder(coded)))

OUTPUT:
Initial message:
My name is Shrey

The encoded message(encrypted by public key)


7523128163571071285513512863841810723

The decoded message(decrypted by public key)

3
T.E AI-DS / SEM 6 / T22 / Shrey Shah / 2101098

My name is Shrey

CONCLUSION:
In conclusion, the RSA cryptosystem provides a robust framework for secure communication
through its reliance on the difficulty of factoring large composite numbers. By implementing
RSA, we can encrypt and decrypt messages securely, although efficiency considerations are
crucial due to the computational intensity of modular exponentiation. Attention to key
generation, key length, cryptographic padding, performance optimization, and resistance to
side-channel attacks ensures the effectiveness and resilience of an RSA implementation in
real-world cryptographic applications.

You might also like