B站 av和bv号互转 py和js版 bigint

2021-01-31 00:16

阅读:667

标签:turn   pre   ber   log   word   https   range   print   lis   

参考

https://www.zhihu.com/question/381784377/answer/1099438784

 

py的可以直接使用

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(x):
	r=0
	for i in range(6):
		r+=tr[x[s[i]]]*58**i
	return (r-add)^xor

def enc(x):
	x=(x^xor)+add
	r=list(‘BV1  4 1 7  ‘)
	for i in range(6):
		r[s[i]]=table[x//58**i%58]
	return ‘‘.join(r)

print(dec(‘BV17x411w7KC‘)) # 170001
print(dec(‘BV1Q541167Qg‘)) # 455017605
print(dec(‘BV1mK4y1C7Bz‘)) # 882584971
print(enc(170001)) # BV17x411w7KC
print(enc(455017605)) # BV1Q541167Qg
print(enc(882584971)) # BV1mK4y1C7Bz

 

js版的需要做一些修改, 主要是使用bigint做计算, 因为其中涉及的数值计算太大了, 普通的会溢出

const table = ‘fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF‘

const tr = table.split(‘‘).reduce(
  (pre, v, i) => {
    pre[table[i]] = BigInt(i)
    return pre
  }, []
)

let s = [11, 10, 3, 8, 4, 6].map(i => BigInt(i))
let xor = BigInt(177451812);
let add = BigInt(8728348608)
const NUM_58 = BigInt(58)

function dec(x) {
  let r = BigInt(0);
  for (let i = 0; i 6; i++) {
    r += tr[x[s[i]]] * NUM_58 ** BigInt(i);
  }
  let res = (r - add) ^ xor
  return res.toString();
}


function enc(x) {
  x = BigInt(x)
  x = (x ^ xor) + add
  let r = ‘BV1  4 1 7  ‘.split(‘‘)
  for (let i = 0; i 6; i++) {
    r[s[i]] = table [x / NUM_58 ** BigInt(i) % NUM_58]
  }
  return r.join(‘‘)
}


console.log(dec(‘BV17x411w7KC‘)) // 170001
console.log(dec(‘BV1Q541167Qg‘)) // 455017605
console.log(dec(‘BV1mK4y1C7Bz‘)) // 882584971
console.log(enc(170001)) // BV17x411w7KC
console.log(enc(455017605)) // BV1Q541167Qg
console.log(enc(882584971)) // BV1mK4y1C7Bz
console.log(enc(498566183)) // BV1AK411W7wq
console.log(dec(‘BV1Ft4y197pr‘)) // 626046351

 推荐:https://www.xmtpost.com/508.html

B站 av和bv号互转 py和js版 bigint

标签:turn   pre   ber   log   word   https   range   print   lis   

原文地址:https://www.cnblogs.com/1994july/p/13192723.html


评论


亲,登录后才可以留言!