-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsepyrot.py
More file actions
executable file
·102 lines (81 loc) · 2.51 KB
/
sepyrot.py
File metadata and controls
executable file
·102 lines (81 loc) · 2.51 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
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
#!/usr/bin/env python
"""Shebang."""
# -*- coding: utf-8 -*-
import string
import re
from random import randint
import codecs
import sys
import getopt
__version__ = "1.1.5"
def replace_spaces(text):
"""Transform every space of the string into random number."""
nb_spaces = text.count(' ')
for i in range(nb_spaces):
text = text.replace(' ', str(randint(0, 9)), 1)
return text
def replace_numbers(text):
"""Transform every number of the string into space."""
return re.sub('[%s]' % string.digits, ' ', text)
def rot13(text):
"""Apply rot13 encryption to given text."""
text = codecs.encode(text, 'rot_13')
return text
def prevent_doubled_chars(text):
"""Change second of doubled letter with a '+'."""
text_as_list = list(text)
for index in range(len(text_as_list) - 1):
if text_as_list[index] == text_as_list[index + 1]:
text_as_list[index + 1] = "+"
text = ''.join(text_as_list)
return text
def recreate_doubled_chars(text):
"""Transform '+' chars into previous letter."""
text_as_list = list(text)
for index in range(len(text_as_list) - 1):
if text_as_list[index + 1] == '+':
text_as_list[index + 1] = text_as_list[index]
text = ''.join(text_as_list)
return text
def encode(text):
"""Call the different encoding steps."""
text = rot13(text)
text = prevent_doubled_chars(text)
text = replace_spaces(text)
print(text)
sys.exit()
def decode(text):
"""Call the different decoding steps."""
text = rot13(text)
text = recreate_doubled_chars(text)
text = replace_numbers(text)
print(text)
sys.exit()
def show_help():
print('\nusage: -e \"message to encode\" # encode a simple text')
print(' -d \"message to decode\" # decode a simple text\n')
print(' -e \"$(cat file.txt)\" > encoded_file.txt # encode a file')
print(' -d \"$(cat encoded_file.txt)\" # decode a file')
sys.exit(2)
def main(argv):
"""Entrance of the script."""
try:
opts, args = getopt.getopt(
argv,
"e:d:",
[
"encode=",
"decode="
]
)
except getopt.GetoptError as e:
print(str(e))
show_help()
for opt, arg in opts:
if opt in ("-e", "--encode"):
encode(arg)
if opt in ("-d", "--decode"):
decode(arg)
show_help()
if __name__ == '__main__':
main(sys.argv[1:])