本文主要是介绍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))
这篇关于php AES cbc模式 pkcs7 128位加密解密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!