1

在 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;
}
}
4

1 回答 1

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

于 2015-11-16T15:07:05.830 回答