Files
kiri-wchat/wchat/officialaccount/dcaler/PKCS7Encoder.php
T

47 lines
860 B
PHP
Raw Normal View History

2019-11-11 18:14:47 +08:00
<?php
2020-03-05 12:41:49 +08:00
namespace wchat\officialaccount\dcaler;
2019-11-11 18:14:47 +08:00
class PKCS7Encoder
{
public static $block_size = 32;
/**
* @param $text
* @return string
*/
function encode($text)
{
$block_size = PKCS7Encoder::$block_size;
$text_length = strlen($text);
//计算需要填充的位数
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
if ($amount_to_pad == 0) {
$amount_to_pad = PKCS7Encoder::$block_size;
}
//获得补位所用的字符
$pad_chr = chr($amount_to_pad);
$tmp = "";
for ($index = 0; $index < $amount_to_pad; $index++) {
$tmp .= $pad_chr;
}
return $text . $tmp;
}
/**
* @param $text
* @return false|string
*/
function decode($text)
{
$pad = ord(substr($text, -1));
if ($pad < 1 || $pad > 32) {
$pad = 0;
}
return substr($text, 0, (strlen($text) - $pad));
}
}