首页 专题 文章 代码 归档

B站的AV号和BV转换【Python+PHP】

1. 缘由

最近B站把原先的AV号转换成了BV号,由于某些原因我们需要AV号和BV号互转;

2. Python源码

# -*- coding: UTF-8 -*-
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'))
print(dec('BV1Q541167Qg'))
print(dec('BV1mK4y1C7Bz'))
print(enc(98791488))
print(enc(455017605))
print(enc(882584971))

3. PHP源码

<?php
class Bilibili{
    protected $table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
    protected $tr = [];
    protected $s = [11, 10, 3, 8, 4, 6];
    protected $xor = 177451812;
    protected $add = 8728348608;

    public function __construct($id=null)
    {
        for ($i = 0; $i < 58; $i++) {
            $this->tr[$this->table[$i]] = $i;
        }
    }

    public function dec($id){
        $r = 0;
        for ($i = 0; $i < 6; $i++) {
            $r += $this->tr[$id[$this->s[$i]]] * 58 ** $i;
        }
        return ($r - $this->add) ^ $this->xor;
    }

    public function enc($id){
        $id = ($id ^ $this->xor) + $this->add;
        $r = str_split('BV1  4 1 7  ');
        for ($i = 0; $i < 6; $i++) {
            $r[$this->s[$i]] = $this->table[floor($id / 58 ** $i) % 58];
        }
        return implode("",$r);
    }
}
?>

好了,仅用于研究用途;

此文阅读完毕,您可以:分享
二维码图片 扫描关注我们哟