0% found this document useful (0 votes)
12 views3 pages

Is5 Pract

The document contains a Python implementation of the RSA encryption algorithm. It includes functions for generating prime numbers, encrypting, and decrypting messages. The main program allows the user to input a message, which is then encrypted and decrypted using RSA with a key length of 1024 bits.

Uploaded by

Anjum Maner
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)
12 views3 pages

Is5 Pract

The document contains a Python implementation of the RSA encryption algorithm. It includes functions for generating prime numbers, encrypting, and decrypting messages. The main program allows the user to input a message, which is then encrypted and decrypted using RSA with a key length of 1024 bits.

Uploaded by

Anjum Maner
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/ 3

Program:

Name: Anjum Maner


RollNo:3129

import random
from math import gcd

def bytes_to_long(bytes_input): return


int.from_bytes(bytes_input, byteorder='big')

def long_to_bytes(long_input): byte_length = (long_input.bit_length() +


7) // 8 return
long_input.to_bytes(byte_length, byteorder='big')

class RSA: def __init__(self,


bit_length):
# Generate two distinct prime numbers p and q p
= self.generate_prime(bit_length // 2) q=
self.generate_prime(bit_length // 2)
# Calculate n = p*q self.n
=p*q
# Calculate z = (p-1)*(q-1) z
= (p - 1) * (q - 1)
# Select a random integer e such that 1 < e < z and gcd(e, z) = 1
self.e = self.generate_e(z) # Calculate d = e^-1 mod z
self.d = pow(self.e, -1, z)

def generate_prime(self, bit_length): while


True: prime_candidate =
random.getrandbits(bit_length) if
self.is_prime(prime_candidate): return
prime_candidate
def is_prime(self, n, k=40): if
n <= 3:
return n == 2 or n == 3
if n % 2 == 0: return
False r, s = 0, n - 1
while s % 2 == 0:
r += 1 s //= 2
for _ in range(k): a=
random.randint(2, n - 1) x=
pow(a, s, n) if x == 1 or x
== n - 1:
continue for
_ in range(r - 1): x
= pow(x, 2, n) if x
== n - 1: break
else: return False
return True

def generate_e(self, z):


e = random.randint(2, z - 1)
while gcd(e, z) != 1: e=
random.randint(2, z - 1)
return e
# Encryption def encrypt(self,
message): msg =
bytes_to_long(message) c=
pow(msg, self.e, self.n) return
long_to_bytes(c)

# Decryption def decrypt(self,


message): c=
bytes_to_long(message) msg
= pow(c, self.d, self.n) return
long_to_bytes(msg)

if __name__ == "__main__": # Input message


message = input("Enter message to encrypt:
").encode()

rsa = RSA(1024)

# Encryption encrypted_message = rsa.encrypt(message)


print("Encrypted message:", encrypted_message)

# Decryption decrypted_message = rsa.decrypt(encrypted_message)


print("Decrypted message:", decrypted_message.decode())

Output:

Enter message to encrypt: Hello, World!


Encrypted message:
b'\x04Xs\x85\xef\xe8\x85`\xeb\xf3=\xaf\xef\x80\xb8V\x857\xaf\xcb\x80\xc2hl\xd8\xf6\xe1\xaf\
xd0\xbe\xdd\xac3`\x17@\xd6\x99\xd3\xef\n\xb1z\xd9M\xdb\xe2\xb4\x91\x16\xf8\xad\xf8\x12\
xdc\x85>\x0c\xf7m\xd0&C\x07\x8aBd\x1eZk=\xb5\xc4\xb5\xddF\xe4\x17b\xcd\x1bFx"6\x91
\x1e\x08S\xa0\x8d-
<\xedf\x99\x10c\xfc\x0e\xdc\x1a\x83\xbf\xb9>r7\xdb\t\xc6R\xa91)oE\xa0rT\x9cu\xab\xd0\xad\
x9e\x9f'

Decrypted message: Hello, World!

You might also like