-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher
More file actions
executable file
·134 lines (122 loc) · 4.32 KB
/
cipher
File metadata and controls
executable file
·134 lines (122 loc) · 4.32 KB
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
#!/usr/bin/env python
#"Just because I don't care doesn't mean I don't understand." "Homer S" 0
import sys
import math
from decrypt import with_zero_bis, det_matrix, matrix_inverse, decrypting
from fractions import Fraction
from fractions import gcd
from server import server_lauch
def matrix_create(l_max, c_max, matrix, string, m_type):
i = 0
c = 0
l = 0
while l < l_max:
while c < c_max:
if m_type == 0:
matrix[l][c] = ord(string[i])
else:
matrix[l][c] = string[i]
c = c + 1
i = i + 1
c = 0
l = l + 1
return matrix
def with_zero(string, n):
while len(string) % n != 0:
string = string + str('\0')
return string
def square_zero(string, n):
while len(string) % pow(n, 2) != 0:
string = string + str('\0')
return string
def size_square(string):
n = 2
while (pow(n, 2) < len(string)):
n = n + 1;
return n
def size(string, n):
size = len(string) / n
return size
def print_matrix(l_max, c_max, matrix, type_m):
c = 0
l = 0
while l < l_max:
while c < c_max:
if c < c_max - 1:
if type_m == 0:
print "%.f \t" %(matrix[l][c]),
elif matrix[l][c] == 0:
print "%.1f \t" %(matrix[l][c]),
else:
print "%.3f \t" %(matrix[l][c]),
else:
if type_m == 0:
print "%.f \t" %(matrix[l][c])
elif matrix[l][c] == 0:
print "%.1f \t" %(matrix[l][c])
else:
print "%.3f \t" %(matrix[l][c])
c = c + 1
c = 0
l = l + 1
def matrix_calcul(l, c, matrix_msg, matrix_key, square_size):
result = 0
i = 0
while i < square_size:
result = result + (matrix_msg[i][c] * matrix_key[l][i])
i = i + 1
print str(result),
def crypting(l_max, c_max, matrix_msg, matrix_key):
c = 0
l = 0
while l < l_max:
while c < c_max:
matrix_calcul(l, c, matrix_msg, matrix_key, c_max)
c = c + 1
c = 0
l = l + 1
if len(sys.argv) == 3:
if int(sys.argv[1]) == 2:
suite = raw_input("Are you sure to want launch a server ? (y or n) : ")
if suite == 'y':
print "/!\\ Researching message only with 2*2 key /!\\"
server_lauch(sys.argv[2])
print "/!\\ Stopping research /!\\"
elif len(sys.argv) == 4:
key = sys.argv[3]
flag = int(sys.argv[1])
square_size = size_square(key)
key = square_zero(key, square_size)
matrix_key = [[0] * square_size for _ in range(square_size)]
matrix_key = matrix_create(square_size, square_size, matrix_key, key, 0)
if flag == 0:
msg = sys.argv[2]
msg = with_zero(msg, square_size)
matrix_msg = [[0] * square_size for _ in range(size(msg, square_size))]
matrix_msg = matrix_create(size(msg, square_size), square_size, matrix_msg, msg, 0)
print "Key matrix :"
print_matrix(square_size, size(key, square_size), matrix_key, 0)
print "Encrypted message :"
crypting(size(msg, square_size), square_size, matrix_key, matrix_msg)
elif flag == 1:
msg = [int(x) for x in sys.argv[2].split()]
msg = with_zero_bis(msg, square_size)
matrix_msg = [[0] * square_size for _ in range(size(msg, square_size))]
matrix_msg = matrix_create(size(msg, square_size), square_size, matrix_msg, msg, 1)
det = det_matrix(matrix_key, square_size, 0)
if det == 0:
print "Invalid Key (non inversible)"
else:
det = Fraction(1, det)
matrix_tmp = matrix_key[0][0]
if square_size == 2:
matrix_key[0][0] = det * matrix_key[1][1]
matrix_key[1][1] = det * matrix_tmp
matrix_key[0][1] = det * -1 * matrix_key[0][1]
matrix_key[1][0] = det * -1 * matrix_key[1][0]
else:
matrix_key = matrix_inverse(det, square_size, matrix_key)
print "Key matrix :"
print_matrix(square_size, size(key, square_size), matrix_key, 1)
print "Decrypted message :"
decrypting(size(msg, square_size), square_size, matrix_key, matrix_msg)