Question: can you fix this code to where the decryption works properly from sage.all import * def encode ( s ) : s = str (

can you fix this code to where the decryption works properly
from sage.all import *
def encode(s):
s = str(s)
if len(s)>71:
print('Error, string too long.')
return 0
return int(sum(len(str(ord(s[i])*(128** i))) for i in range(len(s))))
def decode(n):
v =[]
while n !=0:
v.append(chr(n %128))
n //=128
return "".join(v)
def find_coprime(n):
coprime_candidate = n +1
while gcd(n, coprime_candidate)!=1:
coprime_candidate +=1
return coprime_candidate
# Given values
f =626346493922296891962453123852828762226840739369641575566811218944
g =977000403491994460922837866629768065514748480840502199588478427171
q = find_coprime(f)
h = pow(f,-1, q)* g % q
# Encode each value and calculate their lengths
encoded_lengths = sum(encode(val) for val in (f, g, h))
print("Total length of encoded public key in characters:", encoded_lengths)
# Decrypt the message
e =787184100010685296775885726144975058457369839039986131451577196095
decrypted_message = power_mod(e, h, q)
print("Decrypted message for question 2:", decode(decrypted_message))
# Additional question 3 values
q2=12271828896945466723852667721709432368165618079179670335632592847
h2=44285959218806234657242064417806848999251196745309876841294546060
e2=67411409695073856848233235796674770260203699443817120950905089296
def gauss_reduction(h2, q2):
# Initialize lattice basis vectors
b1= vector([1, Integer(h2)])
b2= vector([0, Integer(q2)])
# Perform Gauss' method of lattice reduction
while True:
# Step 3: Gram-Schmidt orthogonalization
mu =(b1* b2)//(b1* b1)
if abs(mu)<1/2:
break
b2-= round(mu)* b1
# Exchange b1 and b2 if necessary
if norm(b1)> norm(b2):
b1, b2= b2, b1
return b1, b2
# Step 4: Find the shortest vector (F, G) in the lattice
def find_shortest_vector(h2, q2):
b1, b2= gauss_reduction(h2, q2)
# Calculate the coefficients of the shortest vector
F = b1[0]
G = b1[1]
return F, G
# Step 5: Decrypt the message using Bob's private key
def decrypt_message(e2, F, G, q2):
# Decrypt using Bob's private key
d = power_mod(F,-1, q2)
decrypted_message = e2* d % q2
return decrypted_message
# Step 6: Find the shortest vector (F, G) in the lattice
F, G = find_shortest_vector(h2, q2)
# Step 7: Decrypt the message using Bob's private key
decrypted_message = decrypt_message(e2, F, G, q2)
print("Decrypted message for question 3: ", decode(decrypted_message))

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!