当前位置: 首页 > 工具软件 > MCrypt > 使用案例 >

PHP mcrypt 加解密兼容openssl

丌官盛
2023-12-01

mcrypt_encrypt mcrypt_decrypt 已经比较古老,7.1 + PHP已经不支持。新的替换方式使用openssl_encrypt openssl_decrypt。 升级会涉及到新老加解密方式兼容问题。

*** 低版本PHP mcrypt ***

private function _pkcs5Pad($text, $blockSize) {
		$pad = $blockSize - (strlen($text) % $blockSize);
		return $text . str_repeat(chr($pad), $pad);
	}

	private function _pkcs5Unpad($text) {
		$end = substr($text, -1);
		$last = ord($end);
		$len = strlen($text) - $last;
		if(substr($text, $len) == str_repeat($end, $last)) {
			return substr($text, 0, $len);
		}
		return false;
	}

	//加密
	public function encrypt($encrypt, $key) {
		$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
		$paddedData = $this->_pkcs5Pad($encrypt, $blockSize);
		$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
		$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true),0,16);
		$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key2, $paddedData, MCRYPT_MODE_ECB, $iv);
		return base64_encode($encrypted);
	}
	//解密
	public function decrypt($decrypt, $key) {
		$decoded = base64_decode($decrypt);
		$blockSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($blockSize, MCRYPT_RAND);
		$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
		$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key2, $decoded, MCRYPT_MODE_ECB, $iv);
		return $this->_pkcs5Unpad($decrypted);
	}

*** 高版本PHP openssl ***


protected function newDecrypt($callbackData, $token = "")
    {
        $token = substr(openssl_digest(openssl_digest($token, 'sha1', true), 'sha1', true), 0, 16);
        $decrypted = openssl_decrypt($callbackData, 'AES-128-ECB', $token, OPENSSL_ZERO_PADDING);
        return $decrypted;
    }

    protected function newEncrypt($callbackData, $token = "")
    {
        $token = substr(openssl_digest(openssl_digest($token, 'sha1', true), 'sha1', true), 0, 16);

        $data = openssl_encrypt($callbackData, 'AES-128-ECB', $token, OPENSSL_RAW_DATA);

        return base64_encode($data);
    }

这两种方式相互兼容。

 类似资料: