-
Notifications
You must be signed in to change notification settings - Fork 0
/
rot13ascii.py
executable file
·50 lines (41 loc) · 1.31 KB
/
rot13ascii.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
#!/bin/python3
"""
add 13 to each ascii character -> b64 -> subtract 13 from each ascii character pretty simple (:
"""
import base64
import fileinput
import sys
ROT_AMOUNT = 13
def main():
"""main"""
should_decode = False
if "-d" in sys.argv:
sys.argv.remove('-d')
should_decode = True
text = ""
for line in fileinput.input():
text += line
print(decode(text) if should_decode else encode(text))
# print(rot(13, "I followed the rules"))
return 0
def encode(string):
"""yeah good luck reading this one (:"""
return rot(-ROT_AMOUNT, bytes_to_str(base64.b64encode(bytes(rot(ROT_AMOUNT, string), "UTF-8"))))
def decode(string):
"""the reverse"""
return rot(ROT_AMOUNT, bytes_to_str(base64.b64decode(bytes(rot(-ROT_AMOUNT, string), "UTF-8"))))
def rot(amount, string):
"""rotate string by amount"""
chars = list(string)
for i, char in enumerate(chars):
if char.isalpha():
if char.islower():
chars[i] = chr(ord('a') + (ord(char) - ord('a') + amount) % 26)
else:
chars[i] = chr(ord('A') + (ord(char) - ord('A') + amount) % 26)
return "".join(chars)
def bytes_to_str(byte_array):
"""convert bytearray to string"""
return byte_array.decode("UTF-8")
if __name__ == "__main__":
main()