php AES cbc模式 pkcs7 128位加密解密,微信小程序

PHP AES CBC模式PKCS7 128位加密

加密:

        $key = '1234567812345678';
        $iv = '1234567890123456'; 
        $message = '123456';
        $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $len = strlen($message); //取得字符串长度
        $pad = $blocksize - ($len % $blocksize); //取得补码的长度
        $message .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
        $xcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $message, MCRYPT_MODE_CBC, $iv);

PHP AES CBC模式PKCS7 128位解密

解密:

public function stripPkcs7Padding($string) {
        $slast = ord(substr($string, -1));
        $slastc = chr($slast);
        $pcheck = substr($string, -$slast);
 
        if (preg_match("/$slastc{" . $slast . "}/", $string)) {
            $string = substr($string, 0, strlen($string) - $slast);
            return $string;
        } else {
            return false;
        }
    }
 
stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $xcrypt, MCRYPT_MODE_CBC, $iv))

http://php.net/manual/zh/function.mcrypt-encrypt.php

转: https://blog.csdn.net/u011650048/article/details/50846124