-
Notifications
You must be signed in to change notification settings - Fork 0
/
hill_cipher_extra_credit.py
96 lines (84 loc) · 2.39 KB
/
hill_cipher_extra_credit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from sympy import *
import numpy as np
import string
import hill_cipher_extra_credit_samples
init_printing(use_latex='mathjax')
def show_encoding():
alphabet = '_' + string.ascii_uppercase
for c in alphabet:
print("{:>3}".format(c), end='')
print('')
for n in range(len(alphabet)):
print("{:>3}".format(n), end='')
def num2char(numlist):
N = 20
chars = []
for i, n in enumerate(numlist):
n = round(n)
print("{:>4}".format(n), end = ''),
if n == 0:
chars.append('_')
elif n < 0 or n > 26:
chars.append("#")
else:
chars.append(chr(n+64))
if (i+1)%N == 0 or i == len(numlist)-1:
print('')
for c in chars:
print("{:>4}".format(c), end = ''),
print('\n')
chars = []
def num2char_text_only(numlist):
chars = []
for n in numlist:
n = round(n)
if n == 0:
chars.append('_')
elif n < 0 or n > 26:
chars.append("#")
else:
chars.append(chr(n+64))
return "".join(chars)
def char2num(s):
numlist = []
S = s.upper()
for c in S:
if c in string.ascii_uppercase:
numlist.append(ord(c) - ord('A') + 1)
elif c == ' ':
numlist.append(0)
else:
numlist.append(99)
return numlist
def choose_key():
d = 0
while d not in [-3, -2, -1, 1, 2, 3]:
A = Matrix(np.random.randint(0, 10, (3,3)))
d = A.det()
return A, A.inv()
def hill_encoder(text, A=None, show=False):
text = text + ' '*(((-1)*len(text))%3)
nums = Matrix(char2num(text)).reshape(len(text)//3, 3).T
if A != None:
AI = A.inv()
else:
A, AI = choose_key()
cipher = [int(x) for x in list((A*nums).T)]
if show:
return cipher, text, A, AI
else:
return cipher
def simple_num_generator(seed):
a = 1103515245
c = 123456
m = 181
return (a*seed + c) % m
def cipher_generator(pnum):
try:
pnum = int(pnum)
cnum = simple_num_generator(pnum)
cipher = hill_cipher_extra_credit_samples.ciphers[cnum]
except Exception:
print("Error: Invalid format of the UB person number. Enter digits only")
return None
return cipher