Question: import numpy as np from Crypto.Random import get _ random _ bytes from Crypto.Util.Padding import pad, unpad from Crypto.Cipher import AES import base 6 4
import numpy as np
from Crypto.Random import getrandombytes
from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES
import base
# LWE Parameters
n # Dimension
q # Modulus
sigma # Standard deviation for the error distribution
def lwesamplen q sigma:
A nprandom.randint q sizen n
s nprandom.randint q sizen
e nprandom.normal sigma, sizenastypeint q
b npdotA s e q
return A b s
def padkeykey n:
keyasints npfrombufferkey dtypenpuint
paddedkey npzerosn dtypenpuint
paddedkey:keyasints.size keyasints
return paddedkey
def lweencryptA b message, q:
try:
# Ensure message is bytes
if isinstancemessage str:
message message.encode
# Symmetric encryption with AES
key getrandombytes
cipher AES.newkey AES.MODECBC
ctbytes cipher.encryptpadmessage AES.blocksize
# Combine ciphertext and IV
ct basebencodecipheriv ctbytesdecodeutf
# Encryption using LWE
paddedkey padkeykey n
encryptedkey npdotA paddedkey b q
return ct encryptedkey
except Exception as e:
printfEncryption failed: e
return None, None
def lwedecryptA s ct encryptedkey, q:
try:
# Decryption using LWE
decryptedkeyint npdotA s encryptedkey q
decryptedkeybytes decryptedkeyint:astypenpuinttobytes
# Symmetric decryption with AES
ct basebdecodect
iv ct:AES.blocksize
ct ctAESblocksize:
cipher AES.newdecryptedkeybytes, AES.MODECBC iv
pt unpadcipherdecryptct AES.blocksize
return ptdecodeutf
except Exception as e:
printfDecryption failed: e
return None
def main:
try:
message inputEnter a message to encrypt:
# Generate LWE sample
A b s lwesamplen q sigma
# Encrypt the message
ct encryptedkey lweencryptA b message, q
if ct and encryptedkey is not None:
printfEncrypted message: ct
printfEncrypted key: encryptedkey
# Decrypt the message
decryptedmessage lwedecryptA s ct encryptedkey, q
if decryptedmessage is not None:
printfDecrypted message: decryptedmessage
else:
printDecryption failed."
else:
printEncryption failed."
except Exception as e:
printfError in main: e
if namemain:
main
I tried to run this code with input message as hello world but the output came out like this. Decryption failed. Help solve this problem.
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
