2016-11-29 267 views
2

我想將我的php-app中的crypt函數從mcrypt更改爲openssl。現在我在openssl中缺少像mcrypt_enc_get_key_size()這樣的函數?我怎樣才能讀取最大值。 openssl中的密碼方法的密鑰大小?如何在php-openssl中獲取有關密鑰大小的信息

實施例:河豚(CFB)

mcrypt_enc_get_key_size() returns 56 (Bytes) => 448bit 

任何想法?

回答

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上支持的密碼,並手動搜索併爲交換機中的每個交換機或類似設備添加最大密鑰大小。