Advanced Python (Fall 2017)/lecture10

From ICO wiki
Revision as of 11:26, 14 November 2017 by Eroman (talk | contribs) (Created page with "= Lecture 10 = Cryptopals https://cryptopals.com/ Many solution here: https://www.google.ee/search?dcr=0&source=hp&ei=k-oCWtGxJaHg6ASl0a4g&q=cryptopals+solution...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Lecture 10

Cryptopals

https://cryptopals.com/

Many solution here: https://www.google.ee/search?dcr=0&source=hp&ei=k-oCWtGxJaHg6ASl0a4g&q=cryptopals+solutions+python&oq=cryptopals+solutions+p&gs_l=psy-ab.3.0.0i19k1.657.4332.0.4870.17.16.1.0.0.0.124.1236.14j2.16.0....0...1.1.64.psy-ab..0.17.1235...0j0i131k1j0i22i30i19k1j33i21k1j33i160k1.0.IM86h-JeYBc

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))