I wish, using Python3's Crypto.Cipher to use AES in CTR mode. The actual problem is that I have an array of binary digits ( "0/1" in string format ) and that I wish to encrypt/decrypt them using the AES-CTR. After viewing this article I tried to develop the code that is appended below.
from Crypto.Cipher import AES
import os
import sys
secret = os.urandom(16)
crypto = AES.new(os.urandom(32), AES.MODE_CTR, counter=lambda: secret)
msg = "Hello World"
#string-->bytes
bytes_msg = str.encode(msg)
print(msg + " > 2bytes > " + str(bytes_msg))
#bytes-->encrypt
encrypted_bytes_msg = crypto.encrypt(bytes_msg)
print(" > encryption > " + str(encrypted_bytes_msg))
#encrypt-->decrypt
decrypted_bytes_msg = crypto.decrypt(encrypted_bytes_msg)
print(" > decryption > " + str(decrypted_bytes_msg))
#bytes-->string
#decrypted_msg = decrypted_bytes_msg.decode() # <= !ERROR HERE!
#print(" > msg > " + decrypted_msg)
I was expecting to see something like the following:
Hello World > 2bytes > " b'Hello World' > encryption > #JibberishText# > decryption > b'Hello World' > Hello World
The actual results of this run are:
Hello World > 2bytes > b'Hello World' > encryption > b'\x8eo\xfc`\xeck\xcf\r4\x1fS' > decryption > b'\xc7\x93\x8a\x1dK\xad\xc5\x9d8\x9c\x18'
Also: If i do not comment out the last lines I get the following error:
File "aes.py", line 25, in decrypted_msg = decrypted_bytes_msg.decode() UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8e in position 0: invalid start byte