【问题标题】:Getting this error - the IV parameter must be as long as the blocksize in出现此错误 - IV 参数必须与块大小一样长
【发布时间】:2014-06-23 21:52:18
【问题描述】:

在谷歌上搜索了很多加密/解密内容后,我设法构建了这段代码。但是,它显示以下错误,我无法弄清楚它为什么会发生。

错误:警告:mcrypt_encrypt():IV 参数必须与第 16 行 /var/www/encrypt.php 中的块大小一样长

error_reporting(E_ALL ^ E_DEPRECATED);
ini_set('display_errors', '1');

class Cipher {
    private $securekey, $iv;
    function __construct($textkey) {
        $this->securekey = hash('sha256',$textkey,TRUE);
        //$this->iv = mcrypt_create_iv(32);
        $size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB);
        $this->iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
    }

    function encrypt($input) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_CFB, $this->iv));
    }

    function decrypt($input) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_CFB, $this->iv));
    }
}

$cipher = new Cipher('secret passphrase');

$encryptedtext = $cipher->encrypt("hide me");
echo "->encrypt = $encryptedtext<br />";

$decryptedtext = $cipher->decrypt($encryptedtext);
echo "->decrypt = $decryptedtext<br />";

var_dump($cipher);

如果我在 CONSTRUCTOR 函数的 2 行之后发表评论..

$size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB);
$this->iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);

& 取消注释这一行..

$this->iv = mcrypt_create_iv(32);

它停止显示错误。

我无法确定我做错了什么。

【问题讨论】:

  • 旁注:如果您只是对密码进行哈希处理,securekey 是用词不当。 Google for PBKDF2。

标签: php encryption


【解决方案1】:

您将错误的算法名称传递给mcrypt_get_iv_size()。当它应该是MCRYPT_RIJNDAEL_256时,你正在传递MCRYPT_CAST_256

CAST-256 的块大小为 16 字节,而 MCRYPT_RIJNDAEL_256 指定 Rijndael 的块大小为 32 字节。这将导致错误。

【讨论】:

  • 感谢您的回复。你说的对。我使用了错误的算法。实际上,我正在尝试构建一个加密/解密库,该库将用于加密用户的个人数据数据,如 SSN、DOB 和一些财务信息。你能给我一些想法吗,这个 CLASS 是否适合处理这些信息,或者我应该使用其他东西。
猜你喜欢
  • 2014-07-18
  • 2016-06-22
  • 2023-03-26
  • 2015-05-12
  • 2015-11-06
  • 2011-12-02
  • 2020-04-28
  • 1970-01-01
相关资源
最近更新 更多