PHP的openssl_encrypt的补充说明

在PHP(PHP 5 >= 5.3.0, PHP 7, PHP 8)中,openssl_encrypt的参数大致如下:

openssl_encrypt(
    string $data,
    string $cipher_algo,
    string $passphrase,
    int $options = 0,
    string $iv = "",
    string &$tag = null,
    string $aad = "",
    int $tag_length = 16
): string|false

其中$data待加密的数据字符串,这很好理解,这里不做过多阐释。

  • $cipher_algo为加密算法,如AES-128-ECB,中间这个数字表示密钥长度为128位。所有加密算法可通过openssl_get_cipher_methods获得。
  • $passphrase为密钥。既然上面的算法都规定了密钥长度,那这个密钥的字符串长度就已经确定下来了。但PHP随性的一点是如果密钥小于或超过算法指定的长度,也都能正常返回加密结果,而不像Java等语言直接抛出密钥长度不正确的异常。根据手册解释:

    若 passphrase 比预期长度短,将静默用 NUL 填充; 若比预期长度更长,将静默截断。

    那么NUL在PHP中表现又是什么?查了PHP源代码下的openssl实现,找到如下代码

    if (key_len > password_len) {
      if ((OPENSSL_DONT_ZERO_PAD_KEY & options) && !EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len)) {
          php_openssl_store_errors();
          php_error_docref(NULL, E_WARNING, "Key length cannot be set for the cipher algorithm");
          return FAILURE;
      }
      key = emalloc(key_len);
      memset(key, 0, key_len); 
      memcpy(key, *ppassword, password_len);
      *ppassword = (char *) key;
      *ppassword_len = key_len;
      *free_password = 1;
    }

    memset(key, 0, key_len); 应该对应的就是ASCII中的零,也就是PHP里的chr(0)。所以如果一个密钥长度不够,PHP会自动在密钥后面用chr(0)不足。例如:AES-128-ECB算法,密钥长度应为128位,也就是字符串长度为16。这时如果密钥为"1234567890abcde"和"1234567890abcde".chr(0)的意义是一样的,得到的加密结果也是一样的。至于超出长度自动截断,这应该很好理解了。

  • $options 是以下标记的按位或: OPENSSL_RAW_DATA 、 OPENSSL_ZERO_PADDING, 指定对于数据,如何填充的。
    其中OPENSSL_RAW_DATA的默认行为是PKCS7Padding

你可能感兴趣的:(phpopenssl)