This shows you the differences between two versions of the page.
ic:labs:03 [2023/10/01 23:16] razvan.smadu [Laboratorul 03 - PRGs] |
ic:labs:03 [2023/10/16 20:53] (current) razvan.smadu |
||
---|---|---|---|
Line 1: | Line 1: | ||
===== Laboratorul 03 - PRGs ===== | ===== Laboratorul 03 - PRGs ===== | ||
- | Prezentarea PowerPoint pentru acest laborator poate fi găsită [[https://drive.google.com/file/d/1IfyMkfV6jQDzW5KhS_B6wt_azV31SLjL/view?usp=sharing|aici]]. | + | Prezentarea PowerPoint pentru acest laborator poate fi găsită [[https://drive.google.com/file/d/1IfyMkfV6jQDzW5KhS_B6wt_azV31SLjL/view?usp=sharing|aici]]. Puteți găsi o scurtă prezentare a noțiunii de avantaje în [[https://drive.google.com/file/d/1F_mtKi0zeAn1xYQs6Wc7IiiVX8ZW6aeG/view?usp=sharing|acest]] PDF. |
- | Puteți lucra acest laborator folosind și platforma Google Colab, accesând [[https://colab.research.google.com/drive/1dT6VieWJ1QeMLkb0UY3bAuKvTnkRmWBq?usp=sharing | + | Puteți lucra acest laborator folosind platforma Google Colab, accesând [[https://colab.research.google.com/github/ACS-IC-labs/IC-labs/blob/main/labs/lab03/lab3.ipynb |
|acest]] link. | |acest]] link. | ||
+ | <hidden> | ||
<spoiler Click pentru a vedea utils.py> | <spoiler Click pentru a vedea utils.py> | ||
<file python utils.py> | <file python utils.py> | ||
Line 336: | Line 337: | ||
Puteți folosi următorul schelet de cod: | Puteți folosi următorul schelet de cod: | ||
<code python 'ex1_weak_rng.py'> | <code python 'ex1_weak_rng.py'> | ||
+ | from typing import List, Tuple | ||
+ | |||
from utils import * | from utils import * | ||
class WeakRNG: | class WeakRNG: | ||
- | """ Simple class for weak RNG """ | + | """Simple class for weak RNG""" |
- | def __init__(self): | + | def __init__(self) -> None: |
self.rstate = 0 | self.rstate = 0 | ||
self.maxn = 255 | self.maxn = 255 | ||
Line 349: | Line 352: | ||
self.p = 257 | self.p = 257 | ||
- | def init_state(self, rstate): | + | def init_state(self, rstate: int) -> None: |
- | """ Initialise rstate """ | + | """Initialise rstate""" |
self.rstate = rstate # Set this to some value | self.rstate = rstate # Set this to some value | ||
self.update_state() | self.update_state() | ||
- | def update_state(self): | + | def update_state(self) -> None: |
- | """ Update state """ | + | """Update state""" |
self.rstate = (self.a * self.rstate + self.b) % self.p | self.rstate = (self.a * self.rstate + self.b) % self.p | ||
- | def get_prg_byte(self): | + | def get_prg_byte(self) -> int: |
- | """ Return a new PRG byte and update PRG state """ | + | """Return a new PRG byte and update PRG state""" |
- | s = self.rstate & 0xFF | + | b = self.rstate & 0xFF |
self.update_state() | self.update_state() | ||
- | return s | + | return b |
- | def main(): | + | def main() -> None: |
# Initialise weak rng | # Initialise weak rng | ||
wr = WeakRNG() | wr = WeakRNG() | ||
Line 371: | Line 374: | ||
# Print ciphertext | # Print ciphertext | ||
- | CH = 'a432109f58ff6a0f2e6cb280526708baece6680acc1f5fcdb9523129434ae9f6ae9edc2f224b73a8' | + | CH = "a432109f58ff6a0f2e6cb280526708baece6680acc1f5fcdb9523129434ae9f6ae9edc2f224b73a8" |
print("Full ciphertext in hexa:", CH) | print("Full ciphertext in hexa:", CH) | ||
# Print known plaintext | # Print known plaintext | ||
- | pknown = 'Let all creation' | + | pknown = "Let all creation" |
nb = len(pknown) | nb = len(pknown) | ||
print("Known plaintext:", pknown) | print("Known plaintext:", pknown) | ||
Line 382: | Line 385: | ||
# Obtain first nb bytes of RNG | # Obtain first nb bytes of RNG | ||
- | gh = hexxor(pkh, CH[0:nb*2]) | + | gh = hexxor(pkh, CH[0 : nb * 2]) |
print(gh) | print(gh) | ||
gbytes = [] | gbytes = [] | ||
for i in range(nb): | for i in range(nb): | ||
- | gbytes.append(ord(hex_2_str(gh[2*i:2*i+2]))) | + | gbytes.append(ord(hex_2_str(gh[2 * i : 2 * i + 2]))) |
print("Bytes of RNG: ") | print("Bytes of RNG: ") | ||
print(gbytes) | print(gbytes) | ||
Line 398: | Line 401: | ||
# TODO 4: Print the full plaintext | # TODO 4: Print the full plaintext | ||
- | p = '' | + | p = ... |
print("Full plaintext is:", p) | print("Full plaintext is:", p) | ||
Line 489: | Line 492: | ||
import random | import random | ||
- | def get_random_string(n): | + | def get_random_string(n: int) -> str: |
- | """ Generate random bit string """ | + | """Generate random bit string""" |
- | return bin(random.getrandbits(n)).lstrip('0b').zfill(n) | + | return bin(random.getrandbits(n)).lstrip("0b").zfill(n) |
</code> | </code> | ||
</note> | </note> | ||
Line 501: | Line 504: | ||
</note> | </note> | ||
+ | <file python ex4.py> | ||
+ | import math | ||
+ | import random | ||
+ | |||
+ | from utils import * | ||
+ | |||
+ | |||
+ | def get_random_string(n: int) -> str: | ||
+ | """Generate random bit string""" | ||
+ | return bin(random.getrandbits(n)).lstrip("0b").zfill(n) | ||
+ | |||
+ | |||
+ | def main() -> None: | ||
+ | N_RUNS = 100 # the number of runs | ||
+ | N_BITS = 1000 # the number of bits to generate | ||
+ | |||
+ | # TODO: implement and run the multiple times (e.g., 100) the | ||
+ | # statistical tests | ||
+ | |||
+ | |||
+ | if __name__ == "__main__": | ||
+ | main() | ||
+ | </file> | ||
==== Exercițiul 5 - Bonus (2p) ==== | ==== Exercițiul 5 - Bonus (2p) ==== | ||
Line 520: | Line 546: | ||
Încercați să implementați aceeași funcționalitate folosind OpenSSL. OpenSSL suportă RC4, dar nu și Salsa20. Folosiți ChaCha20 in loc de Salsa20, aceasta fiind o variantă îmbunătățită a algoritmului. | Încercați să implementați aceeași funcționalitate folosind OpenSSL. OpenSSL suportă RC4, dar nu și Salsa20. Folosiți ChaCha20 in loc de Salsa20, aceasta fiind o variantă îmbunătățită a algoritmului. | ||
+ | |||
+ | </hidden> |