159.352 Topic 4 — Exercises
Cryptography
Here we will play with some crytographic functions in Python
Hash functions
The Python hashlib module implements the various crytographic hash functions. Import this module and start with some byte string, e.g.
msg = b ' The secret to everything is 42 '
Get a hash object for the MD5 message digest
myhash = hashlib.md5(msg)
Print out a string representation of the hexadecimal form of the digest
print(myhash.hexdigest())
You can see the actual characters using
bytes .fromhex(myhash.hexdigest())
Look at the documentation for hashlib. What is the hash name, the block size, and the digest size?
The hashlib module also implements the SHA-2 family for 256 bits, hashlib.sha256(), and 512 bit, hashlib.sha512() . The SHA-3 family is also available, i.e. hashlib.sha3_256() and hashlib.sha3_512() . Repeat the above for these functions.
Consider how you would use these hashes for server-side storage of a list of user names and passwords. How would the authentication process work?
Password cracking
Suppose you have come into knowledge of the following hexadecimal string that represents the hash value of some password:
af826ad124dce9ef48772e0bac1dec13
Furthermore, you know the original password is 4 bytes and that the MD5 hash was used.
Write a piece of Python code to crack that password. How long does your code take to do this?
Asymmetric key cryptography
Start with the following string
msgtext = b ' Good '
Look at the lectures (and/or relevant sections of Kurose and Ross) on asymmetric key cryp- tography and consider the following values
e = 65537
d = 109182490673
n = 5551201688147
What permutation of values will function as the “public” key? What will be the “private” key?
In a Python script, encode the message using the public key. Try decoding this using the private key. Do you see the original message?
Repeat this by swapping the public and private keys. Do you see the same result? Try a longer message
msgtext = b ' Good things come to those who wait. '
Consider a suitable procedure for implementing asymmetric key cryptography on arbitrarily long messages.