This shows you the differences between two versions of the page.
|
ic:labs:01 [2023/09/30 23:30] razvan.smadu |
ic:labs:01 [2025/02/23 13:24] (current) razvan.smadu [Laboratorul 01 - Introducere] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Laboratorul 01 - Introducere ===== | ===== Laboratorul 01 - Introducere ===== | ||
| - | Puteți lucra acest laborator folosind și platforma Google Colab, accesând [[https://colab.research.google.com/drive/1G8ycsy34UwwQqJHp2DgL5MtGERWzkhN_|acest]] link, cu excepția exercițiului bonus. Un scurt tutorial pentru utilizarea platformei poate fi găsit [[https://docs.google.com/document/d/1Dcnyv9wTfWJx8CEgnR6OgLbHAO7XD1BXGjwOAMAEmlc/edit|aici]]. | + | Puteți lucra acest laborator folosind și platforma Google Colab, accesând [[https://colab.research.google.com/github/ACS-IC-labs/IC-labs/blob/main/labs/lab01/lab1.ipynb|acest]] link, cu excepția exercițiului bonus. Un scurt tutorial pentru utilizarea platformei poate fi găsit [[https://docs.google.com/document/d/1Dcnyv9wTfWJx8CEgnR6OgLbHAO7XD1BXGjwOAMAEmlc/edit|aici]]. |
| ==== Python3 Crash Course ==== | ==== Python3 Crash Course ==== | ||
| - | Tutorialul poate fi găsit [[ic:resurse:python|aici]]. | + | Tutorialul poate fi găsit [[ic:resurse:python|aici]]. La laboratoare vom folosi Python 3.10 sau mai nou. |
| ==== Codificare vs Criptare ==== | ==== Codificare vs Criptare ==== | ||
| Line 31: | Line 31: | ||
| Mai jos găsiți câteva funcții utile pentru conversii și operații de XOR pentru date de diferite formate: | Mai jos găsiți câteva funcții utile pentru conversii și operații de XOR pentru date de diferite formate: | ||
| + | <spoiler Click pentru a vedea utils.py> | ||
| <file python utils.py> | <file python utils.py> | ||
| import base64 | import base64 | ||
| + | from typing import Generator | ||
| - | def _pad(x: str, size: int) -> str: | + | def _pad(data: str, size: int) -> str: |
| - | reminder = len(x) % size | + | reminder = len(data) % size |
| if reminder != 0: | if reminder != 0: | ||
| - | x = "0" * (size - reminder) + x | + | data = "0" * (size - reminder) + data |
| - | return x | + | return data |
| - | def _chunks(string: str, chunk_size: int) -> str: | + | def _chunks(data: str, chunk_size: int) -> Generator[str, None, None]: |
| - | string = _pad(string, chunk_size) | + | data = _pad(data, chunk_size) |
| - | for i in range(0, len(string), chunk_size): | + | for i in range(0, len(data), chunk_size): |
| - | yield string[i : i + chunk_size] | + | yield data[i : i + chunk_size] |
| - | def _hex(x: str) -> str: | + | def _hex(data: int) -> str: |
| - | return format(x, "02x") | + | return format(data, "02x") |
| Line 178: | Line 180: | ||
| - | def strxor(a: str, b: str) -> str: | + | def strxor(operand_1: str, operand_2: str) -> str: |
| """Performs a bitwise exclusive OR (XOR) operation on two strings. | """Performs a bitwise exclusive OR (XOR) operation on two strings. | ||
| Args: | Args: | ||
| - | a (str): The first string to be XORed. | + | operand_1 (str): The first string to be XORed. |
| - | b (str): The second string to be XORed. | + | operand_2 (str): The second string to be XORed. |
| Returns: | Returns: | ||
| Line 196: | Line 198: | ||
| '\\x18\\x00\\x1a' | '\\x18\\x00\\x1a' | ||
| """ | """ | ||
| - | 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(operand_1, operand_2)) |
| - | def bitxor(a: str, b: str) -> str: | + | def bitxor(operand_1: str, operand_2: str) -> str: |
| """Performs a bitwise exclusive OR (XOR) operation on two bit-strings. | """Performs a bitwise exclusive OR (XOR) operation on two bit-strings. | ||
| Args: | Args: | ||
| - | a (str): The first bit-string to be XORed. It should only contain valid | + | operand_1 (str): The first bit-string to be XORed. It should only |
| - | binary digits (0 or 1). | + | contain valid binary digits (0 or 1). |
| - | b (str): The second bit-string to be XORed. It should only contain valid | + | operand_2 (str): The second bit-string to be XORed. It should only |
| - | binary digits (0 or 1). | + | contain valid binary digits (0 or 1). |
| Returns: | Returns: | ||
| Line 219: | Line 221: | ||
| '10011001' | '10011001' | ||
| """ | """ | ||
| - | 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(operand_1, operand_2)) |
| - | def hexxor(a: str, b: str) -> str: | + | def hexxor(operand_1: str, operand_2: str) -> str: |
| """Performs a bitwise exclusive OR (XOR) operation on two hexadecimal | """Performs a bitwise exclusive OR (XOR) operation on two hexadecimal | ||
| strings. | strings. | ||
| Args: | Args: | ||
| - | a (str): The first hexadecimal string to be XORed. It should have an | + | operand_1 (str): The first hexadecimal string to be XORed. It should |
| - | even number of characters and only contain valid hexadecimal digits | + | have an even number of characters and only contain valid hexadecimal |
| - | (0-9, A-F, a-f). | + | digits (0-9, A-F, a-f). |
| - | b (str): The second hexadecimal string to be XORed. It should have an | + | operand_2 (str): The second hexadecimal string to be XORed. It should |
| - | even number of characters and only contain valid hexadecimal digits | + | have an even number of characters and only contain valid |
| - | (0-9, A-F, a-f). | + | digits (0-9, A-F, a-f). |
| Returns: | Returns: | ||
| Line 248: | Line 250: | ||
| return "".join( | return "".join( | ||
| _hex(int(x, 16) ^ int(y, 16)) | _hex(int(x, 16) ^ int(y, 16)) | ||
| - | for (x, y) in zip(_chunks(a, 2), _chunks(b, 2)) | + | for (x, y) in zip(_chunks(operand_1, 2), _chunks(operand_2, 2)) |
| ) | ) | ||
| Line 260: | Line 262: | ||
| Args: | Args: | ||
| bytes_data (bytearray | bytes): The byte array or the byte string to be | bytes_data (bytearray | bytes): The byte array or the byte string to be | ||
| - | converted. It should be encoded in UTF-8 format. | + | converted. It should be encoded in Latin-1 format. |
| Returns: | Returns: | ||
| str: The string representation of the byte array or the byte string, | str: The string representation of the byte array or the byte string, | ||
| - | decoded using UTF-8 encoding. | + | decoded using Latin-1 encoding. |
| Examples: | Examples: | ||
| Line 272: | Line 274: | ||
| 'IC' | 'IC' | ||
| """ | """ | ||
| - | return bytes_data.decode(encoding="utf-8") | + | return bytes_data.decode(encoding="raw_unicode_escape") |
| Line 283: | Line 285: | ||
| Returns: | Returns: | ||
| bytes: The byte string representation of the string, encoded using | bytes: The byte string representation of the string, encoded using | ||
| - | UTF-8 encoding. | + | Latin-1 encoding. |
| Examples: | Examples: | ||
| Line 291: | Line 293: | ||
| b'IC' | b'IC' | ||
| """ | """ | ||
| - | return string_data.encode(encoding="utf-8") | + | return string_data.encode(encoding="raw_unicode_escape") |
| Line 304: | Line 306: | ||
| Returns: | Returns: | ||
| - | str: The base64 encoded string, using UTF-8 encoding. | + | str: The base64 encoded string, using Latin-1 encoding. |
| Examples: | Examples: | ||
| Line 323: | Line 325: | ||
| Returns: | Returns: | ||
| - | str: The decoded string, using UTF-8 encoding. | + | str: The decoded string, using Latin-1 encoding. |
| Examples: | Examples: | ||
| Line 333: | Line 335: | ||
| return bytes_to_string(base64.b64decode(string_to_bytes(data))) | return bytes_to_string(base64.b64decode(string_to_bytes(data))) | ||
| </file> | </file> | ||
| + | </spoiler> | ||
| == Bytes în Python == | == Bytes în Python == | ||
| Line 372: | Line 375: | ||
| <code python> | <code python> | ||
| - | alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | + | ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| Line 378: | Line 381: | ||
| if not "A" <= letter <= "Z": | if not "A" <= letter <= "Z": | ||
| raise ValueError("Invalid letter") | raise ValueError("Invalid letter") | ||
| - | return alphabet[(ord(letter) - ord("A") + 3) % len(alphabet)] | + | return ALPHABET[(ord(letter) - ord("A") + 3) % len(ALPHABET)] |
| </code> | </code> | ||
| Line 397: | Line 400: | ||
| Testați următoarele comenzi în consolă: | Testați următoarele comenzi în consolă: | ||
| <code python> | <code python> | ||
| - | >>> print(alphabet) | + | >>> print(ALPHABET) |
| - | >>> len(alphabet) | + | >>> len(ALPHABET) |
| - | >>> alphabet[0] | + | >>> ALPHABET[0] |
| >>> ord("A") | >>> ord("A") | ||
| >>> ord("D") - ord("A") | >>> ord("D") - ord("A") | ||
| Line 445: | Line 448: | ||
| <file Python test_caesar.py> | <file Python test_caesar.py> | ||
| - | alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | + | ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| Line 451: | Line 454: | ||
| if not 'A' <= letter <= 'Z': | if not 'A' <= letter <= 'Z': | ||
| raise ValueError('Invalid letter') | raise ValueError('Invalid letter') | ||
| - | return alphabet[(ord(letter) - ord('A') + 3) % len(alphabet)] | + | return ALPHABET[(ord(letter) - ord('A') + 3) % len(ALPHABET)] |
| Line 493: | Line 496: | ||
| if not "A" <= letter <= "Z": | if not "A" <= letter <= "Z": | ||
| raise ValueError("Invalid letter") | raise ValueError("Invalid letter") | ||
| - | return alphabet[(ord(letter) - ord("A") + k) % len(alphabet)] | + | return ALPHABET[(ord(letter) - ord("A") + k) % len(ALPHABET)] |