My problem is, that I have something encoded (base64 like) with a differnet index table:
我的问题是,我有一些带有不同索引表的编码(base64 like):
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/
instead of
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
so when I use base64.b64decode()
it gives me a wrong result. Is there a way to set this table durring conversion (as a parameter maybe)?
所以当我使用base64.b64decode()时,它会给我一个错误的结果。有没有办法在转换时设置此表(可能作为参数)?
Or should I "convert" the wrong base64 string, I mean replace 0 to A, 1 to B, etc... and than use base64decode? if so what is the best and fast workaround for this?
或者我应该“转换”错误的base64字符串,我的意思是将0替换为A,将1替换为B等等...而不是使用base64decode?如果是这样,最好的和快速的解决方法是什么?
update1: I use this, which works, but looks a bit slow, and unprofessional. :)
update1:我使用它,这有效,但看起来有点慢,而且不专业。 :)
def correctbase64(str):
dicta = [ ['0','A'], ['1','B'], ['2','C'], ['3','D'], ['4','E'], ['5','F'], ['6','G'], ['7','H'], ['8','I'], ['9','J'], ['A','K'], ['B','L'], ['C','M'], ['D','N'], ['E','O'], ['F','P'], ['G','Q'], ['H','R'], ['I','S'], ['J','T'], ['K','U'], ['L','V'], ['M','W'], ['N','X'], ['O','Y'], ['P','Z'], ['Q','a'], ['R','b'], ['S','c'], ['T','d'], ['U','e'], ['V','f'], ['W','g'], ['X','h'], ['Y','i'], ['Z','j'], ['a','k'], ['b','l'], ['c','m'], ['d','n'], ['e','o'], ['f','p'], ['g','q'], ['h','r'], ['i','s'], ['j','t'], ['k','u'], ['l','v'], ['m','w'], ['n','x'], ['o','y'], ['p','z'], ['q','0'], ['r','1'], ['s','2'], ['t','3'], ['u','4'], ['v','5'], ['w','6'], ['x','7'], ['y','8'], ['z','9'] ]
l = list(str)
for i in range(len(l)):
for c in dicta:
if l[i] == c[0]:
l[i] = c[1]
break
return "".join(l)
4 个解决方案
#1
7
Something like this should work (WARNING: untested code; may be full of mistakes):
像这样的东西应该工作(警告:未经测试的代码;可能充满错误):
import string
my_base64chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
s = s.translate(string.maketrans(my_base64chars, std_base64chars))
data = base64.b64decode(s)
It isn't possible to make the standard base64
functions (or the lower-level ones in binascii
that they call) use a custom table.
无法使标准base64函数(或者它们调用的binascii中的低级函数)使用自定义表。
#2
3
You can use translate()
and maketrans()
:
你可以使用translate()和maketrans():
from string import maketrans
base64fixTable = maketrans("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
def correctbase64(str):
return str.translate(base64fixTable)
#3
0
Use maketrans to build a translation table and then translate from the first alphabet to the second. Then base64 decode.
使用maketrans构建转换表,然后从第一个字母转换为第二个。然后base64解码。
import string
import base64
def decode(str):
#make a translation table.
table = string.maketrans(
#your alphabet
string.digits + string.uppercase + string.lowercase + "+/",
#the original alphabet
string.uppercase + string.lowercase + string.digits + "+/"
)
#translate
str.translate(s, table)
#finally decode
return base64.b64decode(str)
#4
0
this will handle error TypeError: Incorrect padding
这将处理错误TypeError:不正确的填充
from string import maketrans
import base64
STANDARD_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
CUSTOM_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'
def correctbase64(input):
DECODE_TRANS = maketrans(CUSTOM_ALPHABET, STANDARD_ALPHABET)
newStr = input.translate(DECODE_TRANS)
# Add '=' char at the end of the string
newStr += '='
return base64.b64decode(newStr)
print custom_base64decode('x/Tcw/g') # hello
#1
7
Something like this should work (WARNING: untested code; may be full of mistakes):
像这样的东西应该工作(警告:未经测试的代码;可能充满错误):
import string
my_base64chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
s = s.translate(string.maketrans(my_base64chars, std_base64chars))
data = base64.b64decode(s)
It isn't possible to make the standard base64
functions (or the lower-level ones in binascii
that they call) use a custom table.
无法使标准base64函数(或者它们调用的binascii中的低级函数)使用自定义表。
#2
3
You can use translate()
and maketrans()
:
你可以使用translate()和maketrans():
from string import maketrans
base64fixTable = maketrans("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
def correctbase64(str):
return str.translate(base64fixTable)
#3
0
Use maketrans to build a translation table and then translate from the first alphabet to the second. Then base64 decode.
使用maketrans构建转换表,然后从第一个字母转换为第二个。然后base64解码。
import string
import base64
def decode(str):
#make a translation table.
table = string.maketrans(
#your alphabet
string.digits + string.uppercase + string.lowercase + "+/",
#the original alphabet
string.uppercase + string.lowercase + string.digits + "+/"
)
#translate
str.translate(s, table)
#finally decode
return base64.b64decode(str)
#4
0
this will handle error TypeError: Incorrect padding
这将处理错误TypeError:不正确的填充
from string import maketrans
import base64
STANDARD_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
CUSTOM_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'
def correctbase64(input):
DECODE_TRANS = maketrans(CUSTOM_ALPHABET, STANDARD_ALPHABET)
newStr = input.translate(DECODE_TRANS)
# Add '=' char at the end of the string
newStr += '='
return base64.b64decode(newStr)
print custom_base64decode('x/Tcw/g') # hello