-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcustombase64.py
More file actions
61 lines (46 loc) · 1.99 KB
/
Copy pathcustombase64.py
File metadata and controls
61 lines (46 loc) · 1.99 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
import base64
import string
import random
# A new custom charset
# Change this guy to desired string. Append the "=" char if you also want to possibly change its location.
cuscharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#cuscharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz+/"
#cuscharset = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
#cuscharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
#cuscharset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
#cuscharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
# The standard charset
# If you added an "=" char, or some other char to cuscharst above, make sure to add it here as well.
b64charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
encodedset = string.maketrans(b64charset, cuscharset)
decodedset = string.maketrans(cuscharset, b64charset)
def dataencode(x):
y = base64.b64encode(x)
y = y.translate(encodedset)
return y
def datadecode(x):
y = x.translate(decodedset)
y = base64.b64decode(y)
return y
def randset():
"""Generate a random alphabet for use in base64 encoding"""
x = "".join(random.sample(cuscharset, len(cuscharset)))
global encodedset
global decodedset
encodedset = string.maketrans(b64charset, x)
decodedset = string.maketrans(x, b64charset)
if len(cuscharset) == 64:
print "New random charset: " + x + "="
elif len(cuscharset) == 65:
print "New random charset: " + x
print "Record the above string if you ever want to be able to decode this data again.\n"
# Uncomment the command below to generate a random base64 alphabet.
#randset()
plaintext = "Some string to be base64 encoded."
# Encode the plaintext string
enc = dataencode(plaintext)
#enc = 'WGtECM0UDeq9CGs3eHwGriEFqdYIrjgGvdw5qSEGrTkIrTU4vdrFqcEHpjkIqTC4'
# Decode back into plaintext string
dec = datadecode(enc)
print enc
print dec