I am trying to implement Aes encryption with the OCB mode, when i do both encryption and decryption in a single file everything works perfectly, however if i send a json with nonce cyphertext and a tag through a socket i always get a "MAC check failed" error even thou the jsons are identical. Here's encryption and decryption functions respectively.
def enc_pic(data):
cipher = AES.new(key, Amode, nonce=get_random_bytes(8))
data = pickle.dumps(data)
ciphertext, tag = cipher.encrypt_and_digest(data)
json_k = ['nonce', 'ciphertext', 'tag']
json_v = [b64encode(x).decode('utf-8') for x in (cipher.nonce, ciphertext, tag)]
data = json.dumps(dict(zip(json_k, json_v))).encode("UTF-8")
print(data)
return data
def decr_pic(data):
try:
b64 = json.loads(data.decode("UTF-8"))
print(b64)
json_k = ['nonce', 'ciphertext', 'tag']
jv = {k: b64decode(b64[k]) for k in json_k}
cipher = AES.new(glob.aes_key, AES.MODE_OCB, nonce=jv['nonce'])
plaintext = cipher.decrypt_and_verify(jv['ciphertext'], jv['tag'])
res = pickle.loads(plaintext)
except (ValueError, KeyError) as e:
res = "Incorrect decryption"
print(e)
return res