我想把php-app中的crypt-function从mcrypt改为openssl。现在我在openssl中缺少了像mcrypt_enc_get_key_size()这样的函数?如何读取最大值。openssl中cypher方法的密钥大小?
示例:河豚(CFB)
mcrypt_enc_get_key_size() returns 56 (Bytes) => 448bit有什么想法吗?
发布于 2016-12-01 02:43:17
遗憾的是,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上支持的密码,并手动搜索并添加交换机或类似设备中每个密码的最大密钥大小。
https://stackoverflow.com/questions/40862431
复制相似问题