-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSD.py
More file actions
71 lines (60 loc) · 1.75 KB
/
SSD.py
File metadata and controls
71 lines (60 loc) · 1.75 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
print """
##############################################################
# Alvaro Reyes - Simple Sustitution Decipher #
##############################################################
# python SSD.py cipher.txt frequency.txt #
##############################################################
"""
if len(sys.argv) is 1:
print "Howto: enter ciphertext and frequency file and you will get the translation"
print "Frequency file must have only two lines: frequency of ciphertext and frequency of language"
print "Example of frequency file:"
print "THBONKFREGJVUMIYDALCSQWZPX <- Frequency of ciphertext"
print "EAORSNITUCMDLPGQBYVAZFJXKW <- Frequency of language"
print "You can use the frequencyanalyzer.py to get the frequency file"
exit()
if len(sys.argv)!=3:
print "Error: incorrect no. of arguments"
print "Correct input is: cipher.txt frequency.txt"
exit()
try:
frequencies = open(sys.argv[2],"rb").readlines()
except:
print "Failed to read frequency file"
exit()
frec_tmp = []
for f in frequencies:
frec_tmp.append(f.replace("\n","").replace("\r",""))
frequencies = frec_tmp
frec = frequencies[0].upper()
abc = frequencies[1].upper()
dicc = zip(frec.upper(),abc.upper())
def getCipherText(path):
try:
op = open(path,"rb")
text = (op.read()).replace("\n","").replace("\r","")
return text.upper()
except:
print "Failed to read cipher text"
exit()
def fixedChar(l):
for tupla in dicc:
if tupla[0]==l:
return tupla[1]
else:
pass
return l
word = getCipherText(sys.argv[1])
print word + " -> "
print ""
plaintext = ""
for char in word:
plaintext=plaintext+fixedChar(char)
print plaintext
print ""
print "Text. freq. "+frec
print "Dicc. freq.: "+abc