2012-06-25 61 views
0

隨着PHP在equivelant,我試圖效仿的Perl的功能:Perl的encrypt_hex

#!/usr/bin/perl 
use DBI; 
use Crypt::ECB qw(encrypt decrypt encrypt_hex decrypt_hex); 
$code = encrypt_hex($key, "Blowfish", $secret); 

我的結果還沒有產生任何接近,如:

$code = crypt($key, '$2a$07$'."$secret$"); 

任何建議?

+0

@daxim,實際上我不確定。這是自從開發人員離開之後我繼承的代碼。儘管如此,仍然有效。我更新了示例的perl部分以顯示包含。 – Jahmic

回答

1

花了我一些時間弄清楚。 @ daxim使用mcrypt讓我失望了嘗試mcrypt庫的路徑。

$td = mcrypt_module_open('blowfish', '', 'ecb', ''); 
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
mcrypt_generic_init($td, $key, $iv); 
$encrypted_data = mcrypt_generic($td, $secret); 
mcrypt_generic_deinit($td); 
mcrypt_module_close($td); 

$unpacked = unpack("H*", $encrypted_data); 
$hexed = implode($unpacked); 
-2

我不知道有關PERL的功能,但也許the PHP hash-function你想要做什麼:

string hash (string $algo , string $data [, bool $raw_output = false ]) 
1

半答案:

Crypt::ECB::encrypt_hex('Some_key', 'Blowfish', '12345678') 

應該是相當於

unpack('H*', mcrypt_ecb('BLOWFISH', 'Some_key', '12345678', MCRYPT_ENCRYPT)) 

雖然我無法實現它的工作。

+0

+1。我仍然試圖安裝mcrypt。未來幾天會再看一遍。 – Jahmic