PHP¶
<?php
class php_decry
{
/**
* 初始化
* @param $public_key 企业access_token
*/
public function init_box($public_key)
{
$Box = range(0, 255);
$key_length = strlen($public_key);
$j = 0;
foreach ($Box as $key) {
$index = ord($public_key[($key % $key_length)]);
$j = ($j + $Box[$key] + $index) % 256;
list(
$Box[$key],
$Box[$j]
) = array($Box[$j], $Box[$key]);
}
return $Box;
}
/**
* 加密/解密
* @param $string 待加/解密的字符串
*/
public function do_crypt($Box, $string)
{
$index_i = 0;
$index_j = 0;
$out = array();
for ($i = 0; $i < strlen($string); $i++) {
$index_i = ($index_i + 1) % 256;
$index_j = ($index_j + $Box[$index_i]) % 256;
list(
$Box[$index_i],
$Box[$index_j]
) = array($Box[$index_j], $Box[$index_i]);
$r = ($Box[$index_i] + $Box[$index_j]) % 256;
$R = $Box[$r];
$out[] = chr(ord($string[$i]) ^ $R);
}
$res = join("", $out);
return $res;
}
/**
* 用于请求微信get方法
* @param string $url url地址
*/
public function urllib_get($url, $json = true)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$output = curl_exec($ch);
curl_close($ch);
if ($json) {
$return_content = json_decode($output);
} else {
$return_content = $output;
}
return $return_content;
}
/**
* 开始执行
*/
public function run()
{
$corp_secret = "{你的企业的corp_secret}";
$access_token = "{你的企业的access_token}";
$response =$this->urllib_get('https://openapi.2haohr.com/api/departments/?access_token=' . $access_token);
$base64_text = base64_decode($response->data);
$rc4 = $this->init_box($corp_secret);
return $this->do_crypt($rc4, $base64_text);
}
}
$obj=new php_decry();
print($obj->run());