-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProblem 1.5.1
188 lines (145 loc) · 6.17 KB
/
Problem 1.5.1
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# k=same for all, p (unencrypted letter) and encrypted letter change
# create alphabet -> numeric converter (C->2, _->26)
# 0-26 numeral -> five-bit binary converter (2->[0,0,0,1,1])
# add A and B termwise using GF2 (01010+00010=01000) where A and B are lists of 5 bits
#cyphertext=[0,0,1,1,0]
#key =[0,1,0,1,0]
# A=cyphertext B=key
def decrypter(A,B):
numAlph={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z',26:' '}
C=[A[0]+B[0],A[1]+B[1],A[2]+B[2],A[3]+B[3],A[4]+B[4]]
GF2dict={2:0,1:1,0:0}
#D=[GF2dict[C[0]],GF2dict[C[1]],GF2dict[C[2]],GF2dict[C[3]],GF2dict[C[4]]]
D=[GF2dict[X] for X in C]
integer=(D[0]*16+D[1]*8+D[2]*4+D[3]*2+D[4])
if integer>26:
return("Error!")
else:
return(numAlph[integer])
def phraseDecrypter(B):
A=[[1,0,1,0,1], [0,0,1,0,0], [1,0,1,0,1], [0,1,0,1,1], [1,1,0,0,1], [0,0,0,1,1], [0,1,0,1,1], [1,0,1,0,1], [0,0,1,0,0], [1,1,0,0,1], [1,1,0,1,0]]
return([decrypter(X,B) for X in A])
bins=[0,1]
keys=[[X,Y,Z,U,E] for X in bins for Y in bins for Z in bins for U in bins for E in bins]
def autoDecrypter(keys):
return([phraseDecrypter(Z) for Z in keys])
from GF2 import one
A=[1,0,1,0,1]
B=[1,1,0,1,0]
[x+y for x in A for y in B]
phrase=input("Type a phrase!")
alphNum={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25,' ':26}
[ for k in alphNum for v in phrase in alphNum.items()]
digits=set(range(2))
<<<<<<< HEAD
phrase=input("Input a phrase!")
=======
13
+((X-X%2)/2)+X%2
integer=(16*binary[0]+8*binary[1]+4*binary[2]+2*binary[3]+1*binary[4])
1101
[]
alphNumList=[(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6),(H,7),(I,8),(J,9),(K,10),(L,11),(M,12),(N,13),(N,13),(O,14),(P,15),(Q,16),(R,17),(S,18),(T,19),(U,20),(V,21),(W,22),(X,23),(Y,24),(Z,25),( ,26)]
nums=[]
# function GF2addition that takes two lists of binary values and adds them according to the rules of galois field
# decimalToCharacter converts decimals to characters
# binaryToDecimal
# def Decrypter([0,1,0,0,1],[0,0,1,1,1]):
# [0,1,1,1,0]
# "N"
# Decrypter([1,0,1,0,1],[1,0,0,1,0]) = "G"
cypher=[1,0,1,0,1]
[1,1,1,0,0] key
[0,1,0,0,1] output
X=list(range(0,5,1))
I
output=[ for Z in X]
cypher[0]+key[0]=output[0]
output=[cypher[n]+key[n] for n in X]
outputGF2=[0*X for X in output if X>1]
def Decrypter(cypher,key):
GF2addition=[1,0,1,0,1],[1,1,1,0,0]
def GF2addition(A,B):
C=[A[0]+B[0],A[1]+B[1],A[2]+B[2],A[3]+B[3],A[4]+B[4]]
GF2dict={2:0,1:one,0:0}
D=[GF2dict[C[0]],GF2dict[C[1]],GF2dict[C[2]],GF2dict[C[3]],GF2dict[C[4]]]
return(D)
def binary(L):
integer=(L[0]*16+L[1]*8+L[2]*4+L[3]*2+L[4])
print(integer)
alphNum={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25,' ':26}
print(alphNum.items)
#cyphertext=[0,0,1,1,0]
#key =[0,1,0,1,0]
# A=cyphertext B=key
def decrypter(A,B):
numAlph={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z',26:' '}
C=[A[0]+B[0],A[1]+B[1],A[2]+B[2],A[3]+B[3],A[4]+B[4]]
GF2dict={2:0,1:1,0:0}
#D=[GF2dict[C[0]],GF2dict[C[1]],GF2dict[C[2]],GF2dict[C[3]],GF2dict[C[4]]]
D=[GF2dict[X] for X in C]
integer=(D[0]*16+D[1]*8+D[2]*4+D[3]*2+D[4])
if integer>26:
return("Error!")
else:
return(numAlph[integer])
D=[GF2dict[C[0],C[1],C[2],C[3],C[4]]]
U=len(C)
D=[GF2dict[C[X]] for X in U]
D=[GF2dict[X] for X in C]
#modified decrypter
def bruteForce(A):
B=[]
numAlph={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z',26:' '}
C=[A[0]+B[0],A[1]+B[1],A[2]+B[2],A[3]+B[3],A[4]+B[4]]
GF2dict={2:0,1:1,0:0}
D=[GF2dict[C[0]],GF2dict[C[1]],GF2dict[C[2]],GF2dict[C[3]],GF2dict[C[4]]]
integer=(D[0]*16+D[1]*8+D[2]*4+D[3]*2+D[4])
if integer>26:
return("Error!")
else:
return(numAlph[integer])
def binaryToGF2(X):
if X==1:
X=one
return(X)
elif X!=0:
return("Error!")
else:
return(X)
L=[binaryToGF2(X) for X ]
=======
A=[binaryToGF2(X) for X in cyphertext]
B=[binaryToGF2(X) for X in key]
def phraseDecrypter(B):
A=[[1,0,1,0,1], [0,0,1,0,0], [1,0,1,0,1], [0,1,0,1,1], [1,1,0,0,1], [0,0,0,1,1], [0,1,0,1,1], [1,0,1,0,1], [0,0,1,0,0], [1,1,0,0,1], [1,1,0,1,0]]
return([decrypter(X,B) for X in A])
bins=[0,1]
keys=[[X,Y,Z,U,E] for X in bins for Y in bins for Z in bins for U in bins for E in bins]
def autoDecrypter(keys):
return([phraseDecrypter(Z) for Z in keys])
def decrypter(A,B):
numAlph={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z',26:' '}
C=[A[0]+B[0],A[1]+B[1],A[2]+B[2],A[3]+B[3],A[4]+B[4]]
GF2dict={2:0,1:1,0:0}
#D=[GF2dict[C[0]],GF2dict[C[1]],GF2dict[C[2]],GF2dict[C[3]],GF2dict[C[4]]]
D=[GF2dict[X] for X in C]
integer=(D[0]*16+D[1]*8+D[2]*4+D[3]*2+D[4])
if integer>26:
return("Error!")
else:
return(numAlph[integer])
A=[[1,0,1,0,1], [0,0,1,0,0], [1,0,1,0,1], [0,1,0,1,1], [1,1,0,0,1], [0,0,0,1,1], [0,1,0,1,1], [1,0,1,0,1], [0,0,1,0,0], [1,1,0,0,1], [1,1,0,1,0]]
def condensedDecrypter(A):
bins=[0,1]
B=[[X,Y,Z,U,E] for X in bins for Y in bins for Z in bins for U in bins for E in bins]
numAlph={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X',24:'Y',25:'Z',26:' '}
C=[A[0]+B[0],A[1]+B[1],A[2]+B[2],A[3]+B[3],A[4]+B[4]]
GF2dict={2:0,1:1,0:0}
#D=[GF2dict[C[0]],GF2dict[C[1]],GF2dict[C[2]],GF2dict[C[3]],GF2dict[C[4]]]
D=[GF2dict[X] for X in C]
integer=(D[0]*16+D[1]*8+D[2]*4+D[3]*2+D[4])
if integer>26:
return("Error!")
else:
return(numAlph[integer])