def bv2av_or_av2bv(number):
table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'
tr = {}
for i in range(58):
tr[table[i]] = i
s = [11,10,3,8,4,6]
xor = 177451812
add = 8728348608
def dec(number):
r = 0
for i in range(6):
r += tr[number[s[i]]] * 58**i
return 'av{}'.format((r-add)^xor)
def enc(number):
num = int(number[2:])
num = (num^xor) + add
r = list('BV1 4 1 7 ')
for i in range(6):
r[s[i]] = table[num//58**i%58]
return ''.join(r)
if number.strip().lower()[:2] =='bv':
return dec(number)
elif number.strip().lower()[:2] =='av':
return enc(number)
else:
raise Exception('WTF this is invalid number!')
if __name__=='__main__':
bvnum = 'BV17x411w7KC'
avnum = 'av170001'
errornum='asdas123151'
tmp1 = bv2av_or_av2bv(bvnum)
print(tmp1)
tmp2 =bv2av_or_av2bv(avnum)
print(tmp2)
bv2av_or_av2bv(errornum)
结果: