php mega.nz如何使用lib sodium解密文件属性(文件名)

时间:2018-09-13 12:10:14

标签: php cryptography libsodium

我正在尝试从mega.nz api响应中获取文件名,该响应使用php libsodium函数 sodium_crypto_secretbox_open

具有加密属性

我的代码就是这样

function base64url_encode($data) { 
  return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
} 
function base64url_decode($data) { 
  return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); 
} 
function str_to_a32($b) {
    $padding = (((strlen($b) + 3) >> 2) * 4) - strlen($b);
    if ($padding > 0) {
      $b .= str_repeat("\0", $padding);
    }
    return array_values(unpack('N*', $b));
  }
  function a32_to_str($a) {
    return call_user_func_array('pack', array_merge(array('N*'), $a));
  }
$file='2lIDmCLL';
$key='iEigITqmoDk3WJF4WB_OHZCOrB62f2VNPvifDwhjCV4';

$ch = curl_init('https://eu.api.mega.co.nz/cs');

$data = array(array('a' => 'g', 'g'=>0, 'p' => $file));
$data_string = json_encode($data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

$output = curl_exec($ch);

$res = json_decode($output, true);
print_r($res);

$fn=base64url_decode($res[0]['at']);
echo $fn;//exit;
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

$key=base64url_decode($key);
echo $key;
echo sodium_crypto_secretbox_open($fn, $nonce, $key);

,但不提供文件名。请告诉我如何从api响应中解密at参数。

0 个答案:

没有答案