Skip to content

Instantly share code, notes, and snippets.

@rming
Created November 3, 2016 08:12
Show Gist options
  • Save rming/91100dd3216ae067d2a27e701566db67 to your computer and use it in GitHub Desktop.
Save rming/91100dd3216ae067d2a27e701566db67 to your computer and use it in GitHub Desktop.
<?php
$key = '11345678901234561234567890123456';
$iv = '1234567890123456';
$method = "AES-256-CBC";
$options = OPENSSL_RAW_DATA;
$text = "Really";
$pkc_encoder = new PKCS7Encoder();
$text = $pkc_encoder->encode($text);
$res = openssl_encrypt($text, $method, $key, $options, $iv);
echo "pkcs7: " . bin2hex($text);
echo "\n";
echo "encrypted: " . bin2hex($res);
class PKCS7Encoder
{
public static $block_size = 32;
public function encode($text)
{
$block_size = self::$block_size;
$text_length = strlen($text);
//计算需要填充的位数
$amount_to_pad = self::$block_size - ($text_length % self::$block_size);
if ($amount_to_pad == 0) {
$amount_to_pad = self::block_size;
}
//获得补位所用的字符
$pad_chr = chr($amount_to_pad);
$tmp = '';
for ($index = 0; $index < $amount_to_pad; $index++) {
$tmp .= $pad_chr;
}
return $text . $tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment