-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodularCipherTests.py
163 lines (122 loc) · 3.54 KB
/
modularCipherTests.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
import math
text = 5*"You've gotta be carefulvirsihin Vipunen kuoli, Lemminkäinen leikkilöihin. data there isn't raukaisen sanaisen arkun, virsilippahan viritän,s oJop' on astuiksen alemma, laskeusi lainehille,n occasion."
text += text[:40]
alphabet = "abcdefghijklmnopqrstuvwxyz"
N = 0
def _patternFromSequence(sequence):
if len(sequence)>30:
return _patternFromSequence(sequence[:len(sequence)//2]) + _patternFromSequence(sequence[len(sequence)//2:])
letterMapping = {}
for c in sequence:
if not c in letterMapping.keys() and sequence.count(c) > 1:
letterMapping[c] = chr(65 + len(letterMapping))
pattern = ""
for c in sequence:
pattern += letterMapping.get(c, ".")
return pattern
def asciify(m):
ret = ""
for c in m:
ret += chr(32+c)
return ret
#c = previous ciphertext char
#p = current plaintext char
#pp = previous plaintext char
def func(c, p, pp):
r = c + 2**(p+pp)
return r%83
def encode(m, init):
ret = [init]
prevC = 0
a=0
for c in m:
c = c.lower()
if c in alphabet:
i = alphabet.index(c)
#ret.append(func(ret[-1], i, prevC))
ret.append((i+ ret[-1]+1)%83)
if prevC == i:
ret[-1] = 3**(ret[-1])
ret[-1] %= 83
prevC = i
return ret
#bucketSizes = [8,1,3,3,12,2,2,4,7,1,1,4,1,7,7,2,1,6,6,8,2,1,1,1,1,1]
bucketSizes = [6,2,2,3,9,2,2,4,5,2,2,4,2,5,5,2,2,5,5,2,2,2,2,2,2,2]
print(sum(bucketSizes))
def encode2(text, offset):
buckets = []
i = 0
for s in bucketSizes:
n = []
for j in range(s):
n.append(i)
i+=1
buckets.append(n)
ret = [2]
cText = []
for c in offset+text:
c = c.lower()
if c in alphabet:
cText.append(alphabet.index(c))
i = 0
prevV = 0
for c,cprev in zip(cText[1:], cText[:-1]):
b = buckets[(c)%26]
v = b[0]
b.remove(v)
b.append(v)
if i>=len(offset):
ret.append((v)%83)
if v%10==0:
buckets[c],buckets[cprev] = buckets[cprev],buckets[c]
prevV = v
i+=1
return ret
def encode3(text, offset):
g1_pattern = [1]
g2_pattern = [1, 0]
g3_pattern = [1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0]
cn = offset
gn = offset
ret = []
prevP = 0
for p in text:
p=p.lower()
if p==" ":
cn=0
if p in alphabet:
pindex = alphabet.index(p)
off = 0
d = (pindex - prevP)%26
if d == 0:
d = 26
for i in range(d):
off += 3*g1_pattern[gn%1] + 4*g2_pattern[gn%2] + 3*g3_pattern[gn%17]
gn+=1
cn += off;
ret.append((cn)%83)
prevP = pindex
return ret
#alphabet = alphabet[::-1]
print(len(text))
A = encode3(text, 3)
B = encode3(text, 14)
#A = encode2(text[:50]+"erwervdfbbbtbt"+text[:50])
#B = encode2(text[:50]+"ergetrgergetggr"+text[:50])
hasCrossDoubles = False
for i in range(len(A)-1):
if A[i]==B[i+1] or B[i]==A[i+1]:
hasCrossDoubles = True
print(asciify(A[:200]))
print(asciify(B[:200]))
print(_patternFromSequence(A[:200]))
print(_patternFromSequence(B[:200]))
#print(len(set(A+B)), list(filter(lambda a: not a in A+B, range(83))))
print("Has Cross-Double:",hasCrossDoubles)
print("Gap Freqs:")
for i in range(20):
a=0
for j in range(len(B)-i):
if B[j] == B[j+i]:
a += 1
print(i, a)