Programming$/Crypto

python으로 vigenere solver 짜기

ch4rli3kop 2017. 10. 16. 14:42
반응형
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from math import *
from collections import Counter
 
= open('./vigenere.txt','r')
encode=f.read()
f.close()
 
alphabetDict={1:'a',2:'b',3:'c',4:'d',5:'e',6:'f',7:'g',8:'h',9:'i',10:'j',11:'k',12:'l',13:'m',14:'n',15:'o',16:'p',17:'q',18:'r',19:'s',20:'t',21:'u',22:'v',23:'w',24:'x',25:'y',26:'z'}
 
def getGcd (a,b):
    while(a!=0):
      tmp=b%a
      b=a
      a=tmp
    return abs(b)
 
print '====================================================================================================================='
print '\n   Two letters\n'
print '====================================================================================================================='
print '\n'
 
string2=[]                                      
 
for i in range(1,27):                           
   for j in range(1,27):                         
         s=alphabetDict[i]+alphabetDict[j]       
         string2.append([encode.count(s),s])     
 
index2=[]                     
count=0
cha=[]
gcd2=[]
string2.sort(reverse=True)                       
 
for i in range(0,26*26):
  indexcount=0
  if string2[i][0]>26:               
    print '---------------------------------------------------------------------------------------------------------------------\n'           
    print " string : \"{}\", frequency : {}".format(string2[i][1],string2[i][0])                         
 
    for j in range(0,len(encode)-2):           
 
      if encode[j:j+2]==string2[i][1]:        
        #print j,
        index2.append([])
        index2[count].append(j)            
        indexcount=indexcount+1                
 
    print '\n index :',
    print index2[count]                       
    print '\n'
    cha.append([])
 
    for i in range(0,indexcount-1):
      for j in range(i+1,indexcount):
        cha[count].append(index2[count][j]-index2[count][i])
 
    #print cha[count]
    gcd2.append([])
    maybe=indexcount
 
    if indexcount>6:
      maybe=6
 
    for i in range(0,factorial(maybe-1)-1):
      for j in range(i+1,factorial(maybe-1)):
        gcd2[count].append(getGcd(cha[count][i],cha[count][j]))
 
    gcd_frequency=Counter(gcd2[count]).most_common(6)
    for i in range(0,6):
      print "frequency :{} , gcd : {}".format(gcd_frequency[i][1],gcd_frequency[i][0]) 
 
    print '\n'
    count=count+1
 
print '====================================================================================================================='
print '\n   Three letters\n'
print "====================================================================================================================="
print '\n'
 
string3=[]
 
for i in range(1,27):
   for j in range(1,27):
      for k in range(1,27):
         s=alphabetDict[i]+alphabetDict[j]+alphabetDict[k]
         string3.append([encode.count(s),s])
 
index3=[]         
count=0
cha3=[]
gcd3=[]
string3.sort(reverse=True)
 
for i in range(0,26*26*26):
  indexcount=0
  if string3[i][0]>7:   
    print '---------------------------------------------------------------------------------------------------------------------\n'           
    print " string : \"{}\", frequency : {}".format(string3[i][1],string3[i][0])          
 
    for j in range(0,len(encode)-3):
      if encode[j:j+3]==string3[i][1]:
        #print j,
        index3.append([])
        index3[count].append(j)
        indexcount=indexcount+1
 
    print '\n index : ',
    print index3[count]
    print '\n'
    #print indexcount
    cha3.append([])
 
    for i in range(0,indexcount-1):
      for j in range(i+1,indexcount):
        cha3[count].append(index3[count][j]-index3[count][i])
 
    #print cha3[count]
    gcd3.append([])
    maybe=indexcount
    if indexcount>5:
      maybe=5
 
    for i in range(0,factorial(maybe-1)-1):
      for j in range(i+1,factorial(maybe-1)):
        gcd3[count].append(getGcd(cha3[count][i],cha3[count][j]))
 
    #print gcd3[count]
    gcd_frequency=Counter(gcd3[count]).most_common(6)
 
    for i in range(0,6):
      print "frequency :{} , gcd : {}".format(gcd_frequency[i][1],gcd_frequency[i][0]) 
 
    print '\n'
    count=count+1
 
 
print '***************************************************************************************************************'
print '***************************************************************************************************************'
print '\n'
print '  Through the frequency of the gcd values for difference of each index value, we can guess the key length!\n\n'
keylength=raw_input('  Input the value as you think of as the key length : ')
print '\n'
print '###############################################################################################################'
print '###############################################################################################################'
 
encode=list(encode)
keylength=int(keylength)
 
many=[]
 
for i in range(0,keylength):
  many.append([])
 
for i in range(0,len(encode)):
  many[i%keylength].append(encode[i])
 
#print many
 
key=""
 
split_alphabet_frequency=[]
for i in range(0,keylength):
  split_alphabet_frequency.append([])                                    
 
for i in range(0,keylength):
  for j in range(1,27):                                               
    a=alphabetDict[j]    
    split_alphabet_frequency[i].append([many[i].count(a),a])   
 
 split_alphabet_frequency[i].sort(reverse=True)
  #print split_alphabet_frequency[i]
 
  #print split_alphabet_frequency[i][0][1]
  si=ord(split_alphabet_frequency[i][0][1])-ord('e')
 
  if si < 0:
    si=si+97+26
    key=key+chr(si)
  else:
    si=si+97
    key=key+chr(si)
 
  count=0
 
  for j in many[i]:
    zi=ord(j)-(si-97)
 
    if zi < 97:
      zi=zi+26
 
    many[i][count]=chr(zi)
    count=count+1
 
print "\n"
print "key : " + key
print "\n"
 
decode=""
 
for i in range(0,len(encode)):
  decode=decode+(many[i%keylength][i/keylength])
 
print "decode : \n" + decode
w=open('./decoded vigenere.txt','w')
w.write(decode)
w.close()
cs

카지스키 방식으로 키 길이를 구하여 비제네르 암호를 복호화 시키는 코드를 짰다. 산출된 예상 키 길이를 사용자가 확인하고 직접 입력하는 방식을 사용했다. 키를 출력하고, 복호화되어 나온 원문을 출력한다.


반응형

'Programming$ > Crypto' 카테고리의 다른 글

caesor / mono alphabatic / vigenere  (0) 2020.04.19
python3 AES 암복호화  (0) 2019.10.27
AES-128 암호화 복호화 C로 짜기 (미완본)  (0) 2018.01.01