PHP Conference Japan 2024

openssl_cipher_iv_length

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

openssl_cipher_iv_length取得加密向量長度

說明

openssl_cipher_iv_length(字串 $cipher_algo): 整數|false

取得加密初始化向量 (iv) 的長度。

參數

cipher_algo

加密方法,可用值列表請參考 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
新增註釋

使用者貢獻的註釋 2 則註釋

Tim Hunt
9 年前
返回值是以位元組為單位的長度。(不是位元或其他單位。)
Vee W.
6 年前
<?php
$ciphers
= openssl_get_cipher_methods();

// 應避免使用 ECB 模式
$ciphers = array_filter($ciphers, function ($n) {
return
stripos($n, "ecb") === FALSE;
});

// 至少早在 2016 年 8 月,Openssl 就已宣告以下演算法為弱加密演算法:基於 RC2、RC4、DES、3DES、MD5 的演算法
$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";
}
}
?>

會是...
AES-xxx-xxx 為 16
BF-xxx 為 8
CAMELLIA-xxx 為 16
CAST5-xxx 為 8
IDEA-xxx 為 8
SEED-xxx 為 16

小寫
aes-xxx-xxx 混雜 16 和 12。
id-aes-xxx 混雜 12 和 8。
以上數值是在 Windows 上使用 PHP 5.5 - 5.6 測試的。PHP 7.x 的結果與此不同。
To Top