Skip to content

Commit

Permalink
Add lab3
Browse files Browse the repository at this point in the history
  • Loading branch information
vis4rd committed Nov 7, 2023
1 parent 27b6048 commit 475af08
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lab3/zad1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from copy import copy

def zad1():
def a(index, start: list[int]):
if index < 4:
return start[index]
else:
temp = copy(start)
for i in range(0, index):
temp[i % 4] = (temp[(i % 4) - 4] + temp[(i % 4) - 3] + temp[(i % 4) - 2] + temp[(i % 4) - 1]) % 2
return temp[index % 4]

def b(index, start: list[int]):
if index < 4:
return start[index]
else:
temp = copy(start)
for i in range(0, index):
temp[i % 4] = (temp[(i % 4) - 4] + temp[(i % 4) - 1]) % 2
return temp[index % 4]


def generate(func, start: list[int], length = 50):
return ''.join([chr(func(i, start) + ord('0')) for i in range(0, length)])

print(f"Dane testowe:")

print(f"a - wektor: 0100")
print(f"a - strumien: {generate(a, [0, 1, 0, 0])}")
print(f"b - wektor: 1000")
print(f"b - strumien: {generate(b, [1, 0, 0, 0])}")

def search(seq):
guess = 1
max_len = len(seq)
for x in range(1, max_len):
for y in range(1, max_len):
if seq[0:x] == seq[y:y+x] :
guess = x

return abs(guess - max_len)


print(f"\nWyniki a:")
for i in range(0, 16):
binary = [ord(c) - ord('0') for c in f"{i:04b}"]
stream = generate(a, binary)
print(f"P({i:04b})={search(stream)}")


print(f"\nWyniki b:")
for i in range(0, 16):
binary = [ord(c) - ord('0') for c in f"{i:04b}"]
stream = generate(b, binary)
print(f"P({i:04b})={search(stream)}")


def main():
zad1()

if __name__ == '__main__':
main()
46 changes: 46 additions & 0 deletions lab3/zad2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from copy import copy

def zad2():
def gen(index, start: list[int]):
if index < 5:
return start[index]
else:
temp = copy(start)
for i in range(0, index):
temp[i % 5] = (temp[(i % 5) - 5] + temp[(i % 5) - 2]) % 2
return temp[index % 5]


def generate(start: list[int], length = 50):
return ''.join([chr(gen(i, start) + ord('0')) for i in range(0, length)])

plaintext_str = "011001111111000"
plaintext = [ord(c) - ord('0') for c in plaintext_str]

stream = generate([1, 1, 0, 1, 0], length=15)

def encode(plaintext, stream):
return ''.join([chr(((ord(p) + ord(k)) % 2) + ord('0')) for p, k in zip(plaintext, stream)])

print(f"Encryption:")
print(f"Plaintext: {plaintext_str}")
print(f"Key stream:{stream}")
encoded = encode(plaintext_str, stream)
print(f"Cyphertext:{encoded}")

def decode(plaintext, stream):
return ''.join([chr(((ord(p) - ord(k)) % 2) + ord('0')) for p, k in zip(plaintext, stream)])

print("Decryption:")
print(f"Cyphertext:{encoded}")
print(f"Key stream:{stream}")
decoded = decode(encoded, stream)
print(f"Plaintext: {decoded}")



def main():
zad2()

if __name__ == '__main__':
main()
72 changes: 72 additions & 0 deletions lab3/zad3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import matplotlib.pyplot as plt


def zad3():
def parity(y_i) -> int:
parity = 0
while True:
parity += y_i & 1
y_i = y_i >> 1
if not y_i:
break
return parity % 2


def blumblumshub(p, q, s):
n = p * q
y = []
z = ""
for i in range(0, n):
y_im1 = s if i == 0 else y[i-1]
y_i = ((y_im1*y_im1) % n)
y.append(y_i)

z_i = parity(y_i)
z += chr(z_i + ord('0'))

plt.title(f"{p=}, {q=}, {s=}")
plt.yscale("log")
plt.plot(y, 'o-', linewidth=0.1, markersize=0.3)
plt.show()

return z[1::]

def search(seq):
max_len = len(seq) // 2
for length in range(1, max_len):
for y in range(0, max_len, length):
if len(seq[y:y+length]) < length:
break
if seq[0:length] != seq[y:y+length]:
break
else:
return length

return 0



print("a: ")
a = blumblumshub(7, 19, 100)
print(f"{a}")
a_cycle = search(a)
print(f"{a_cycle=}")

print("v: ")
b = blumblumshub(67, 71, 100)
print(f"{b}")
b_cycle = search(b)
print(f"{b_cycle=}")

print("v: ")
c = blumblumshub(163, 167, 100)
print(f"{c}")
c_cycle = search(c)
print(f"{c_cycle=}")


def main():
zad3()

if __name__ == '__main__':
main()

0 comments on commit 475af08

Please sign in to comment.