From 7f70f46742fb4e8c9ad41552eae52e2c9712ab9d Mon Sep 17 00:00:00 2001 From: vis4rd Date: Tue, 7 Nov 2023 21:40:29 +0100 Subject: [PATCH] Add lab4 --- lab4/zad1.py | 45 +++++++++++++++++++++++++++++++++ lab4/zad2.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lab4/zad3.py | 40 ++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 lab4/zad1.py create mode 100644 lab4/zad2.py create mode 100644 lab4/zad3.py diff --git a/lab4/zad1.py b/lab4/zad1.py new file mode 100644 index 0000000..a18d20f --- /dev/null +++ b/lab4/zad1.py @@ -0,0 +1,45 @@ +def zad1(): + def KSA(key): + S = [i for i in range(0, 256)] + j = 0 + for i in range(0, 256): + j = (j+ S[i] + ord(key[i % len(key)])) % 256 + S[i], S[j] = S[j], S[i] + return S + + def Stream_Generator(plaintext, S): + i = 0 + j = 0 + for k in range(0, len(plaintext)): + i = (i+1) % 256 + j = (j+S[i]) % 256 + S[i], S[j] = S[j], S[i] + byte_key : int = S[(S[i] + S[j]) % 256] + yield byte_key + + def encode(plaintext, keystream): + plaintext_num = [ord(i) for i in plaintext] + plaintext_hex = ''.join([f'{i:02X}' for i in plaintext_num]) + retval = '' + for p, k in zip(plaintext_hex, keystream): + retval += f'{(int(p, 16) ^ int(k, 16)):X}' + return retval + + def RC4(plaintext, key): + print(f"Plaintext: {plaintext}") + print(f"Key: {key}") + gen = Stream_Generator(plaintext, KSA(key)) + keystream = ''.join([f'{i:02X}' for i in gen]) + print(f"Keystream: {keystream}") + ciphertext = encode(plaintext, keystream) + print(f"Ciphertext:{ciphertext}\n") + + RC4("Attack at dawn", "Secret") + RC4("Plaintext", "Key") + RC4("pedia", "Wiki") + +def main(): + zad1() + +if __name__ == '__main__': + main() diff --git a/lab4/zad2.py b/lab4/zad2.py new file mode 100644 index 0000000..9c8fd64 --- /dev/null +++ b/lab4/zad2.py @@ -0,0 +1,70 @@ +import math + +def zad2(): + P = ['a', 'b', 'c'] + C = [1, 2, 3, 4] + K = ["K1", "K2", "K3"] + + Pr_P = {'a': 1.0/2.0, 'b': 1.0/3.0, 'c': 1.0/6.0} + Pr_K = {'K1': 1.0/3.0, 'K2': 1.0/3.0, 'K3': 1.0/3.0} + Pr_C = { + 1: (Pr_P['a'] * Pr_K['K1']) + (Pr_P['c'] * Pr_K['K3']), + 2: (Pr_P['a'] * Pr_K['K2']) + (Pr_P['b'] * Pr_K['K1']), + 3: (Pr_P['a'] * Pr_K['K3']) + (Pr_P['b'] * Pr_K['K2']) + (Pr_P['c'] * Pr_K['K1']), + 4: (Pr_P['b'] * Pr_K['K3']) + (Pr_P['c'] * Pr_K['K2']) + } + + def H(target): + sum = 0 + for k, v in target.items(): + if v == 0: + continue + sum += v * math.log2(v) + return -sum + + def HH(target, req): + sum = 0 + for r in req: + for k, v in target.items(): + if v == 0: + continue + sum += r * v * math.log2(v) + return -sum + + H_P = H(Pr_P) + print(f"{H_P=}") + + H_C = H(Pr_C) + print(f"{H_C=}") + + H_K = H(Pr_K) + print(f"{H_K=}") + + H_KC = H_K + H_P - H_C + print(f"{H_KC=}") + + Pr_PC = { + 'a1': (Pr_P['a'] * Pr_K['K1']) / Pr_C[1], + 'a2': (Pr_P['a'] * Pr_K['K2']) / Pr_C[2], + 'a3': (Pr_P['a'] * Pr_K['K3']) / Pr_C[3], + 'a4': (Pr_P['a'] * 0) / Pr_C[4], + 'b1': (Pr_P['b'] * 0) / Pr_C[1], + 'b2': (Pr_P['b'] * Pr_K['K1']) / Pr_C[2], + 'b3': (Pr_P['b'] * Pr_K['K2']) / Pr_C[3], + 'b4': (Pr_P['b'] * Pr_K['K3']) / Pr_C[4], + 'c1': (Pr_P['c'] * Pr_K['K3']) / Pr_C[1], + 'c2': (Pr_P['c'] * 0) / Pr_C[2], + 'c3': (Pr_P['c'] * Pr_K['K1']) / Pr_C[3], + 'c4': (Pr_P['c'] * Pr_K['K2']) / Pr_C[4], + } + + H_PC = HH(Pr_PC, Pr_C) + print(f"{H_PC=}") + + + +def main(): + zad2() + +if __name__ == '__main__': + main() diff --git a/lab4/zad3.py b/lab4/zad3.py new file mode 100644 index 0000000..c843b46 --- /dev/null +++ b/lab4/zad3.py @@ -0,0 +1,40 @@ +import math +import matplotlib.pyplot as plt + +def zad3(): + def H(target): + sum = 0 + for k, v in target.items(): + if v == 0: + continue + sum += v * math.log2(v) + return -sum + + def coin_entropy(o, r): + Pr = { + 'o': o, + 'r': r + } + return H(Pr) + + print(f"{coin_entropy(0.5, 0.5)=}") + print(f"{coin_entropy(1.0/4.0, 3.0/4.0)=}") + print(f"{coin_entropy(99.0/100.0, 1.0/100.0)=}") + + def calc(): + retval = [] + for i in range(1, 31): + p = 2**i + retval.append(coin_entropy((p-1)/p, 1/p)) + return retval + + plt.title("Entropy of a biased coin, y = entropy, x = power of 2") + plt.plot(calc()) + plt.show() + + +def main(): + zad3() + +if __name__ == '__main__': + main()