Preparation for implementing some cryptographic primitives in Python
Later on in the semester we will be implementing some cryptographic primitives in Python. Want to see if you need to do some preparatory work? Here is a good practice task to use as a gauge.
First, set up a virtualenv or conda environment. For conda:
Next install the PETLib package:
Once you have petlib installed and have passed the unit tests, a mini-example you can try is to implement symmetric encryption using AES-GCM. You don't really need knowledge of AES-GCM to do this; it's all about using PETLib effectively.
- Note: don't look at the official documentation for quick_gcm_enc until you're ready to check your work, as it contains the answer as an example. Here is the documentation for quick_gcm_enc that you will need, sans example:
quick_gcm_enc(key, iv, msg, assoc=None, tagl=16)[source] One operation GCM encryption. Args: key (str): the AES symmetric key. Length depends on block cipher choice. iv (str): an Initialization Vector of up to the block size. (Can be shorter.) msg (str): the message encrypt. assoc (str): associated data that will be integrity protected, but not encrypted. tagl (int): the length of the tag, up to the block length.
- Consider these imports:
from os import urandom from petlib.cipher import Cipher
- Note that urandom produces cryptographically strong bytes, which is handy for keys and ivs.
- Use the encoded plaintext rather than the input directly (you can encypt bytes not unicode strings, hence the need for encoding and decoding with UTF8 first).
- The documentation for petlib.cipher is available here.
- Once you've given this a try, you can check your work with the docs by looking at the quick_gcm_enc example
Let me know how it goes.