https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture10&feed=atom&action=historyAdvanced Python (Fall 2017)/lecture10 - Revision history2024-03-29T09:46:32ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture10&diff=126801&oldid=prevEroman: 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..."2017-11-14T08:26:52Z<p>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..."</p>
<p><b>New page</b></p><div>= Lecture 10 =<br />
<br />
Cryptopals<br />
<br />
https://cryptopals.com/<br />
<br />
Many solution here: https://www.google.ee/search?dcr=0&amp;source=hp&amp;ei=k-oCWtGxJaHg6ASl0a4g&amp;q=cryptopals+solutions+python&amp;oq=cryptopals+solutions+p&amp;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<br />
<br />
Very good solutions here (better than mine): https://github.com/ricpacca/cryptopals<br />
<br />
== Binary, Hex, Base64 ==<br />
<br />
Look them up if you don't know what they are. Unfortanetly, I cannot find a very good introduction to the topics in python.<br />
<br />
In python you have integer literals in base 10, like <code>10</code> or in base 16, like <code>0xf</code> or in base 2, like <code>0b1</code><br />
<br />
Binary numbers can easily be representing in base 16 (called &quot;Hex&quot;). 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.<br />
<br />
== Problems ==<br />
<br />
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<br />
<br />
== Problem 1-1 ==<br />
<br />
<source lang="python">import base64<br />
<br />
message_hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"<br />
message_bytes = bytearray.fromhex(message_hex)<br />
message_base64 = base64.encodebytes(message_bytes)<br />
print(message_base64)</source><br />
== Problem 1-2 ==<br />
<br />
one way<br />
<br />
<source lang="python"># Convert both hex string to bytes<br />
hex_string_1 = '1c0111001f010100061a024b53535009181c'<br />
hex_string_2 = '686974207468652062756c6c277320657965'<br />
<br />
bytes_1 = bytearray.fromhex(hex_string_1)<br />
bytes_2 = bytearray.fromhex(hex_string_2)<br />
<br />
# Pair each byte with other byte in same position<br />
byte_pair_list = list(zip(bytes_1, bytes_2))<br />
<br />
# go through each bit in hex string bytes, and xor, and store result<br />
# XOR opertator is ^<br />
result_list = []<br />
for b1, b2 in byte_pair_list:<br />
xor_result = b1 ^ b2<br />
result_list.append(xor_result)<br />
<br />
# convert list of integers to hex<br />
result_bytes = bytearray(result_list)<br />
print(bytearray.hex(result_bytes))</source></div>Eroman