Question: Please Explain how I can make task 5 calculate d = private key using the signature if _ _ name _ _ = =
Please Explain how I can make task calculate dprivate key using the signature
if namemain:
# function to find signature
def taskself fromuserid: str touserid: str amount: int, d: int, e: int, n: int int:
# TODO: Implement this method for Task
# Build the transaction string
# The transaction string should be in the format of "fromuserid:touserid:amount"
trans fromuserid : touserid : stramount
# Hash the transaction string
# This provides a fixedsize string, which simplifies further processing and ensures that the
# data hasn't been altered without detection.
transhash hashlib.shatransencodeutf
printTransaction hash:
transhash
# You may find this line helpful for getting the integer value of the transaction hash
transhashasint int.frombytestranshash.digest sysbyteorder
# EncryptSign the transaction hash
signature powtranshashasint, d n
printSigned hash:
signature
# Create the signature the number is simply a placeholder
return signature
# function to find factors
def taskself nstr: str estr: str str:
n intnstr
e intestr
# Step
p q self.getfactorsn
# Step
d self.getprivatekeyfrompqep q e
return hexdrstripL
def taskself givenpublickeyn: int, givenpublickeye: int, publickeylist: list int:
# TODO: Implement this method for Task
d
return d
def getprivatekeyfrompqeself p: int, q: int, e: int:
# Calculate phin
phin p q
# Calculate the modular inverse of e modulo phin
try:
d powe phin
except ValueError:
# Handle the case where the modular inverse does not exist
d
return d
def getfactorsself n: int:
# TODO: Implement this method for Task Step
# n p q
# data structure to store all the factors
factors
limit math.sqrtn #this is why alg will have exponential running time complexity
for p in range math.floorlimit :
if n p :
#factors.appendi ni
q n p
if self.isprimep and self.isprimeq:
return p q
# Create a list to store the factor pairs
# Check to see which are relative primes
p
q
# Return if no factors are found
return p q
def isprimeself n k:
# we just care about positive integers
if n :
return False
# Fermat's primality test is probabilistic: the more k iterations we make
# the higher the probability that the given number is not composite so a prime
for in rangek: # dont wanna use iteration counter in python we use underscore
# generate a random number N
a random.randint n # a is random integer in range n
# if ann : then n is not a prime
# if powa n n:
if powa n n a:
return False # then we know the given number is not a prime
# after k iterations we can come to the conclusion that n is a prime
return True
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
