I'm trying to use AES-CTR-128 in my code. I use Python 2.7 and utilize the cryptography module to do the encryption.
I need to set specific Counter value, for example counter_iv = 112. When I tried
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
key = os.urandom(32)
counter_iv = 112
cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(b"a secret message") + encryptor.finalize()
This gave me an error message:
Traceback (most recent call last): File "aestest.py", line 14, in <module> cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend) File "/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/ciphers/modes.py", line 139, in __init__ raise TypeError("nonce must be bytes") TypeError: nonce must be bytes
I think it tells me that counter_iv should be a 16-byte string.
My question is how to convert a integer, or maybe a long integer into a 16-byte string?
Moreover, is there any way to convert an integer to any length string? Thanks for your help.
encryptor?