sifangpay/app/ServicePay/Citpay/Aes56.php

117 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\ServicePay\Citpay;
class Aes56
{
public static function encrypt($str, $screct_key, $IVS = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
{
//AES, 128 模式加密数据 CBC
$IVS = implode(array_map("chr", $IVS));
$screct_key = static::getSercrtCode($screct_key);
$screct_key = base64_decode($screct_key);
//echo $screct_key . '---000</br/>';
$str = static:: addPKCS7Padding($str);
$iv = @ mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), 1);
$encrypt_str = @mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_CBC, $IVS);
return base64_encode($encrypt_str);
}
public static function getSercrtCode($key)
{
$key = static::sha256hashBase64($key);
// $ivSize = @mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
// $iv = @mcrypt_create_iv($ivSize, MCRYPT_RAND);
$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
return base64_encode($key2);
}
/**
* 填充算法
* @param string $source
* @return string
*/
public static function addPKCS7Padding($source)
{
$source = trim($source);
$block = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $block - (strlen($source) % $block);
if ($pad <= $block) {
$char = chr($pad);
$source .= str_repeat($char, $pad);
}
return $source;
}
/**
* 移去填充算法
* @param string $source
* @return string
*/
public static function stripPKSC7Padding($source)
{
$source = trim($source);
$char = substr($source, -1);
$num = ord($char);
if ($num == 62) return $source;
$source = substr($source, 0, -$num);
return $source;
}
public static function sha256hashBase64($data, $hashIterations = 1024)
{
$str = $data;
for ($x = 0; $x < $hashIterations; $x++) {
$tar = hash("sha256", $str);
$str = static::str2bin($tar);
}
return base64_encode($str);
}
public static function str2bin($hexdata)
{
$bindata = "";
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
public static function decrypt($str, $screct_key, $IVS = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
{
//AES, 128 模式加密数据 CBC
$str = base64_decode($str);
$IVS = implode(array_map("chr", $IVS));
$screct_key = static::getSercrtCode($screct_key);
$screct_key = base64_decode($screct_key);
$iv = @mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), 1);
// $encrypt_str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_CBC$IVS );
$encrypt_str = @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_CBC, $IVS);
$encrypt_str = trim($encrypt_str);
//$encrypt_str = static::stripPKSC7Padding($encrypt_str);
return $encrypt_str;
}
/**
*url 安全的base64编码 sunlonglong
*/
function base64url_encode($data)
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
/**
*url 安全的base64解码 sunlonglong
*/
function base64url_decode($data)
{
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
}