Skip to content

Commit

Permalink
Add lab4
Browse files Browse the repository at this point in the history
  • Loading branch information
vis4rd committed Nov 7, 2023
1 parent 475af08 commit 7f70f46
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lab4/zad1.py
Original file line number Diff line number Diff line change
@@ -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()
70 changes: 70 additions & 0 deletions lab4/zad2.py
Original file line number Diff line number Diff line change
@@ -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()
40 changes: 40 additions & 0 deletions lab4/zad3.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7f70f46

Please sign in to comment.