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 > -
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:]) -
@Yuzhe-pei where do you get
msgfrom? My guess is that it isn’t a byte array.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login