Answered step by step
Verified Expert Solution
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 tkTk
window.titleRSA Encryption and Decryption"
# Create a text box for entering the message
messageentry tkEntrywindow
messageentry.pack
# Example usage function with modifications to update the GUI
def exampleusage:
message messageentry.get
# Generate prime numbers p and q
p generateprimenumber
q generateprimenumber
# Calculate n and phin
n p q
phin p q
# Find encryption key e
e euclidalgorithmphin
# Find decryption key d
d extendedeuclidalgorithme phin
# Encrypt the message
ciphertext encryptmessage e n
# Decrypt the ciphertext
decryptedmessage decryptciphertext d n
# Update the results label
resultslabel.configtextfOriginal Message: message
Encrypted Message: ciphertext
Decrypted Message: decryptedmessage
# Create a button to perform encryption and decryption
encryptdecryptbutton tkButtonwindow text"Encrypt and Decrypt", commandexampleusage
encryptdecryptbutton.pack
# Create a label to display the results
resultslabel tkLabelwindow
resultslabel.pack
# Generate prime numbers p and q
def generateprimenumber:
while True:
num random.randint # Adjust the range as per your requirements
if isprimenum:
return num
# MillerRabin primality testing
def isprimenum:
if num or num :
return True
if num or num :
return False
r s num
while s :
r
s
for in range: # Adjust the number of iterations for desired accuracy
a random.randint num
x powa s num
if x or x num :
continue
for in ranger :
x powx num
if x num :
break
else:
return False
return True
# Euclid's algorithm to find the encryption key e
def euclidalgorithmphin:
while True:
e random.randint phin
if math.gcde phin:
return e
# Extended Euclid's algorithm to find the decryption key d
def extendedeuclidalgorithme phin:
d
x x y y
tempphin phin
while e :
temp tempphin e
temp tempphin temp e
tempphin e
e temp
x x temp x
y y temp y
x x
x x
y y
y y
if tempphin :
d y
if d :
d phin
return d
# Hash function
def hashmessagemessage:
sha hashlib.sha
shaupdatemessageencodeutf
return shadigest
# RSA encryption
def encryptmessage e n:
numericmessage int.frombytesmessageencode 'big'
return pownumericmessage, e n
# RSA decryption
def decryptciphertext d n:
numericciphertext powciphertext d n
decryptedbytes numericciphertext.tobytesnumericciphertext.bitlength 'big'
decryptedmessage
for byte in decryptedbytes:
try:
decryptedmessage chrbyte
except UnicodeDecodeError:
decryptedmessage fByte byte
return decryptedmessage
# Digital signature signing
def signmessage d n:
hashedmessage hashmessagemessage
signature powintfrombyteshashedmessage, 'big' d n
return signature
# Digital signature verifying
def verifymessage signature, e n:
hashedmessage hashmessagemessage
decryptedsignature powsignature e n
return int.frombyteshashedmessage, 'big' decryptedsignature
# Run the Tkinter event loop
window.mainloop
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started