Created
November 12, 2013 04:23
-
-
Save jbdatko/7425443 to your computer and use it in GitHub Desktop.
Example of pycrypto CCM mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MODE_CCM is supported by the dropin replacement of the original PyCrypto
https://github.com/Legrandin/pycryptodome