This shows you the differences between two versions of the page.
|
ic:labs:01 [2023/10/01 01:13] razvan.smadu [Codificare vs Criptare] |
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 34: | Line 34: | ||
| <file python utils.py> | <file python utils.py> | ||
| import base64 | import base64 | ||
| + | from typing import Generator | ||
| Line 43: | Line 44: | ||
| - | def _chunks(data: str, chunk_size: int) -> str: | + | def _chunks(data: str, chunk_size: int) -> Generator[str, None, None]: |
| data = _pad(data, chunk_size) | data = _pad(data, chunk_size) | ||
| for i in range(0, len(data), chunk_size): | for i in range(0, len(data), chunk_size): | ||
| Line 49: | Line 50: | ||
| - | def _hex(data: str) -> str: | + | def _hex(data: int) -> str: |
| return format(data, "02x") | return format(data, "02x") | ||
| Line 261: | 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 273: | Line 274: | ||
| 'IC' | 'IC' | ||
| """ | """ | ||
| - | return bytes_data.decode(encoding="utf-8") | + | return bytes_data.decode(encoding="raw_unicode_escape") |
| Line 284: | 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 292: | Line 293: | ||
| b'IC' | b'IC' | ||
| """ | """ | ||
| - | return string_data.encode(encoding="utf-8") | + | return string_data.encode(encoding="raw_unicode_escape") |
| Line 305: | 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 324: | Line 325: | ||
| Returns: | Returns: | ||
| - | str: The decoded string, using UTF-8 encoding. | + | str: The decoded string, using Latin-1 encoding. |
| Examples: | Examples: | ||
| Line 374: | Line 375: | ||
| <code python> | <code python> | ||
| - | alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | + | ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| Line 380: | 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 399: | 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 447: | Line 448: | ||
| <file Python test_caesar.py> | <file Python test_caesar.py> | ||
| - | alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | + | ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| Line 453: | 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 495: | 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)] |