-
Notifications
You must be signed in to change notification settings - Fork 2
/
rot47.py
145 lines (129 loc) · 7.45 KB
/
rot47.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/python3
import argparse
from utilities.colors import color
from utilities.tools import clear
# encryption/decryption in same function - rotates the character ASCIIs by 47 % 94 to keep them within valid range
def transform(msg):
# list with the ASCII values of the plaintext
msgASCII = [ord(char) for char in msg] # char to ASCII
transASCII = []
for ch in msgASCII:
if ch in range(33,127):
transASCII.append(33+((ch+14)%94))
else:
transASCII.append(ch)
transform = ''.join(map(chr,transASCII))
return transform
def parsefile(filename):
message = ''
try:
with open(filename) as f:
for line in f:
message+=line
except FileNotFoundError:
print('{}[-] File not found{}\n{}[!] Please make sure the file with the filename exists in the current working directory{}'.format(color.RED,color.END,color.YELLOW,color.END))
quit()
return message
def run():
try:
clear()
# prompt for choice of action
choice = input('{}[?]{} Encrypt or Decrypt? [e/d] : '.format(color.BLUE,color.END))
if choice == 'e' or choice == 'E':
# whether to load a file for the plaintext or type it from the console
filechoice = input('{}[?]{} Load from a file? [y/N] : '.format(color.BLUE,color.END)).lower()
if filechoice != 'y':
pt = input('{}[?]{} Enter the Plaintext : '.format(color.BLUE,color.END)) # plaintext input
else:
filename = input('{}[?]{} Enter the filename: '.format(color.BLUE,color.END))
pt = parsefile(filename)
ciphertext = transform(pt) # calling the enc() function with the input
print('{}[+]{} The Ciphertext is : {}{}{}'.format(color.GREEN,color.END,color.RED,ciphertext,color.END))
elif choice == 'd' or choice == 'D':
# whether to load a file for the plaintext or type it from the console
filechoice = input('{}[?]{} Load from a file? [y/N] : '.format(color.BLUE,color.END)).lower()
if filechoice != 'y':
ct = input('{}[?]{} Enter the Ciphertext : '.format(color.BLUE,color.END)) # ciphertext input
else:
filename = input('{}[?]{} Enter the filename: '.format(color.BLUE,color.END))
ct = parsefile(filename)
plaintext = transform(ct) # calling dec() function with the input
print('{}[+]{} The Plaintext is : {}{}{}'.format(color.GREEN,color.END,color.RED,plaintext,color.END))
else:
print('{}[-] Please provide a valid coice of action{}'.format(color.RED,color.END))
quit()
except KeyboardInterrupt:
print('\n{}[!] Exiting...{}'.format(color.RED,color.END))
# main driver function
# parses arguments
# prompts the user for necessary inputs if arguments not provided
def main():
try:
clear()
# script description
parser = argparse.ArgumentParser(description='ROT47 Encryption & Decryption')
# encryption group option (single option --encrypt)
enc_group = parser.add_argument_group('Encryption Options')
enc_group.add_argument('-e','--encrypt', help='Encrypt a given Plaintext', default=False, action='store_true')
# decryption group options (--decrypt and --brute)
dec_group = parser.add_argument_group('Decryption Options')
dec_group.add_argument('-d','--decrypt', help='Decrypt a given Ciphertext', default=False, action='store_true')
# file option - whether to load from a file
parser.add_argument('-f','--file', help='Load the Plaintext/ Ciphertext from a file', default=False, action='store_true')
# message (either plain or cipher) - handled later on based on options
parser.add_argument('TEXT', help='Plaintext or Ciphertext (based on mode)')
try: # if all options and positional argument (TEXT) provided
args = parser.parse_args()
except: # if positional argument TEXT not provided - prompts user with necessary options
# prompt for choice of action
choice = input('{}[?]{} Encrypt or Decrypt? [e/d] : '.format(color.BLUE,color.END))
if choice == 'e' or choice == 'E':
# whether to load a file for the plaintext or type it from the console
filechoice = input('{}[?]{} Load from a file? [y/N] : '.format(color.BLUE,color.END)).lower()
if filechoice != 'y':
pt = input('{}[?]{} Enter the Plaintext : '.format(color.BLUE,color.END)) # plaintext input
else:
filename = input('{}[?]{} Enter the filename: '.format(color.BLUE,color.END))
pt = parsefile(filename)
ciphertext = transform(pt)
print('{}[+]{} The Ciphertext is : {}{}{}'.format(color.GREEN,color.END,color.RED,ciphertext,color.END))
elif choice == 'd' or choice == 'D':
# whether to load a file for the plaintext or type it from the console
filechoice = input('{}[?]{} Load from a file? [y/N] : '.format(color.BLUE,color.END)).lower()
if filechoice != 'y':
ct = input('{}[?]{} Enter the Ciphertext : '.format(color.BLUE,color.END)) # ciphertext input
else:
filename = input('{}[?]{} Enter the filename: '.format(color.BLUE,color.END))
ct = parsefile(filename)
plaintext = transform(ct)
print('{}[+]{} The Plaintext is : {}{}{}'.format(color.GREEN,color.END,color.RED,plaintext,color.END))
else:
print('{}[-] Please provide a valid coice of action{}'.format(color.RED,color.END))
quit()
# parsing command line argumets (provided the necvessary ones are given)
if args.encrypt: # if encrypt flag is on
if args.decrypt: # decrypt flag should be off
print('{}[-] Please select only one option among Encrypt or Decrypt at a time{}'.format(color.RED,color.END))
quit()
else: # good to go - call enc() function and display result
if args.file:
pt = parsefile(args.TEXT)
ciphertext = transform(pt)
else:
ciphertext = transform(args.TEXT)
print('{}[+]{} The Ciphertext is : {}{}{}'.format(color.GREEN,color.END,color.RED,ciphertext,color.END))
elif args.decrypt: # if decrypt flag is on
if args.file:
ct = parsefile(args.TEXT)
plaintext = transform(ct)
else:
plaintext = transform(args.TEXT)
print('{}[+]{} The Plaintext is : {}{}{}'.format(color.GREEN,color.END,color.RED,plaintext,color.END))
# if no arguments are provided except for positional (TEXT)
else:
print('{}[-] At least one of Encryption or Decryption action is required{}'.format(color.RED,color.END))
except KeyboardInterrupt:
print('\n{}[!] Exiting...{}'.format(color.RED,color.END))
quit()
if __name__ == '__main__':
main()