-
Notifications
You must be signed in to change notification settings - Fork 0
/
adfgvx_enc.py
executable file
·145 lines (124 loc) · 3.81 KB
/
adfgvx_enc.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
#!/usr/bin/env python
base_alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
letter_key = ['a', 'd', 'f', 'g', 'v', 'x']
alpha_key = [ ['a', 'b', 'c', 'd', 'e', 'f'], ['g', 'h', 'i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p', 'q', 'r'], ['s', 't', 'u', 'v', 'w', 'x'], ['y', 'z', '0', '1', '2', '3'], ['4', '5', '6', '7', '8', '9'] ]
print "ADFGVX encryption subshell."
print "Base Alphabet:",
print base_alphabet
print "\nBlock Cipher:"
print " ADFGVX"
print " ------"
print "A|",
print "".join(alpha_key[0])
print "D|",
print "".join(alpha_key[1])
print "F|",
print "".join(alpha_key[2])
print "G|",
print "".join(alpha_key[3])
print "V|",
print "".join(alpha_key[4])
print "X|",
print "".join(alpha_key[5])
plain_string=raw_input("Input plaintext string: ").lstrip().rstrip().lower()
trans_string=raw_input("Input transposition string: ").lstrip().rstrip().lower()
print "Parsing input string..."
key_string = ""
for i in range(0,len(plain_string)):
# ignore_flag = 0
char_loc_c = 0
char_loc_r = 0
to_match = plain_string[i]
for j in range(0, len(alpha_key)):
# print "checking keyrow " + str(j) + ":" + "".join(alpha_key[j]) + " for " + to_match
try:
char_loc_c = alpha_key[j].index(to_match)
char_loc_r = j
key_string = key_string + letter_key[char_loc_r] + letter_key[char_loc_c] + " "
except ValueError:
pass
# print "Location of " + to_match + " is " + letter_key[char_loc_r] + "" + letter_key[char_loc_c]
# print "ignore flag is: " + str(ignore_flag)
# key_string = key_string + letter_key[char_loc_r] + letter_key[char_loc_c] + " "
key_string = key_string.rstrip()
#print "Intermediate key string for message is: " + key_string
key_string_list = key_string.split(" ")
key_string_new = "".join(key_string_list)
#print key_string_new
key_len = len(key_string_new)
key_pos = 0
for i in range(0,len(trans_string)):
print trans_string[i],
print
print "- - - - - -"
msg_block_c = list()
while key_pos < key_len:
cur_line = ""
cur_list = list()
for i in range(0,len(trans_string)):
try:
cur_line = cur_line + key_string_new[key_pos] + " "
cur_list.append(key_string_new[key_pos])
except IndexError:
break
key_pos = key_pos + 1
cur_line = cur_line.rstrip()
print cur_line
# print cur_list
msg_block_c.append(cur_list)
#for i in range(0,len(msg_block_c)):
# print msg_block_c[i]
#print len(msg_block_c)
trans_list = list()
msg_block_d = dict()
for i in range(0,len(trans_string)):
cur_line = ""
cur_list = list()
for j in range(0,len(msg_block_c)):
try:
cur_line = cur_line + msg_block_c[j][i] + " "
cur_list.append(msg_block_c[j][i])
except IndexError:
pass
cur_line = cur_line.rstrip()
trans_list.append(trans_string[i])
# print trans_string[i]
# print cur_line
# print cur_list
msg_block_d[trans_string[i]] = cur_list
trans_list.sort()
sorted_string = "".join(trans_list)
print
for i in range(0,len(sorted_string)):
print sorted_string[i],
print
print "- - - - - -"
msg_block_t = list()
for i in range(0,len(msg_block_c)):
cur_line = ""
cur_list = list()
for j in range(0,len(sorted_string)):
try:
cur_line = cur_line + msg_block_d[sorted_string[j]][i] + " "
cur_list.append(msg_block_d[sorted_string[j]][i])
except IndexError:
cur_line = cur_line + " "
cur_list.append(" ")
pass
print cur_line
msg_block_t.append(cur_list)
#print msg_block_t
#final_string = ""
#for i in range(0,len(msg_block_t)):
# final_string = final_string + "".join(msg_block_t[i]) + " "
final_string = ""
for i in range(0,len(sorted_string)):
for j in range(0,len(msg_block_t)):
if msg_block_t[j][i] != " ":
try:
final_string = final_string + msg_block_t[j][i]
except IndexError:
pass
final_string = final_string + " "
print
print "Encrypted message string is: " + final_string