@SureshPatil/

## No description

Files
• main.py
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
```
```def c2i(c, alphabet):
return alphabet.index(c)
#"""Returns the index of c in the string alphabet, starting at 0"""
# Copy your method from subcipher.py here

def i2c(i, alphabet):
return alphabet[i]
#"""Returns the character at index i in alphabet"""
# Copy your method from subcipher.py here

def prepare_string(s, alphabet):
newS = ""
for char in s:
if char in alphabet.upper() or char in alphabet.lower():
newS += char.upper()
return newS

def mod_inverse(a, m):
for x in range(1,m):
if((a*x)%m == 1):
return x
#print("No modular inverse found!")
return -1

def determinant(matrix):
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

def isNotCoprime(a, lenAlpha):
if lenAlpha % a == 0:
return True
for x in range(2, a):
if lenAlpha % x == 0 and a % x == 0:
return True
return False

def inverseOfMatrix(matrix, m):
det = determinant(matrix)%m
modInvDet = mod_inverse(det, m)
if modInvDet == -1:
return -1
newMatrix = [[(matrix[1][1]*modInvDet)%m, (-modInvDet*matrix[0][1])%m], [(-modInvDet*matrix[1][0])%m, (matrix[0][0]*modInvDet)%m]]
return newMatrix

def multiply_2x1(matrix1,matrix2, m):
# print(matrix1, matrix2)
newmatrix = [(matrix1[0][0]*matrix2[0] + matrix1[0][1]*matrix2[1])%m, (matrix1[1][0]*matrix2[0] + matrix1[1][1]*matrix2[1])%m]
return newmatrix

def multiply_2x2(matrix1, matrix2, m):
newmatrix = [[(matrix1[0][0]*matrix2[0][0] + matrix1[0][1]*matrix2[1][0])%m, (matrix1[0][0]*matrix2[0][1] + matrix1[0][1]*matrix2[1][1])%m], [(matrix1[1][0]*matrix2[0][0] + matrix1[1][1]*matrix2[1][0])%m, (matrix1[1][0]*matrix2[0][1] + matrix1[1][1]*matrix2[1][1])%m]]
return newmatrix

def hill_encode(encMatrix, plaintext, alphabet):
if inverseOfMatrix(encMatrix, len(alphabet)) == -1:
return 'Error: Matrix not Invertible!'
elif len(plaintext)%2 != 0:
plaintext += "X"
ciphertext = ''
for index in range(0, len(plaintext), 2):
ogMatrix = [c2i(char, alphabet) for char in plaintext[index:index + 2]]
cipherMatrix = multiply_2x1(encMatrix, ogMatrix, len(alphabet))
# print(cipherMatrix)
for index in cipherMatrix:
ciphertext += i2c(index, alphabet)
return ciphertext

def hill_decode(encMatrix, ciphertext, alphabet):
invMatrix = inverseOfMatrix(encMatrix, len(alphabet))
if invMatrix == -1:
return -1
else:
plaintext = ''
for index in range(0, len(ciphertext), 2):
# print(ciphertext)
cipherMatrix = [c2i(char, alphabet) for char in ciphertext[index: index + 2]]
ogMatrix = multiply_2x1(invMatrix, cipherMatrix, len(alphabet))
for index in ogMatrix:
plaintext += i2c(index, alphabet)
return plaintext

def find_encMatrix(crib, ciphertext, alphabet):
for x in range(0, len(crib), 2):
for y in range(x+2, len(crib), 2):
if(len(crib) - y > 2):
pairs = [[c2i(crib[x],alphabet), c2i(crib[y], alphabet)], [c2i(crib[x+1], alphabet), c2i(crib[y+1], alphabet)]]
invOfPairs = inverseOfMatrix(pairs, len(alphabet))
if invOfPairs != -1:
cipherPairs = [[c2i(ciphertext[x],alphabet), c2i(ciphertext[y], alphabet)], [c2i(ciphertext[x+1], alphabet), c2i(ciphertext[y+1], alphabet)]]
encMatrix = multiply_2x2(cipherPairs, invOfPairs, len(alphabet))
return encMatrix
return -1

def hill_solver(crib, ciphertext, alphabet):
encMatrix = find_encMatrix(crib, ciphertext, alphabet)
if encMatrix == -1:
return -1, -1
plaintext = hill_decode(encMatrix, ciphertext, alphabet)
if plaintext != -1:
if crib not in plaintext or plaintext.index(crib) != 0:
return -1, -1
return encMatrix, plaintext

alpha = ''
for i in range(33, 94):
alpha += chr(i)

ids = []
line = line.strip()
ids.append(prepare_string(newId, alpha))
inF.close()

commonPass = []
line = line.strip()
commonPass.append(prepare_string(line, alpha))
inF.close()

encMatrix = []
userIndex = 0
plaintext = -1
plebs = []
for crib in commonPass:
userIndex
encMatrix, plaintext = hill_solver(crib, password, alpha)
plebs = []
if encMatrix != -1:
if plaintext != -1:
for c in commonPass:
if c in plaintext and plaintext.index(c) == 0:
plebs.append([x, plaintext])

if len(plebs) > 2:
print(encMatrix)
break
if len(plebs) > 2:
break

print(plebs)

for x in range(0, len(plebs)):
print(ids[plebs[x][0]], plebs[x][1])

# print(user[0], cracked[0], encMatrices[0])
# index = 1
# for x in range(1, len(cracked)):
#   if 'DAVID' in cracked[x]:
#     index = x
#     break
# print(user[index], cracked[index], encMatrices[index])
# print(len(cracked))```
Fetching token