PKCS7Encoder::decode PHP Method

decode() public method

对解密后的明文进行补位删除
public decode ( $text ) : 删除填充补位后的明文
return 删除填充补位后的明文
    function decode($text)
    {
        $pad = ord(substr($text, -1));
        if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
            $pad = 0;
        }
        return substr($text, 0, strlen($text) - $pad);
    }

Usage Example

Example #1
0
 public function decrypt($encrypted, $appid)
 {
     try {
         $ciphertext_dec = base64_decode($encrypted);
         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
         $iv = substr($this->key, 0, 16);
         mcrypt_generic_init($module, $this->key, $iv);
         $decrypted = mdecrypt_generic($module, $ciphertext_dec);
         mcrypt_generic_deinit($module);
         mcrypt_module_close($module);
     } catch (Exception $e) {
         return array(ErrorCode::$DecryptAESError, NULL);
     }
     try {
         $pkc_encoder = new PKCS7Encoder();
         $result = $pkc_encoder->decode($decrypted);
         if (strlen($result) < 16) {
             return '';
         }
         $content = substr($result, 16, strlen($result));
         $len_list = unpack('N', substr($content, 0, 4));
         $xml_len = $len_list[1];
         $xml_content = substr($content, 4, $xml_len);
         $from_appid = substr($content, $xml_len + 4);
     } catch (Exception $e) {
         print $e;
         return array(ErrorCode::$IllegalBuffer, NULL);
     }
     if ($from_appid != $appid) {
         return array(ErrorCode::$ValidateAppidError, NULL);
     }
     return array(0, $xml_content);
 }
All Usage Examples Of PKCS7Encoder::decode
PKCS7Encoder