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 encodes:
s strs
if lens:
printError string too long.
return
return intsumlenstrordsi i for i in rangelens
def decoden:
v
while n :
vappendchrn
n
return joinv
def findcoprimen:
coprimecandidate n
while gcdn coprimecandidate:
coprimecandidate
return coprimecandidate
# Given values
f
g
q findcoprimef
h powf q g q
# Encode each value and calculate their lengths
encodedlengths sumencodeval for val in f g h
printTotal length of encoded public key in characters:", encodedlengths
# Decrypt the message
e
decryptedmessage powermode h q
printDecrypted message for question : decodedecryptedmessage
# Additional question values
q
h
e
def gaussreductionh q:
# Initialize lattice basis vectors
b vector Integerh
b vector Integerq
# Perform Gauss' method of lattice reduction
while True:
# Step : GramSchmidt orthogonalization
mu b bb b
if absmu:
break
b roundmu b
# Exchange b and b if necessary
if normb normb:
b b b b
return b b
# Step : Find the shortest vector F G in the lattice
def findshortestvectorh q:
b b gaussreductionh q
# Calculate the coefficients of the shortest vector
F b
G b
return F G
# Step : Decrypt the message using Bob's private key
def decryptmessagee F G q:
# Decrypt using Bob's private key
d powermodF q
decryptedmessage e d q
return decryptedmessage
# Step : Find the shortest vector F G in the lattice
F G findshortestvectorh q
# Step : Decrypt the message using Bob's private key
decryptedmessage decryptmessagee F G q
printDecrypted message for question : decodedecryptedmessage
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
