How to use AES CTR mode decrypt msg
-
Hi to everyone.
I try to create a program using AES in CTR mode.
from crypto import AES import crypto key = b'notsuchsecretkey' # 128 bit (16 bytes) key counter = crypto.getrandbits(128) # hardware generated random IV (never reuse it) cipher = AES(key, AES.MODE_CTR, counter=counter) msg = cipher.encrypt(b'Attack at dawn') print(msg) # ... after properly sent the encrypted message somewhere ... cipher = AES(key, AES.MODE_CTR, msg[:16]) # on the decryption side original = cipher.decrypt(msg[16:]) print(original)
Output
>>> b'\xdb\x16\xa43\x0bfZb\x89\xae\xec\xdf\xf1\xfd' Traceback (most recent call last): File "<stdin>", line 12, in <module> TypeError: object with buffer protocol required >
As you can see, I can encrypt msg. But when I decrypt msg, I will receive the following Error.
Traceback (most recent call last): File "<stdin>", line 12, in <module> TypeError: object with buffer protocol required >
-
@Yuzhe-pei where do you get
msg
from? My guess is that it isn’t a byte array.
-
I am using some old stable code with older firmware, and this is not using CTR mode, but CFB. I use an encrypt then MAC approach, with the IV sent between the MAC digest and the cipher text. I'm not sure how different this is to CTR mode or more recent pycom code, but hope it might help.
cipher = AES(config.aes_key, AES.MODE_CFB,lora_in[hmac_len:hmac_len+iv_len]) original = cipher.decrypt(lora_in[hmac_len+iv_len:])