0% found this document useful (0 votes)
12 views

RSA

The document contains a Python implementation of RSA encryption and decryption. It includes functions for generating prime numbers, key pairs, and performing encryption and decryption of messages. The output demonstrates the generation of keys and the successful encryption and decryption of the message 'Hello World'.

Uploaded by

Sayandeep Das
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)
12 views

RSA

The document contains a Python implementation of RSA encryption and decryption. It includes functions for generating prime numbers, key pairs, and performing encryption and decryption of messages. The output demonstrates the generation of keys and the successful encryption and decryption of the message 'Hello World'.

Uploaded by

Sayandeep Das
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/ 3

L02 Swagat Mitra

RSA Encryption and Decryption


16/12/2024 22BAI1400

CODE

import random
from math import gcd

def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

def generate_prime_candidate(length):
candidate = random.randrange(2**(length-1), 2**length)
if candidate % 2 == 0:
candidate += 1
return candidate

def generate_prime_number(length=8):
candidate = generate_prime_candidate(length)
while not is_prime(candidate):
candidate = generate_prime_candidate(length)
return candidate

def modular_inverse(e, phi):


d, x1, x2, y1 = 0, 0, 1, 1
temp_phi = phi
while e > 0:
temp1 = temp_phi // e
temp2 = temp_phi - temp1 * e
temp_phi, e = e, temp2
x = x2 - temp1 * x1
y = d - temp1 * y1
x2, x1 = x1, x
d, y1 = y1, y
if temp_phi == 1:
return d + phi

def generate_keypair(keysize):
p = generate_prime_number(keysize)
q = generate_prime_number(keysize)
while q == p:
q = generate_prime_number(keysize)

n = p * q
phi = (p - 1) * (q - 1)

e = random.randrange(1, phi)
while gcd(e, phi) != 1:
e = random.randrange(1, phi)

d = modular_inverse(e, phi)

return ((e, n), (d, n))

def encrypt(public_key, plaintext):


e, n = public_key
cipher = [(ord(char) ** e) % n for char in plaintext]
return cipher

def decrypt(private_key, ciphertext):


d, n = private_key
plain = [chr((char ** d) % n) for char in ciphertext]
return ''.join(plain)

if __name__ == "__main__":
keysize = 8
print("Generating RSA key pairs...")
public_key, private_key = generate_keypair(keysize)
print(f"Public Key: {public_key}")
print(f"Private Key: {private_key}")

message = "Hello World"


print(f"Original Message: {message}")

encrypted_msg = encrypt(public_key, message)


print(f"Encrypted Message: {encrypted_msg}")

decrypted_msg = decrypt(private_key, encrypted_msg)


print(f"Decrypted Message: {decrypted_msg}")

OUTPUT

[Running] python -u "d:\SWAGAT\CRYPTO LAB\rsa.py"


Generating RSA key pairs...
Public Key: (253, 38957)
Private Key: (45109, 38957)
Original Message: Hello World
Encrypted Message: [8706, 16274, 19123, 19123, 22638, 35935, 36956, 22638,
9124, 19123, 19937]
Decrypted Message: Hello World

[Done] exited with code=0 in 0.248 seconds

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy