Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

93 lines (80 sloc) 2.22 KB

Blowfish (CBC)

Encrypt

$resource = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($resource, $key, $iv);
$encrypted = mcrypt_generic($resource, $content);
mcrypt_generic_deinit($resource);
mcrypt_module_close($resource);

$l = strlen($key);
if ($l < 16) {
    $key = str_repeat($key, ceil(16 / $l));
}
if ($m = strlen($content) % 8) {
    $content .= str_repeat("\x00", 8 - $m);
}
$encrypted = openssl_encrypt($content, 'bf-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
$encrypted = base64_encode($encrypted);

Decrypt

$resource = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($resource, $key, $iv);
$decrypted = mdecrypt_generic($resource, $content);
mcrypt_generic_deinit($resource);
mcrypt_module_close($resource);

$l = strlen($key);
if ($l < 16) {
    $key = str_repeat($key, ceil(16 / $l));
}
$content = base64_decode($content);
$decrypted = openssl_decrypt($content, 'bf-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
$decrypted = trim($decrypted);

PEAR::Crypt_Blowfish

Encrypt

$bf = new Crypt_Blowfish($key);
$encrypted = $bf->encrypt($content);

$l = strlen($key);
if ($l < 16) {
    $key = str_repeat($key, ceil(16 / $l));
}
if ($m = strlen($content) % 8) {
    $content .= str_repeat("\x00", 8 - $m);
}
$encrypted = openssl_encrypt($content, 'bf-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
$encrypted = base64_encode($encrypted);

Decrypt

$bf = new Crypt_Blowfish($key);
$decrypted = $bf->decrypt($content);

$l = strlen($key);
if ($l < 16) {
    $key = str_repeat($key, ceil(16 / $l));
}

$content = base64_decode($content);
$decrypted = openssl_decrypt($content, 'bf-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
$decrypted = trim($decrypted);
You can’t perform that action at this time.