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 get_random_bytes
from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES
import base64
# LWE Parameters
n =512 # Dimension
q =2**15 # Modulus
sigma =3.2 # Standard deviation for the error distribution
def lwe_sample(n, q, sigma):
A = np.random.randint(0, q, size=(n, n))
s = np.random.randint(0, q, size=n)
e = np.random.normal(0, sigma, size=n).astype(int)% q
b =(np.dot(A, s)+ e)% q
return A, b, s
def pad_key(key, n):
key_as_ints = np.frombuffer(key, dtype=np.uint8)
padded_key = np.zeros(n, dtype=np.uint8)
padded_key[:key_as_ints.size]= key_as_ints
return padded_key
def lwe_encrypt(A, b, message, q):
try:
# Ensure message is bytes
if isinstance(message, str):
message = message.encode()
# Symmetric encryption with AES
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_CBC)
ct_bytes = cipher.encrypt(pad(message, AES.block_size))
# Combine ciphertext and IV
ct = base64.b64encode(cipher.iv + ct_bytes).decode('utf-8')
# Encryption using LWE
padded_key = pad_key(key, n)
encrypted_key =(np.dot(A, padded_key)+ b)% q
return ct, encrypted_key
except Exception as e:
print(f"Encryption failed: {e}")
return None, None
def lwe_decrypt(A, s, ct, encrypted_key, q):
try:
# Decryption using LWE
decrypted_key_int =(np.dot(A, s)+ encrypted_key)% q
decrypted_key_bytes = decrypted_key_int[:16].astype(np.uint8).tobytes()
# Symmetric decryption with AES
ct = base64.b64decode(ct)
iv = ct[:AES.block_size]
ct = ct[AES.block_size:]
cipher = AES.new(decrypted_key_bytes, AES.MODE_CBC, iv)
pt = unpad(cipher.decrypt(ct), AES.block_size)
return pt.decode('utf-8')
except Exception as e:
print(f"Decryption failed: {e}")
return None
def main():
try:
message = input("Enter a message to encrypt: ")
# Generate LWE sample
A, b, s = lwe_sample(n, q, sigma)
# Encrypt the message
ct, encrypted_key = lwe_encrypt(A, b, message, q)
if ct and encrypted_key is not None:
print(f"Encrypted message: {ct}")
print(f"Encrypted key: {encrypted_key}")
# Decrypt the message
decrypted_message = lwe_decrypt(A, s, ct, encrypted_key, q)
if decrypted_message is not None:
print(f"Decrypted message: {decrypted_message}")
else:
print("Decryption failed.")
else:
print("Encryption failed.")
except Exception as e:
print(f"Error in main: {e}")
if __name__=="__main__":
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.
 import numpy as np from Crypto.Random import get_random_bytes from Crypto.Util.Padding import

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 Databases Questions!