-
Notifications
You must be signed in to change notification settings - Fork 1
/
b64.py
executable file
·56 lines (50 loc) · 1.73 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
#
# b64.py - Base64 encode or decode
#
# Copyright (C) 2015 Michael Davies ([email protected])
#
# e.g. Encoding example:
# mrda@carbon:~/src/python$ ./b64.py "Hello There"
# SGVsbG8gVGhlcmU=
#
# e.g. Decoding example
# mrda@carbon:~/src/python$ ./b64.py dec SGVsbG8gVGhlcmU=
# Hello There
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
import base64
import os
import sys
def encode(strings):
s = ' '.join(s for s in strings)
return base64.b64encode(s.encode()).decode("utf-8", "ignore")
def decode(strings):
final = []
for s in strings:
final.append(base64.b64decode(s).decode("utf-8", "ignore"))
return final
if __name__ == "__main__":
if (len(sys.argv) == 1):
progname = os.path.basename(__file__)
sys.exit('Usage: %s [enc|encode|dec|decode] string(s)' % progname)
elif (sys.argv[1] == "enc") or (sys.argv[1] == "encode"):
print(encode(sys.argv[2:]))
elif (sys.argv[1] == "dec") or (sys.argv[1] == "decode"):
print(' '.join(s for s in decode(sys.argv[2:])))
else:
print(encode(sys.argv[1:]))