略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: openssl_cipher_iv_length

2024-11-14

openssl_cipher_iv_length

(PHP 5 >= 5.3.3, PHP 7, PHP 8)

openssl_cipher_iv_length获取密码iv长度

说明

openssl_cipher_iv_length(string $method): int

获取密码初始化向量(iv)长度。

参数

method

密码的方法,更多值查看 openssl_get_cipher_methods() 函数。

返回值

成功,返回密码长度, 失败返回 false .

错误/异常

当密码方法未知时,抛出一个E_WARNING 级的错误。

范例

示例 #1 openssl_cipher_iv_length() 范例

<?php
$method 
'AES-128-CBC';
$ivlen openssl_cipher_iv_length($method);

echo 
$ivlen;
?>

以上例程的输出类似于:

16
add a noteadd a note

User Contributed Notes 2 notes

up
10
Tim Hunt
7 years ago
The return value is a length in bytes. (Not bits, or anything else.)
up
-3
Vee W.
4 years ago
<?php
$ciphers
= openssl_get_cipher_methods();

//ECB mode should be avoided
$ciphers = array_filter($ciphers, function ($n) {
    return
stripos($n, "ecb") === FALSE;
});
   
// At least as early as Aug 2016, Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based
$ciphers = array_filter($ciphers, function ($c) {
    return
stripos($c, "des") === FALSE;
});
$ciphers = array_filter($ciphers, function ($c) {
    return
stripos($c, "rc2") === FALSE;
});
$ciphers = array_filter($ciphers, function ($c) {
    return
stripos($c, "rc4") === FALSE;
});
$ciphers = array_filter($ciphers, function ($c) {
    return
stripos($c, "md5") === FALSE;
});

if (
is_array($ciphers)) {
    foreach (
$ciphers as $cipher) {
        echo
$cipher.': ';
        echo
openssl_cipher_iv_length($cipher);
        echo
"<br>\n";
    }
}
?>

Will be...
AES-xxx-xxx is 16
BF-xxx is 8
CAMELLIA-xxx is 16
CAST5-xxx is 8
IDEA-xxx is 8
SEED-xxx is 16

lower case:
aes-xxx-xxx are mixed between 16 and 12.
id-aes-xxx are mixed between 12 and 8.
The values above are tested with PHP 5.5 - 5.6 on Windows. In PHP 7.x is different than this.

官方地址:https://www.php.net/manual/en/function.openssl-cipher-iv-length.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3