add clear
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user