Skip to content

Commit e537838

Browse files
committed
Added Letter to templar's challenge solution script
1 parent 18444f0 commit e537838

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

Mystery Twister Solutions/Caesar Brute Force.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/python3.4
2-
# Written by Anirudh Anand: email - [email protected]
2+
# Written by Anirudh Anand (lucif3r) : email - [email protected]
33

44
# Caesar Brute force.py: A python program to Crack caesar/Rotational ciphers
55
# This is a simple python program which helps in cracking Caesar/rotational
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/python3.4
2+
# Written by Anirudh Anand (lucif3r) : email - [email protected]
3+
4+
# Letter to Templars.py: A python program to de-cipher strings using keys
5+
# This is a simple python program which helps in cracking Ciphers based on
6+
# keys. This is written in general to work with any length of keys but is
7+
# originally written to crack the challenge: Letter to Templars series in
8+
# Mystery Twister. Challenge url: http://goo.gl/nwzTr1
9+
10+
__author__ = 'lucif3r'
11+
12+
import itertools
13+
import argparse
14+
15+
16+
class Templar:
17+
def __init__(self):
18+
self.plain = ""
19+
20+
def crack(self, cipher, key, outfile=""):
21+
"""
22+
This function will intake the cipher text, key and the optional
23+
argument outfile (file to which output should be written). The
24+
function will first calculate all the possible permutations of
25+
the given key and use every single one of them to brute force
26+
the cipher.
27+
28+
:type outfile: str
29+
:param cipher: The cipher text
30+
:param key: Sample key/string which should be used for permuting
31+
:param outfile: File to which output should be written (optional)
32+
:return:
33+
"""
34+
keys = self.permute_keys(key)
35+
for i in keys:
36+
j = -1
37+
while j < len(cipher) - len(key) + 1:
38+
self.plain += cipher[j + int(i[0])]
39+
self.plain += cipher[j + int(i[1])]
40+
self.plain += cipher[j + int(i[2])]
41+
self.plain += cipher[j + int(i[3])]
42+
j += len(key)
43+
self.print_args(i, outfile)
44+
self.plain = ""
45+
return self.plain
46+
47+
def print_args(self, key, outfile=" "):
48+
"""
49+
This will check if an output file is specified in the arguments
50+
if so, then the output will be written to the file or else it
51+
will be printed on the terminal
52+
53+
:param key:
54+
:param outfile: File to which output should be written
55+
:return:
56+
"""
57+
if outfile == " ":
58+
print("Key: ", key)
59+
print("Decrypted String: ", self.plain)
60+
return
61+
else:
62+
file = open(outfile, "a")
63+
file.write("\nkey: " + key + "\nDecrypted String: " + self.plain + "\n")
64+
return
65+
66+
def permute_keys(self, words):
67+
"""
68+
This function will return all the permutations of the key which
69+
is used to decrypt the cipher.
70+
71+
:type words: list
72+
:param words: The words which is used to permute
73+
:return:
74+
"""
75+
list_pass = [''.join(i) for i in itertools.permutations(words)]
76+
print(len(list_pass))
77+
return list_pass
78+
79+
80+
def main():
81+
c = Templar()
82+
parser = argparse.ArgumentParser(description='To decipher a string using the given key')
83+
parser.add_argument('-k', '-key', type=str, default=0,
84+
help='Enter a sample key (we will permutate and give results of all possibilities)')
85+
parser.add_argument('-c', '-cipher', type=str, default=" ",
86+
help='File in which the cipher is saved')
87+
parser.add_argument('-o', '-output', type=str, default=" ",
88+
help='Output the result to a file')
89+
args = parser.parse_args()
90+
91+
if args.k == 0:
92+
print("Please enter a valid key. Use -h to see help commands")
93+
exit(0)
94+
if args.c == " ":
95+
print("A valid file containing cipher is required. Use -h to see help commands")
96+
exit(0)
97+
else:
98+
try:
99+
cipher = open(args.c, "r").readline()
100+
except FileNotFoundError:
101+
print("File not Found. Please enter a valid file")
102+
103+
c.crack(cipher, args.k, args.o)
104+
return
105+
106+
107+
if __name__ == '__main__':
108+
main()

0 commit comments

Comments
 (0)