如何将mcrypt_ecb()函数转换为mcrypt_generic()?

时间:2015-11-16 14:58:31

标签: php encryption mcrypt php-7

在PHP 5.5中,mcrypt_ecb()被折旧。我需要将我的Cryptogrpahy类转换为mcrypt_generic()mdecrypt_generic(),但文档中有关于如何使用TripleDES的稀疏细节。 任何有关转换encrypt()和decrypt()函数的帮助都会受到赞赏,因此我可以准备迁移到PHP 7。

class Crypt {
private $_key  = __CLASS__;
function __construct($key = null) {
    is_null($key)  || ($this->Key  = $key);
}

function __set($property, $value) {
    switch ($property) {
        case 'Key' : return $this->_setKey($value);
    }
}

function __get($property) {
    switch ($property) {
        case 'Key' : return $this->_key;
    }
}

public function encrypt($data) {
    $k = $this->_key;
    if (strlen($k) > 24)
        $k = substr($k, 0, 24);
    return base64_encode(base64_encode(mcrypt_ecb(MCRYPT_TripleDES, $k, $data, MCRYPT_ENCRYPT)));
}

public function decrypt($crypt) {
    $k = $this->_key;
    if (strlen($k) > 24)
        $k = substr($k, 0, 24);
    return trim(mcrypt_ecb(MCRYPT_TripleDES, $k, base64_decode(base64_decode($crypt)), MCRYPT_DECRYPT));
}

protected function _setKey($key) {
    $this->_key = (string) $key;
}
}

1 个答案:

答案 0 :(得分:2)

mcrypt_encrypt(MCRYPT_TripleDES, $k, $data, MCRYPT_MODE_modename)
mcrypt_decrypt(MCRYPT_TripleDES, $k, base64_decode(base64_decode($crypt)), MCRYPT_MODE_modename)

http://php.net/manual/en/mcrypt.constants.php