-
Notifications
You must be signed in to change notification settings - Fork 2
/
vigenere.py
148 lines (115 loc) · 3.99 KB
/
vigenere.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
from __builtin__ import any as b_any
import time
import sys
import thread
import sqlite3
from subprocess import call
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def createTable():
conn = sqlite3.connect('crypto.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS wordlist
(key text, cleartext text, words real, score real)''')
conn.commit()
conn.close()
def addEntry(key, cleartext, words, score):
conn = sqlite3.connect('crypto.db')
c = conn.cursor()
c.execute("INSERT INTO wordlist VALUES ('%s', '%s', %s, %s)" % (key, cleartext, words, score))
conn.commit()
cyphertext = "ZEJFOKHTMSRMELCPODWHCGAW"
# key: DAY
# ctxt: WELCOMETOPROBLEMOFTHEDAY
def crack(keys, dictionary):
with open(keys) as keylist:
for key in keylist:
key = key.strip()
wordCount = 0
score = 0
cleartxt = decryptMessage(key, cyphertext)
for word in dictionary:
word = word.strip()
if (word in cleartxt and (len(word) > 3)):
wordCount = wordCount + 1
score = score + len(word)
if (score > 12):
print("%s %s %s %s \n" % (cleartxt, key, wordCount, score))
addEntry(key, cleartxt, wordCount, score)
print(keys + " IS ALL DONE!")
return 0
def main():
generate_words()
createTable()
englishDictionary = open("english.dic")
dictionary = englishDictionary.readlines()
try:
thread.start_new_thread( crack, ("listaa",dictionary,) )
thread.start_new_thread( crack, ("listab",dictionary,) )
thread.start_new_thread( crack, ("listac",dictionary,) )
thread.start_new_thread( crack, ("listad",dictionary,) )
thread.start_new_thread( crack, ("listae",dictionary,) )
except:
print "Error: unable to start thread"
while 1:
pass
def generate_words():
with open("list.dic","a") as listFile:
print("Generating single char list:")
for char1 in LETTERS:
listFile.write(char1 + "\n")
print("Generating two word:")
for char1 in LETTERS:
for char2 in LETTERS:
listFile.write(char1 + char2 + "\n")
print("Generating three word:")
for char1 in LETTERS:
for char2 in LETTERS:
for char3 in LETTERS:
listFile.write(char1 + char2 + char3 + "\n")
print("Generating four word:")
for char1 in LETTERS:
for char2 in LETTERS:
for char3 in LETTERS:
for char4 in LETTERS:
listFile.write(char1 + char2 + char3 + char4 + "\n")
print("Generating five word:")
for char1 in LETTERS:
for char2 in LETTERS:
for char3 in LETTERS:
for char4 in LETTERS:
for char5 in LETTERS:
listFile.write(char1 + char2 + char3 + char4 + char5 + "\n")
# divide the resulting file into 5 files
call(["wc","-l", "2471326", "list.dic", "list"])
def encryptMessage(key, message):
return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = [] # stores the encrypted/decrypted message string
keyIndex = 0
key = key.upper()
for symbol in message: # loop through each character in message
num = LETTERS.find(symbol.upper())
if num != -1: # -1 means symbol.upper() was not found in LETTERS
if mode == 'encrypt':
num += LETTERS.find(key[keyIndex]) # add if encrypting
elif mode == 'decrypt':
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting
num %= len(LETTERS) # handle the potential wrap-around
# add the encrypted/decrypted symbol to the end of translated.
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
keyIndex += 1 # move to the next letter in the key
if keyIndex == len(key):
keyIndex = 0
else:
# The symbol was not in LETTERS, so add it to translated as is.
translated.append(symbol)
return ''.join(translated)
# If vigenereCipher.py is run (instead of imported as a module) call
# the main() function.
if __name__ == '__main__':
main()