Skip to content

Commit

Permalink
Add lab2
Browse files Browse the repository at this point in the history
  • Loading branch information
vis4rd committed Oct 10, 2023
1 parent 1cc0e5a commit 27b6048
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.linting.pycodestyleEnabled": false
}
39 changes: 39 additions & 0 deletions lab2/zad1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def add_z26(a, b):
return (a + b) % 26

def zad1():
plaintext = "thiscryptosystemisnotsecure"
key = "CIPHER"
print("key: " + key)

def get_next_key_char(index):
return key[index % len(key)]

print("Encryption")
output = ""
for index, c in enumerate(plaintext):
text_as_num = ord(c) - ord('a')
key_as_num = ord(get_next_key_char(index)) - ord('A')

output += chr(add_z26(text_as_num, key_as_num) + ord('A'))

print("Plaintext: " + plaintext)
print("Cyphertext: " + output)

print("\nDecryption")
output2 = ""
for index, c in enumerate(output):
text_as_num = ord(c) - ord('A')
key_as_num = ord(get_next_key_char(index)) - ord('A')

output2 += chr(((text_as_num - key_as_num) % 26) + ord('a'))

print("Plaintext: " + output)
print("Cyphertext: " + output2)


def main():
zad1()

if __name__ == '__main__':
main()
62 changes: 62 additions & 0 deletions lab2/zad2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import numpy as np

def to_num(c):
return ord(c) - ord('a')

def multi_inverse(b, n):
r1 = n
r2 = b
t1 = 0
t2 = 1

while(r1 > 0):
q = int(r1/r2)
r = r1 - q * r2
r1 = r2
r2 = r
t = t1 - q * t2
t1 = t2
t2 = t

if(r1 == 1):
inv_t = t1
break

return inv_t

def zad2():
plaintext = "july"
key = np.array([[11, 8], [3, 7]])

det = int(np.linalg.det(key))
plaintext_num = [to_num(c) for c in plaintext]
text_matrix = np.array(plaintext_num).reshape(2, 2)

print(f"det = {det}")

print("Encryption")
print("Plaintext: " + plaintext)

output_num = np.dot(text_matrix, key) % 26
output = ''.join([chr(c + ord('A')) for c in output_num.flatten()])

print("Cyphertext: " + output)

print("\nDecryption")
print("Plaintext: " + output)

plaintext_num = [(ord(c) - ord('A')) for c in output]
text_matrix = np.array(plaintext_num).reshape(2, 2)

inv_key = np.array([[7, 18], [23, 11]])

output_num = np.dot(text_matrix, inv_key) % 26
output = ''.join([chr(c + ord('a')) for c in output_num.flatten()])

print(f"Cyphertext: {output}")

def main():
zad2()

if __name__ == '__main__':
main()
46 changes: 46 additions & 0 deletions lab2/zad3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def to_num(c):
return ord(c) - ord('A')

def to_num2(c):
return ord(c) - ord('a')

def zad3():
plaintext = "TGEEMNELNNTDROEOAAHDOETCSHAEIRLM"
permutation = {
1: 2, 2: 4, 3: 6, 4: 1, 5: 8, 6: 3, 7: 5, 8: 7
}
inv_permutation = {v: k for k, v in permutation.items()}

print("Decryption")
print("Cyphertext: " + plaintext)

plaintext_num = [to_num(c) for c in plaintext]
plaintext_splits = [plaintext_num[i:i+8] for i in range(0, len(plaintext_num), 8)]
output = [0] * len(plaintext_num)
for index, split in enumerate(plaintext_splits):
for i in range(len(split)):
output[index*8 + i] = split[permutation[i + 1]-1]

output_text = ''.join([chr(c + ord('a')) for c in output])
print("Plaintext: " + output_text)

print("Encryption")
print("Plaintext: " + output_text)

output_text_num = [to_num2(c) for c in output_text]
output_text_splits = [output_text_num[i:i+8] for i in range(0, len(output_text_num), 8)]
output2 = [0] * len(output_text_num)
for index, split in enumerate(output_text_splits):
for i in range(len(split)):
output2[index*8 + i] = split[inv_permutation[i + 1]-1]

output2_text = ''.join([chr(c + ord('A')) for c in output2])
print("Cyphertext: " + output2_text)



def main():
zad3()

if __name__ == '__main__':
main()
15 changes: 15 additions & 0 deletions lab2/zad4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def zad4():
cypher = "BEEAKFYDJXUQYHYJIQRYHTYJIQFBQDUYJIIKFUHCQD"
outputs = [""] * 26

for i in range(1, 26):
outputs[i] = ''.join([chr((ord(c) - ord('A') + i) % 26 + ord('a')) for c in cypher])
print(f"{i}: {outputs[i]}")

print(f"plaintext: {outputs[10]}")

def main():
zad4()

if __name__ == '__main__':
main()
29 changes: 29 additions & 0 deletions lab2/zad5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random

def crypt(letter, key):
return chr((ord(letter) - ord('a') + key) % 26 + ord('a'))

def zad5():
results = []
for i in range(1000):
random_length = random.randint(5, 100)
random_key = random.randint(0, 25)

random_plaintext = ''.join([chr(random.randint(0, 25) + ord('a')) for _ in range(random_length)])
encrypted = ''.join([crypt(c, random_key) for c in random_plaintext])

for i in range(1, 26):
decrypted = ''.join([crypt(c, -i) for c in encrypted])

if decrypted == random_plaintext:
results.append(i)
break

print(f"average key: {sum(results) / len(results)}")


def main():
zad5()

if __name__ == '__main__':
main()
32 changes: 32 additions & 0 deletions lab2/zad6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np

def crypt(letter_num, key):
return (letter_num + key) % 26

def zad6():
cypher = "BEEAKFYDJXUQYHYJIQRYHTYJIQFBQDUYJIIKFUHCQD"
key_frequency_table = "ETAOINSHRDLCUMWFGYPBVKJXQZ"

print(f"cypher: {cypher}")
print(f"frequency_table: {key_frequency_table}")

cypher_frequency_table = []
for i in range(26):
cypher_frequency_table.append(cypher.count(chr(i + ord('A'))))

argmax = np.argmax(cypher_frequency_table)

print(f"Most common letter: {chr(argmax + ord('A'))} with {cypher_frequency_table[argmax]} instances")

cypher_num = [ord(c) - ord('A') for c in cypher]
for letter in key_frequency_table:
key = (argmax - (ord(letter) - ord('A'))) % 26
plaintext = ''.join([chr(crypt(c, -key) + ord('a')) for c in cypher_num])
print(f"Potential key: {key} -> {plaintext}")


def main():
zad6()

if __name__ == '__main__':
main()

0 comments on commit 27b6048

Please sign in to comment.