PHP实现Java的decryptWithUnzipFromBase64

默认分类 2023-12-22

protected static $appSecret = 'xxxxxxx';
$str='xxxxxx';
$secret=base64_encode(self::pbkdf2(self::$appSecret,hash('md5',self::$appSecret,true),65536,16));
$res=self::decryptWithUnzipFromBase64($str,base64_decode($secret),hash('md5',self::$appSecret,true));
/**
 * pbkdf2解密
 * @param $password //待解密数据
 * @param $salt //盐
 * @param $iterations //迭代次数
 * @param $keyLength //数据长度
 * @return false|string
 */
public static function pbkdf2($password, $salt, $iterations, $keyLength)
{
    $blockSize  = 64;
    $hashLength = 20;
    $numBlocks  = ceil($keyLength / $hashLength);
    $output = '';
    for ($i = 1; $i <= $numBlocks; $i++) {
        $lastResult = $block = hash_hmac('sha1', $salt . pack('N', $i), $password, true);
        for ($j = 1; $j < $iterations; $j++) {
            $block      = hash_hmac('sha1', $block, $password, true);
            $lastResult ^= $block;
        }
        $output .= $lastResult;
    }
    return substr($output, 0, $keyLength);
}
/**
 * base64数据解压
 * @param $base64Data //待解压base64数据
 * @param $key //秘钥
 * @param $iv //向量
 * @return false|string
 */
public static function decryptWithUnzipFromBase64($base64Data, $key, $iv)
{
    // 解码 Base64 字符串中的数据
    $decodedData = base64_decode($base64Data);
    // 使用 AES-128-CBC 算法解密数据
    $decryptedData = openssl_decrypt($decodedData, 'AES-128-CBC', $key, 1, $iv);
    // 解压数据
    $uncompressedData = gzuncompress($decryptedData);
    // 返回解压后的数据
    return $uncompressedData;
}
知识共享署名声明
本文由 admin 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

楼主残忍的关闭了评论