Skip to content

Instantly share code, notes, and snippets.

@fcicq
Created October 15, 2012 12:23
Show Gist options
  • Save fcicq/3892188 to your computer and use it in GitHub Desktop.
Save fcicq/3892188 to your computer and use it in GitHub Desktop.
AES CTR Test
from Crypto.Cipher import AES
from Crypto.Random import _UserFriendlyRNG
from Crypto.Util import Counter
import base64
AES_NBITS = 256
key = _UserFriendlyRNG.get_random_bytes(AES_NBITS / 8)
iv = _UserFriendlyRNG.get_random_bytes(AES.block_size / 2)
# we use the 8 bytes as iv, 8 bytes as counter, max file size is 2^64 * 16 = 2^68 bytes.
ctr_counter = Counter.new(AES.block_size * 8 / 2, prefix=iv)
cipher = AES.new(key, mode=AES.MODE_CTR, counter=ctr_counter)
print cipher.encrypt(b'\x00'*16) # remember every 16 bytes (block_size), the counter is +1ed
print base64.b64encode(iv), base64.b64encode(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment