我想将我的php-app中的crypt-functions从mcrypt更改为openssl。现在我在openssl中缺少像mcrypt_enc_get_key_size()这样的函数?我怎样才能读到最大值在openssl中密码化一个密码方法?
示例:河豚(CFB)
mcrypt_enc_get_key_size() returns 56 (Bytes) => 448bit
有什么想法吗?
答案 0 :(得分:0)
遗憾的是OpenSSL没有这样的功能。一种选择是检查每个支持的密码的密钥大小并使用交换机。如果你喜欢AES,你可以做这样的事情。
$method = "AES-256-CBC"; // Or whatever you want
if (preg_match("/^aes-?([0-9]+)/i", $method, $matches)) {
// AES has the key size in it's name as bits
$keySize = $matches[1] / 8;
} else {
$ivSize = openssl_cipher_iv_length($method);
if ($ivSize > 0) {
/*
* This will fit will with most.
* A few might get a larger key than required, but larger is better than smaller
* since larger keys just get's downsized rather than padded.
*
*/
$keySize = $ivSize * 2;
} else {
// Defaults to 128 when IV is not used
$keySize = 16;
}
}
例如。
BF使用64位块大小,在这种情况下将获得128位密钥大小。它需要32位,最多需要448位。
CAST5使用64位块大小,需要40位到128位密钥大小,在这种情况下它将获得128位。
它并不完美,但它会起作用。或者如上所述,您始终可以在http://php.net/manual/en/function.openssl-get-cipher-methods.php上检查支持的密码,并在交换机或类似设备中手动搜索并添加每个密钥的最大密钥大小。