-
Notifications
You must be signed in to change notification settings - Fork 0
/
b64.py
56 lines (50 loc) · 2.14 KB
/
b64.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from base64 import b64encode, b64decode
from sys import argv
from binascii import Error as Decode_error
from os.path import isfile as file_exists
if len(argv) == 1 or (len(argv) == 2 and argv[1] == '--help'):
print("Usage:", argv[0], "[options] [message]")
print("{0:6}".format(""), argv[0], "[options] [input_file] [output_file]", end="\n\n")
print("-e, --encode{0:6}encode message or input file".format(""))
print("-d, --decode{0:6}decode message or input file".format(""))
elif len(argv) == 3:
if argv[1] in ('-e', '--encode'):
print(b64encode(argv[2].encode()).decode())
elif argv[1] in ('-d', '--decode'):
try:
print(b64decode(argv[2].encode()).decode())
except Decode_error:
print("Error: Cannot decode message to base64. Check if the message is encoded correctly.")
except UnicodeDecodeError:
print("Error: Ooops! Got strange decoding. Please, try to decode it as file.")
else:
print("Error: Invalid [options] arguments")
elif len(argv) == 4:
message = []
try:
with open(argv[2], "rb") as reading_file:
for line in reading_file:
message.append(line)
except FileNotFoundError:
print("Error: File {} is not found!".format(argv[2]))
exit()
if file_exists(argv[3]):
if input("File {} already exists! Overwrite that file? (y/n): ".format(argv[3])).lower() != 'y':
exit()
if argv[1] in ('-e', '--encode'):
with open(argv[3], 'wb') as writing_file:
for line in message:
writing_file.write(b64encode(line) + b'\n')
elif argv[1] in ('-d', '--decode'):
try:
with open(argv[3], 'wb') as writing_file:
for line in message:
writing_file.write(b64decode(line) + b'\n')
except Decode_error:
print("Error: Cannot decode text in file to base64. Check if the text is encoded correctly.")
else:
print("Error: Invalid [options] arguments")
else:
print("Error: Invalid arguments quantity. See {} --help".format(argv[0]))