返回值是以位元組為單位的長度。(不是位元或其他單位。)
(PHP 5 >= 5.3.3, PHP 7, PHP 8)
openssl_cipher_iv_length — 取得加密向量長度
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
<?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 的結果與此不同。