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 5 calculate d=private key using the signature
if __name__=="__main__":
# function to find signature
def task_4(self, from_user_id: str, to_user_id: str, amount: int, d: int, e: int, n: int)-> int:
# TODO: Implement this method for Task 4
# Build the transaction string
# The transaction string should be in the format of "from_user_id:to_user_id:amount"
trans = from_user_id +':'+ to_user_id +':'+ str(amount)
# Hash the transaction string
# This provides a fixed-size string, which simplifies further processing and ensures that the
# data hasn't been altered without detection.
trans_hash = hashlib.sha256(trans.encode('utf-8'))
print("Transaction hash:
", trans_hash)
# You may find this line helpful for getting the integer value of the transaction hash
trans_hash_as_int = int.from_bytes(trans_hash.digest(), sys.byteorder)
# Encrypt/Sign the transaction hash
signature = pow(trans_hash_as_int, d, n)
print("Signed hash:
", signature)
# Create the signature (the number 11 is simply a placeholder)
return signature
# function to find factors
def task_5(self, n_str: str, e_str: str)-> str:
n = int(n_str,16)
e = int(e_str,16)
# Step 1
p, q = self.get_factors(n)
# Step 2
d = self.get_private_key_from_p_q_e(p, q, e)
return hex(d).rstrip('L')
def task_6(self, given_public_key_n: int, given_public_key_e: int, public_key_list: list)-> int:
# TODO: Implement this method for Task 6
d =0
return d
def get_private_key_from_p_q_e(self, p: int, q: int, e: int):
# Calculate phi(n)
phi_n =(p -1)*(q -1)
# Calculate the modular inverse of e modulo phi(n)
try:
d = pow(e,-1, phi_n)
except ValueError:
# Handle the case where the modular inverse does not exist
d =0
return d
def get_factors(self, n: int):
# TODO: Implement this method for Task 5, Step 1
# n = p * q
# data structure to store all the factors
factors =[]
limit = math.sqrt(n) #this is why alg will have exponential running time complexity
for p in range(2, math.floor(limit +1)):
if n % p ==0:
#factors.append([i, n/i])
q = n // p
if self.is_prime(p) and self.is_prime(q):
return p, q
# Create a list to store the factor pairs
# Check to see which are relative primes
p =0
q =0
# Return 0,0 if no factors are found
return p, q
def is_prime(self, n, k=10):
# we just care about positive integers
if n <=1:
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 range(k): # dont wanna use iteration counter in python we use underscore
# generate a random number [2,N-1]
a = random.randint(2, n)-1 # a is random integer in range 2->(n-1)
# if a^n-1%n !=1 : then n is not a prime
# if pow(a, n-1, n)!=1:
if pow(a, 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!