PHP MCrypt解密失败,除非使用AES-256


PHP MCrypt decryption fails unless using AES-256

我已经写了一个类来加密和解密字符串,但我不能使用任何密码,除了MCRYPT_RIJNDAEL_256。程序总是报告消息已损坏。我该怎么修理它?

我测试失败的密码是MCRYPT_RIJNDAEL_128 MCRYPT_RIJNDAEL_192 MCRYPT_BLOWFISH MCRYPT_SERPENTMCRYPT_TWOFISH

下面是我的代码:

class Crypt {
    private $masterKey;
    private $subKey;
    private $cipher = MCRYPT_RIJNDAEL_256 ;
    private $cipherMode = MCRYPT_MODE_CFB;
    //private $hashAlog = 'sha256';
    public function __construct($masterKey) {
        $this->masterKey = $masterKey;
    }
    public function setKey($masterKey) {
        $this->__construct($masterKey);
    }
    public function encrypt($message) {
        $iv = mcrypt_create_iv($this->getIVSize());
        $hmac = $this->signMsg($message);
        $this->genSubKey($iv);
        $cipherText = mcrypt_encrypt($this->cipher, $this->subKey, $message, $this->cipherMode, $iv);
        $cipherText = $iv . $hmac . $cipherText;
        return base64_encode($cipherText);
    }
    public function decrypt($enc_message) {
        $mixedMsg = base64_decode($enc_message);
        $iv = substr($mixedMsg, 0, $this->getIVSize());
        $this->genSubKey($iv);
        $hmac = substr($mixedMsg, $this->getIVSize(), strlen($this->signMsg(null)));
        $cipherText = substr($mixedMsg, $this->getIVSize() + strlen($this->signMsg(null)));
        $message = mcrypt_decrypt($this->cipher, $this->subKey, $cipherText, $this->cipherMode, $iv);
        if(!$message)
            die("Decrypt Error!");
        if($hmac != $this->signMsg($message))
            die("Message Corrupted");
        return $message;
    }
    private function genSubKey($iv) {
        $this->subKey = hash_pbkdf2("sha256", $this->masterKey, $iv, 50000, $this->getKeySize());
    }
    private function getKeySize() {
        return mcrypt_get_key_size($this->cipher, $this->cipherMode);
    }
    private function getIVSize() {
        return mcrypt_get_iv_size($this->cipher, $this->cipherMode);
    }
    private function signMsg($message) {
        return hash_hmac("sha512", $message, $this->masterKey, true);
    }
}

问题是由genSubKey函数中的hash_pbkdf2函数引起的。由于hash_pbkdf2将输出十六进制编码的字符串,因此它将是密钥大小的两倍。为了解决这个问题,我们需要将true作为附加参数传递给它,让它输出原始字节并适合密钥大小。

更正后的代码:

private function genSubKey($iv) {
    $this->subKey = hash_pbkdf2("sha256", $this->masterKey, $iv, 50000, $this->getKeySize(), true);
}