Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import random import hashlib import math import tkinter as tk # Create a Tkinter window window = tk . Tk ( ) window.title (

import random
import hashlib
import math
import tkinter as tk
# Create a Tkinter window
window = tk.Tk()
window.title("RSA Encryption and Decryption")
# Create a text box for entering the message
message_entry = tk.Entry(window)
message_entry.pack()
# Example usage function with modifications to update the GUI
def example_usage():
message = message_entry.get()
# Generate prime numbers p and q
p = generate_prime_number()
q = generate_prime_number()
# Calculate n and phi(n)
n = p * q
phi_n =(p -1)*(q -1)
# Find encryption key (e)
e = euclid_algorithm(phi_n)
# Find decryption key (d)
d = extended_euclid_algorithm(e, phi_n)
# Encrypt the message
ciphertext = encrypt(message, e, n)
# Decrypt the ciphertext
decrypted_message = decrypt(ciphertext, d, n)
# Update the results label
results_label.config(text=f"Original Message: {message}
Encrypted Message: {ciphertext}
Decrypted Message: {decrypted_message}")
# Create a button to perform encryption and decryption
encrypt_decrypt_button = tk.Button(window, text="Encrypt and Decrypt", command=example_usage)
encrypt_decrypt_button.pack()
# Create a label to display the results
results_label = tk.Label(window)
results_label.pack()
# Generate prime numbers p and q
def generate_prime_number():
while True:
num = random.randint(2**15,2**16) # Adjust the range as per your requirements
if is_prime(num):
return num
# Miller-Rabin primality testing
def is_prime(num):
if num ==2 or num ==3:
return True
if num <2 or num %2==0:
return False
r, s =0, num -1
while s %2==0:
r +=1
s //=2
for _ in range(5): # Adjust the number of iterations for desired accuracy
a = random.randint(2, num -1)
x = pow(a, s, num)
if x ==1 or x == num -1:
continue
for _ in range(r -1):
x = pow(x,2, num)
if x == num -1:
break
else:
return False
return True
# Euclid's algorithm to find the encryption key (e)
def euclid_algorithm(phi_n):
while True:
e = random.randint(2, phi_n)
if math.gcd(e, phi_n)==1:
return e
# Extended Euclid's algorithm to find the decryption key (d)
def extended_euclid_algorithm(e, phi_n):
d =0
x1, x2, y1, y2=0,1,1,0
temp_phi_n = phi_n
while e >0:
temp1= temp_phi_n // e
temp2= temp_phi_n - temp1* e
temp_phi_n = e
e = temp2
x = x2- temp1* x1
y = y2- temp1* y1
x2= x1
x1= x
y2= y1
y1= y
if temp_phi_n ==1:
d = y2
if d <0:
d += phi_n
return d
# Hash function
def hash_message(message):
sha256= hashlib.sha256()
sha256.update(message.encode('utf-8'))
return sha256.digest()
# RSA encryption
def encrypt(message, e, n):
numeric_message = int.from_bytes(message.encode(), 'big')
return pow(numeric_message, e, n)
# RSA decryption
def decrypt(ciphertext, d, n):
numeric_ciphertext = pow(ciphertext, d, n)
decrypted_bytes = numeric_ciphertext.to_bytes((numeric_ciphertext.bit_length()+7)//8, 'big')
decrypted_message =""
for byte in decrypted_bytes:
try:
decrypted_message += chr(byte)
except UnicodeDecodeError:
decrypted_message += f"[Byte {byte}]"
return decrypted_message
# Digital signature signing
def sign(message, d, n):
hashed_message = hash_message(message)
signature = pow(int.from_bytes(hashed_message, 'big'), d, n)
return signature
# Digital signature verifying
def verify(message, signature, e, n):
hashed_message = hash_message(message)
decrypted_signature = pow(signature, e, n)
return int.from_bytes(hashed_message, 'big')== decrypted_signature
# Run the Tkinter event loop
window.mainloop()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

1583474013, 978-1583474013

More Books

Students also viewed these Databases questions

Question

demonstrate the importance of induction training.

Answered: 1 week ago