代码之家  ›  专栏  ›  技术社区  ›  Señor Reginold Francis

PHP:mcrypt和openssl产生不同的输出

  •  0
  • Señor Reginold Francis  · 技术社区  · 7 年前

    希望有人能解释一下。

    我正在更新一些使用 mcrypt_generic 函数,使用DES-CBC密码

    当我更新此代码以使用 openssl_encrypt

    $this->_cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC,'');
    mcrypt_generic_init($this->_cipher, $this->_key, $this->_iv) 
    mcrypt_generic($this->_cipher, $text)
    

    方法输出前:

    27049189e7e08db6型

    之后

    openssl_encrypt($text, "DES-CBC", $this->_key,  OPENSSL_RAW_DATA , $this->_iv);
    

    方法输出后:

    27049189e7e08db6d504d16516e1c567型

    为什么会发生这种情况,以及可以采取什么措施(子字符串除外)来防止这种情况?

    0 回复  |  直到 7 年前
        1
  •  2
  •   treyBake user1850175    7 年前

    根据 this :

    1.3. 数据大小

    可能只是在填充输入。

    mcrypt_encrypt() 似乎在默认情况下填充0(基于中的代码示例) docs

    # creates a cipher text compatible with AES (Rijndael block size = 128)
    # to keep the text confidential 
    # only suitable for encoded input that never ends with value 00h
    # (because of default zero padding)
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
    $plaintext, MCRYPT_MODE_CBC, $iv);
    

    这就解释了长度的不同。

        2
  •  1
  •   Señor Reginold Francis    7 年前

    所以我设法找到了解决办法

    openssl_encrypt($text, "DES-CBC", $this->_key,  OPENSSL_NO_PADDING, $this->_iv);
    

    这种模式,

    OPENSSL无填充