统计
  • 建站日期:2021-03-10
  • 文章总数:10192 篇
  • 评论总数:216 条
  • 分类总数:56 个
  • 最后更新:4月19日
文章 未分类

【记录(旧)】qq视频真实地址获取 ckey算法PHP版本。

小天
首页 未分类 正文
<?php
class tea {
  private $seed = 0xdead;
  private $maxf = 0xffffffff;
  private $token = [0xfa, 0x82, 0xde, 0xb5, 0x2d, 0x4b, 0xba, 0x31, 0x39, 0x6,  0x33, 0xee, 0xfb, 0xbf, 0xf3, 0xb6];

  public function __construct() {
  }
  private function str_sum($s) {
    $n = 0;
    foreach($s as $i) {
      $n = $this->maxf & intval($n * 131 + $i);
    }
    return 0x7fffffff & $n;
  }

  private function pack_str($t, $d) {
    $l = strlen($d);
    $t[] = ($l & 0xff00) >> 8;
    $t[] = $l & 0xff;
    for($i=0;$i < $l;$i++) {
      $t[] = ord($d[$i]);
    }
    return $t;
  }

  private function rands(){
    if ($this->seed == 0) {
      $this->seed = 123459876;
    }
    $k1 = $this->maxf & (-2836 * (floor($this->seed / 127773)));
    $k2 = $this->maxf & (16807 * (floor($this->seed % 127773)));
    $this->seed = $this->maxf & ($k1 + $k2);
    if ($this->seed < 0) {
      $this->seed += PHP_INT_MAX;
    }
    return $this->seed;
  }

  private function new_pack($v, $d) {
    foreach($d as $s) {
      $k = str_split(pack("I", $s));
      krsort($k);
      foreach($k as $c) {
        $v[] = ord($c);
      }
    }
    return $v;
  }

  private function new_unpack($d) {
    $str = '';
    $ret = [];
    foreach($d as $b) $str .= chr($b);
    $t = range(0, strlen($str), 4);
    for($i=0;$i < count($t) - 1;$i++) {
      $s = substr($str, $t[$i], 4);
      $n = unpack('N', $s)[1];
      if ($n < 0){
        $n += 4294967296;
      }
      $ret[] = $n;
    }
    return $ret;
  }

  private function tea_encrypt($v, $k) {
    $s = (integer) 0;
    $k = $this->new_unpack($k);
    $v = $this->new_unpack($v);
    $bjz_int = PHP_INT_MAX;
    for($i=0;$i < 16;$i++){
      $s += 0x9e3779b9;
      $s &= $this->maxf;
      $v[0] += ($s + $v[1]) ^ (($v[1] >> 5) + $k[1]) ^ (($v[1] << 4) + $k[0]);
      $v[0] &= $this->maxf;
      $v[1] += ($s + $v[0]) ^ (($v[0] >> 5) + $k[3]) ^ (($v[0] << 4) + $k[2]);
      $v[1] &= $this->maxf;
    }
    return $this->new_pack([], $v);
  }

  private function oi_symmetry_encrypt2($raw, $key) {
    $data = [];
    $temp = [0, 0, 0, 0, 0, 0, 0, 0];
    $pad_salt_body_zero_len = count($raw) + 10;
    $pad_len = $pad_salt_body_zero_len % 8;
    if ($pad_len) $pad_len = 8 - $pad_len;
    array_push($data, $this->rands() & 0xf8 | $pad_len);
    while($pad_len + 2) {
      array_push($data, $this->rands() & 0xff);
      $pad_len = $pad_len - 1;
    }
    foreach($raw as $rd) $data[] = $rd;
    array_push($data, 0, 0 ,0, 0, 0, 0, 0);
    $dt = array_slice($data, 0, 8);
    $enc = $this->tea_encrypt($dt, $key);
    $length = count($data);
    $d = range(8, $length, 8);
    foreach($d as $i) {
      if ($i != $length) {
        $dk = array_slice($data, $i);
        for($j=0;$j < 8;$j++) {
          $dk[$j] = $dk[$j] ^ $enc[$i - 8 + $j];
        }
        $dk = $this->tea_encrypt($dk, $key);
        for($j=0;$j < 8;$j++) {
          $dk[$j] = ($dk[$j] ^ $data[$i - 8 + $j] ^ $temp[$j]);
          array_push($enc, $dk[$j]);
          $temp[$j] = $enc[$i - 8 + $j];
        }
      }
    }
    return $enc;
  }

  public function getCkey($vid, $ver = '3.2.19.356', $platform = 1400603, $stdfrom = 'bcng') {
    $out = '';$data = [];$tm = $_SERVER['REQUEST_TIME'];
    $guid = '21F9945F5114779E123F0E34C7B8E54500BA5E369';
    $data = $this->new_pack($data, [21507, 3168485562]);
    $data = $this->new_pack($data, [$platform]);
    $data = $this->new_pack($data, [9999, $tm]);
    $data = $this->pack_str($data, $stdfrom);
    $data = $this->pack_str($data, rand());
    $data = $this->pack_str($data, $ver);
    $data = $this->pack_str($data, $vid);
    $data = $this->pack_str($data, $guid);
    array_push($data, 0, 1, 52, 0, 1, 52, 0, 0, 0, 1, 0, 0, 0, 0);
    $len = count($data);
    array_unshift($data, $len & 0xff);
    array_unshift($data, ($len & 0xff00) >> 8);
    $enc = $this->oi_symmetry_encrypt2($data, $this->token);
    $pad = [0, 0, 0, 0, 0xff & $this->rands(), 0xff & $this->rands(), 0xff & $this->rands(), 0xff & $this->rands()];
    $pad[0] = $pad[4] ^ 71 & 0xff;
    $pad[1] = $pad[5] ^ -121 & 0xff;
    $pad[2] = $pad[6] ^ -84 & 0xff;
    $pad[3] = $pad[7] ^ -86 & 0xff;
    foreach($enc as $b) $pad[] = $b;
    $pad = $this->new_pack($pad, [$this->str_sum($data)]);
    foreach($pad as $b) $out .= chr($b);
    $out = base64_encode($out);
    $out = str_replace('=', '', $out);
    $out = str_replace('+', '_', $out);
    $out = str_replace('/', '-', $out);
    return urlencode($out);
  }
}

$t = new tea();
$ckey = $t->getCkey(empty($_GET['vid']) ? "x0015remj2b" : trim($_GET['vid']));
echo "{"ckey":"$ckey"}";

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

-- 展开阅读全文 --
这篇文章最后更新于2022-3-13,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
优酷视频m3u8解析PHP源码(截至2020-06-30 可用)
« 上一篇
【记录】华数视频地址加密方式算法PHP版本
下一篇 »
为了防止灌水评论,登录后即可评论!

已有 1 条评论

  1. 8sTkzEXF Lv.1
    2024-03-11 13:01         Android /     Google Chrome

    说道:111111111

HI ! 请登录
注册会员,享受下载全站资源特权。