Lecture 10

Cryptopals

Very good solutions here (better than mine): https://github.com/ricpacca/cryptopals

Binary, Hex, Base64

Look them up if you don't know what they are. Unfortanetly, I cannot find a very good introduction to the topics in python.

In python you have integer literals in base 10, like 10 or in base 16, like 0xf or in base 2, like 0b1

Binary numbers can easily be representing in base 16 (called "Hex"). 8 bits (binary digits) is 2 digits in hex. Base64 is a way of storing binary data so it uses only ascii characters. A string of Base64 is shorter than a string of equivalent Hex, so Base64 is popular way to put binary data embeded in a text document.

Problems

The problems are good because they require us to understand encoding in python. My solutions are poorer quality in comparison to the solutions here: https://github.com/ricpacca/cryptopals

Problem 1-1

import base64

message_hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
message_bytes = bytearray.fromhex(message_hex)
message_base64 = base64.encodebytes(message_bytes)
print(message_base64)

Problem 1-2

one way

# Convert both hex string to bytes
hex_string_1 = '1c0111001f010100061a024b53535009181c'
hex_string_2 = '686974207468652062756c6c277320657965'

bytes_1 = bytearray.fromhex(hex_string_1)
bytes_2 = bytearray.fromhex(hex_string_2)

# Pair each byte with other byte in same position
byte_pair_list = list(zip(bytes_1, bytes_2))

# go through each bit in hex string bytes, and xor, and store result
# XOR opertator is ^
result_list = []
for b1, b2 in byte_pair_list:
xor_result = b1 ^ b2
result_list.append(xor_result)

# convert list of integers to hex
result_bytes = bytearray(result_list)
print(bytearray.hex(result_bytes))