Skip to content

Instantly share code, notes, and snippets.

@jbdatko
Created November 12, 2013 04:23
Show Gist options
  • Save jbdatko/7425443 to your computer and use it in GitHub Desktop.
Save jbdatko/7425443 to your computer and use it in GitHub Desktop.
Example of pycrypto CCM mode
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
hdr = b'To your eyes only'
plaintext = b'Attack at dawn'
key = b'Sixteen byte key'
nonce = get_random_bytes(11)
cipher = AES.new(key, AES.MODE_CCM, nonce)
cipher.update(hdr)
msg = nonce, hdr, cipher.encrypt(plaintext), cipher.digest()
# We assume that the tuple ``msg`` is transmitted to the receiver:
nonce, hdr, ciphertext, mac = msg
key = b'Sixteen byte key'
cipher = AES.new(key, AES.MODE_CCM, nonce)
cipher.update(hdr)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(mac)
print "The message is authentic: hdr=%s, pt=%s" % (hdr, plaintext)
except ValueError:
print "Key incorrect or message corrupted"
@Northshoot
Copy link

hi,

Which version of Crypto are you using?
There is no MODE_CCM

dir(AES)
['AESCipher', 'MODE_CBC', 'MODE_CFB', 'MODE_CTR', 'MODE_ECB', 'MODE_OFB', 'MODE_OPENPGP', 'MODE_PGP', '_AES', 'builtins', 'doc', 'file', 'name', 'package', 'revision', 'block_size', 'blockalgo', 'key_size', 'new']

@alext234
Copy link

MODE_CCM is supported by the dropin replacement of the original PyCrypto
https://github.com/Legrandin/pycryptodome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment