I’m having quite the difficulty understanding what’s wrong here, does anyone have the knowledge to decipher what this error message means and potentially how to fix the issue?
”’
JavaScript
x
25
25
1
from Crypto.Cipher import DES
2
3
def DESenc():
4
key = b'password'
5
message = b'i like beans'
6
cipher = DES.new(key, DES.MODE_EAX)
7
ciphertext, tag = cipher.encrypt_and_digest(message)
8
9
file_out = open("encrypted.bin", "wb")
10
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ]
11
file_out.close()
12
13
def DESdec():
14
file_in = open("encrypted.bin", "rb")
15
nonce, tag, ciphertext = [ file_in.read(x) for x in (16, 16, -1) ]
16
key = b'password'
17
18
cipher = DES.new(key, DES.MODE_EAX, nonce=nonce)
19
data = cipher.decrypt_and_verify(ciphertext, tag)
20
21
print(data.decode('ascii'))
22
23
DESenc()
24
DESdec()
25
”’
Advertisement
Answer
According to the documentation, the tag size corresponds by default to the block size of the algorithm (here), which is 8 bytes for DES (and not 16 bytes as e.g. for AES). Therefore, during decryption
JavaScript
1
2
1
nonce, tag, ciphertext = [ file_in.read(x) for x in (16, 8, -1) ]
2
must be applied. Then decryption works.
Alternatively, the tag size can be set explicitly (but for the parameters used, DES/EAX, 8 bytes is the maximum tag size).
Note that DES is deprecated and insecure. Today’s standard is AES.