This shows you the differences between two versions of the page.
ic:laboratoare:01 [2020/10/03 12:15] acosmin.maria |
ic:laboratoare:01 [2020/10/18 15:55] (current) acosmin.maria |
||
---|---|---|---|
Line 29: | Line 29: | ||
<file python utils.py> | <file python utils.py> | ||
import base64 | import base64 | ||
+ | |||
# CONVERSION FUNCTIONS | # CONVERSION FUNCTIONS | ||
+ | |||
def _chunks(string, chunk_size): | def _chunks(string, chunk_size): | ||
for i in range(0, len(string), chunk_size): | for i in range(0, len(string), chunk_size): | ||
yield string[i:i+chunk_size] | yield string[i:i+chunk_size] | ||
+ | |||
+ | def _hex(x): | ||
+ | return format(x, '02x') | ||
+ | |||
def hex_2_bin(data): | def hex_2_bin(data): | ||
return ''.join(f'{int(x, 16):08b}' for x in _chunks(data, 2)) | return ''.join(f'{int(x, 16):08b}' for x in _chunks(data, 2)) | ||
+ | |||
def str_2_bin(data): | def str_2_bin(data): | ||
return ''.join(f'{ord(c):08b}' for c in data) | return ''.join(f'{ord(c):08b}' for c in data) | ||
+ | |||
def bin_2_hex(data): | def bin_2_hex(data): | ||
return ''.join(f'{int(b, 2):02x}' for b in _chunks(data, 8)) | return ''.join(f'{int(b, 2):02x}' for b in _chunks(data, 8)) | ||
+ | |||
def str_2_hex(data): | def str_2_hex(data): | ||
return ''.join(f'{ord(c):02x}' for c in data) | return ''.join(f'{ord(c):02x}' for c in data) | ||
+ | |||
def bin_2_str(data): | def bin_2_str(data): | ||
return ''.join(chr(int(b, 2)) for b in _chunks(data, 8)) | return ''.join(chr(int(b, 2)) for b in _chunks(data, 8)) | ||
+ | |||
def hex_2_str(data): | def hex_2_str(data): | ||
return ''.join(chr(int(x, 16)) for x in _chunks(data, 2)) | return ''.join(chr(int(x, 16)) for x in _chunks(data, 2)) | ||
+ | |||
# XOR FUNCTIONS | # XOR FUNCTIONS | ||
+ | |||
def strxor(a, b): # xor two strings, trims the longer input | def strxor(a, b): # xor two strings, trims the longer input | ||
return ''.join(chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b)) | return ''.join(chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b)) | ||
+ | |||
def bitxor(a, b): # xor two bit-strings, trims the longer input | def bitxor(a, b): # xor two bit-strings, trims the longer input | ||
return ''.join(str(int(x) ^ int(y)) for (x, y) in zip(a, b)) | return ''.join(str(int(x) ^ int(y)) for (x, y) in zip(a, b)) | ||
+ | |||
def hexxor(a, b): # xor two hex-strings, trims the longer input | def hexxor(a, b): # xor two hex-strings, trims the longer input | ||
- | return ''.join(hex(int(x, 16) ^ int(y, 16))[2:] for (x, y) in zip(_chunks(a, 2), _chunks(b, 2))) | + | return ''.join(_hex(int(x, 16) ^ int(y, 16)) for (x, y) in zip(_chunks(a, 2), _chunks(b, 2))) |
+ | |||
# BASE64 FUNCTIONS | # BASE64 FUNCTIONS | ||
+ | |||
def b64decode(data): | def b64decode(data): | ||
return bytes_to_string(base64.b64decode(string_to_bytes(data))) | return bytes_to_string(base64.b64decode(string_to_bytes(data))) | ||
+ | |||
def b64encode(data): | def b64encode(data): | ||
return bytes_to_string(base64.b64encode(string_to_bytes(data))) | return bytes_to_string(base64.b64encode(string_to_bytes(data))) | ||
+ | |||
# PYTHON3 'BYTES' FUNCTIONS | # PYTHON3 'BYTES' FUNCTIONS | ||
+ | |||
def bytes_to_string(bytes_data): | def bytes_to_string(bytes_data): | ||
return bytes_data.decode() # default utf-8 | return bytes_data.decode() # default utf-8 | ||
+ | |||
def string_to_bytes(string_data): | def string_to_bytes(string_data): | ||
return string_data.encode() # default utf-8 | return string_data.encode() # default utf-8 | ||
Line 234: | Line 237: | ||
Decrypt the last ciphertext knowing that all the messages were encrypted with the same key using OTP. | Decrypt the last ciphertext knowing that all the messages were encrypted with the same key using OTP. | ||
- | <file ciphers.ciphertexts> | + | <file txt ciphers.ciphertexts> |
8841a58f876901c9e195d1e320e0c30a017bec11b0643d30533adcb0475e85a820d64e1a0869963453b490933b7005839f7d8a9571c8a890d75773bc2acc11d5cb3259f0610e95ad6ae1ec8445fc836b661b9c0554494c430210989e4a42ff7b4c19338945a68653c89d783e8460935c93896a3d73d9bc84a8e381951443ab8ada62c5d662d43c0da848c3602d | 8841a58f876901c9e195d1e320e0c30a017bec11b0643d30533adcb0475e85a820d64e1a0869963453b490933b7005839f7d8a9571c8a890d75773bc2acc11d5cb3259f0610e95ad6ae1ec8445fc836b661b9c0554494c430210989e4a42ff7b4c19338945a68653c89d783e8460935c93896a3d73d9bc84a8e381951443ab8ada62c5d662d43c0da848c3602d | ||
8e14e681d0651cd5fb99d1a87cee972b4436fe19b22c3d1e7a75c2a6155ac4fa06d74e07042889300ab490d226614c818574d99a38d8a899d45478f83cca04818a3549f061079bb139a5f78542eac63873499513460d48534345addf5f42b632475623d14fb49c16c1913d7fca019f59d09b253c3c98a480e1e3829c0942bec2da478bcc6bd42a00e953883a622497 | 8e14e681d0651cd5fb99d1a87cee972b4436fe19b22c3d1e7a75c2a6155ac4fa06d74e07042889300ab490d226614c818574d99a38d8a899d45478f83cca04818a3549f061079bb139a5f78542eac63873499513460d48534345addf5f42b632475623d14fb49c16c1913d7fca019f59d09b253c3c98a480e1e3829c0942bec2da478bcc6bd42a00e953883a622497 | ||
Line 247: | Line 250: | ||
</file> | </file> | ||
+ | == Hints == | ||
+ | <note tip> | ||
+ | * What happens when you XOR a character from [a-z] with the character ' ' (spacebar). Check also for [A-Z]. | ||
+ | * You can't write a perfect algorithm to solve the problem in one shot, you will mostly have to guess. Why? | ||
+ | * The challenge is nice, but can get tedious. Luckily for us there is a cool open source implementation. Check it out! | ||
+ | </note> | ||
=== How to run on Windows === | === How to run on Windows === | ||
- | 1. Activate WSL (Windows Linux Subsystem) [[https://docs.microsoft.com/en-us/windows/wsl/install-win10|here]]. | + | - Activate WSL (Windows Linux Subsystem) [[https://docs.microsoft.com/en-us/windows/wsl/install-win10|here]] |
- | 2. Install Ubuntu from Windows Store | + | - turn Windows features on/off |
- | 3. Open a terminal and type "ubuntu" | + | - activate "Containers" and "Windows Subsystem for Linux" |
- | 4. Wait | + | - restart and install Ubuntu from Windows Store |
+ | - open a terminal and type ubuntu | ||
+ | - wait for the installation to complete and don't forget to "sudo apt-get upgrade", "sudo apt-get update" | ||
+ | - check that you have python3 installed and pip (python package manager) | ||
+ | - pip install urwid | ||
+ | - git clone https://github.com/cosminacho/MTP.git | ||
+ | - python mtp.py <ciphertexts filename> | ||