PHP7中如何实现DES-EDE-CBC加密解密操作?

更新于
2026-09-27 18:56:46
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计566个文字,预计阅读时间需要3分钟。

PHP7中如何实现DES-EDE-CBC加密解密操作?

本章节为家庭介绍PHP7中使用DES-EDE-CBC加密解密的方法。具有一定的参考价值,如有需要的朋友可参考以下内容,希望对大家有所帮助。

1. 条件限制及与PHP5对比在PHP7.1及以上版本,通常不再使用mcrypt库。mcrypt库在PHP5中广泛使用,但在PHP7中已被弃用。PHP7引入了openssl库,作为新的加密扩展。

在PHP5上,使用mcrypt库进行DES-EDE-CBC加密解密的方法如下:

php// 加密$iv_length=mcrypt_enc_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC);$iv=mcrypt_create_iv($iv_length, MCRYPT_RAND);$encrypted=mcrypt_encrypt(MCRYPT_DES, MCRYPT_MODE_CBC, $data, MCRYPT_MODE_CBC, $iv);

// 解密$decrypted=mcrypt_decrypt(MCRYPT_DES, MCRYPT_MODE_CBC, $encrypted, MCRYPT_MODE_CBC, $iv);

在PHP7及以上版本,使用openssl库进行DES-EDE-CBC加密解密的方法如下:

php// 加密$iv_length=openssl_cipher_iv_length('des-ede-cbc');$iv=openssl_random_pseudo_bytes($iv_length);$encrypted=openssl_encrypt($data, 'des-ede-cbc', $key, OPENSSL_RAW_DATA, $iv);

// 解密$decrypted=openssl_decrypt($encrypted, 'des-ede-cbc', $key, OPENSSL_RAW_DATA, $iv);

以上方法在PHP7及以上版本均有效,且兼容性较好。在实际应用中,请根据具体需求选择合适的加密方法。

本篇文章给大家介绍一下PHP7中使用“DES-EDE-CBC”加解密的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

1. 条件约束

之前PHP5上常使用的mcrypt库在PHP7.1+上已经被移除,故我们采用openssl对数据进行加解密。

加密方式采用DES-EDE-CBC方式。

密钥填充方式为:采用24位密钥,先将key进行MD5校验取值,得出16位字串,再取key MD5校验值前8位追加到先前的取值后面。由此组装出24位的密钥。

2. 代码分享

<?php class DesEdeCbc { private $cipher, $key, $iv; /** * DesEdeCbc constructor. * @param $cipher * @param $key * @param $iv */ public function __construct($cipher, $key, $iv) { $this->cipher = $cipher; $this->key= $this->getFormatKey($key); $this->iv = $iv; } /** * @func 加密 * @param $msg * @return string */ public function encrypt($msg) { $des = @openssl_encrypt($msg, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv); return base64_encode($des); } /** * @func 解密 * @param $msg * @return string */ public function decrypt($msg) { return @openssl_decrypt(base64_decode($msg), $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv); } /** * @func 生成24位长度的key * @param $skey * @return bool|string */ private function getFormatKey($skey) { $md5Value= md5($skey); $md5ValueLen = strlen($md5Value); $key = $md5Value . substr($md5Value, 0, $md5ValueLen / 2); return hex2bin($key); } } $cipher = 'DES-EDE-CBC'; $msg = 'HelloWorld'; $key = '12345678'; $iv = "\x00\x00\x00\x00\x00\x00\x00\x00"; $des = new DesEdeCbc($cipher, $key, $iv); // 加密 $msg = $des->encrypt($msg); echo '加密后: ' . $msg . PHP_EOL; // 解密 $src = $des->decrypt($msg); echo '解密后: ' . $src . PHP_EOL;

3. 一点说明

可以根据实际情况调整加密方式、key的填充方式、及iv向量来满足不同的需求。

PHP7中如何实现DES-EDE-CBC加密解密操作?

本文共计566个文字,预计阅读时间需要3分钟。

PHP7中如何实现DES-EDE-CBC加密解密操作?

本章节为家庭介绍PHP7中使用DES-EDE-CBC加密解密的方法。具有一定的参考价值,如有需要的朋友可参考以下内容,希望对大家有所帮助。

1. 条件限制及与PHP5对比在PHP7.1及以上版本,通常不再使用mcrypt库。mcrypt库在PHP5中广泛使用,但在PHP7中已被弃用。PHP7引入了openssl库,作为新的加密扩展。

在PHP5上,使用mcrypt库进行DES-EDE-CBC加密解密的方法如下:

php// 加密$iv_length=mcrypt_enc_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC);$iv=mcrypt_create_iv($iv_length, MCRYPT_RAND);$encrypted=mcrypt_encrypt(MCRYPT_DES, MCRYPT_MODE_CBC, $data, MCRYPT_MODE_CBC, $iv);

// 解密$decrypted=mcrypt_decrypt(MCRYPT_DES, MCRYPT_MODE_CBC, $encrypted, MCRYPT_MODE_CBC, $iv);

在PHP7及以上版本,使用openssl库进行DES-EDE-CBC加密解密的方法如下:

php// 加密$iv_length=openssl_cipher_iv_length('des-ede-cbc');$iv=openssl_random_pseudo_bytes($iv_length);$encrypted=openssl_encrypt($data, 'des-ede-cbc', $key, OPENSSL_RAW_DATA, $iv);

// 解密$decrypted=openssl_decrypt($encrypted, 'des-ede-cbc', $key, OPENSSL_RAW_DATA, $iv);

以上方法在PHP7及以上版本均有效,且兼容性较好。在实际应用中,请根据具体需求选择合适的加密方法。

本篇文章给大家介绍一下PHP7中使用“DES-EDE-CBC”加解密的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

1. 条件约束

之前PHP5上常使用的mcrypt库在PHP7.1+上已经被移除,故我们采用openssl对数据进行加解密。

加密方式采用DES-EDE-CBC方式。

密钥填充方式为:采用24位密钥,先将key进行MD5校验取值,得出16位字串,再取key MD5校验值前8位追加到先前的取值后面。由此组装出24位的密钥。

2. 代码分享

<?php class DesEdeCbc { private $cipher, $key, $iv; /** * DesEdeCbc constructor. * @param $cipher * @param $key * @param $iv */ public function __construct($cipher, $key, $iv) { $this->cipher = $cipher; $this->key= $this->getFormatKey($key); $this->iv = $iv; } /** * @func 加密 * @param $msg * @return string */ public function encrypt($msg) { $des = @openssl_encrypt($msg, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv); return base64_encode($des); } /** * @func 解密 * @param $msg * @return string */ public function decrypt($msg) { return @openssl_decrypt(base64_decode($msg), $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv); } /** * @func 生成24位长度的key * @param $skey * @return bool|string */ private function getFormatKey($skey) { $md5Value= md5($skey); $md5ValueLen = strlen($md5Value); $key = $md5Value . substr($md5Value, 0, $md5ValueLen / 2); return hex2bin($key); } } $cipher = 'DES-EDE-CBC'; $msg = 'HelloWorld'; $key = '12345678'; $iv = "\x00\x00\x00\x00\x00\x00\x00\x00"; $des = new DesEdeCbc($cipher, $key, $iv); // 加密 $msg = $des->encrypt($msg); echo '加密后: ' . $msg . PHP_EOL; // 解密 $src = $des->decrypt($msg); echo '解密后: ' . $src . PHP_EOL;

3. 一点说明

可以根据实际情况调整加密方式、key的填充方式、及iv向量来满足不同的需求。

PHP7中如何实现DES-EDE-CBC加密解密操作?