溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP7中使用“DES-EDE-CBC”加解密的方法

發(fā)布時(shí)間:2021-05-12 09:40:35 來(lái)源:億速云 閱讀:394 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹PHP7中使用“DES-EDE-CBC”加解密的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

1. 條件約束

之前PHP5上常使用的mcrypt庫(kù)在PHP7.1+上已經(jīng)被移除,故我們采用openssl對(duì)數(shù)據(jù)進(jìn)行加解密。

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

密鑰填充方式為:采用24位密鑰,先將key進(jìn)行MD5校驗(yàn)取值,得出16位字串,再取key MD5校驗(yàn)值前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位長(zhǎng)度的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. 一點(diǎn)說(shuō)明

可以根據(jù)實(shí)際情況調(diào)整加密方式、key的填充方式、及iv向量來(lái)滿足不同的需求。

php的框架有哪些

php的框架:1、Laravel,Laravel是一款免費(fèi)并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個(gè)PHP框架。3、Symfony,Symfony是一款為Web項(xiàng)目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強(qiáng)大的PHP框架。

以上是“PHP7中使用“DES-EDE-CBC”加解密的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI