1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| function encrypt($cleartext, $key){
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
$iv = '00000000';
// ajout du Padding.
$cleartext_length = strlen($cleartext)%8;
for($i=$cleartext_length; $i<8; $i++){
$cleartext .= chr(8-$cleartext_length);
}
$cipherText='';
// 128-bit blowfish encryption:
if (mcrypt_generic_init($cipher, $key, $iv) != -1){
// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
$cipherText = mcrypt_generic($cipher,$cleartext );
mcrypt_generic_deinit($cipher);
}
return $cipherText;
}
function decrypt($encrypted, $key){
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
$iv = '00000000';
// 128-bit blowfish encryption:
if (mcrypt_generic_init($cipher, $key, $iv) != -1){
// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
$cleartext = mdecrypt_generic($cipher,$encrypted );
mcrypt_generic_deinit($cipher);
}
// suppression du padding.
$pad = ord(substr($cleartext,strlen($cleartext)-1));
if($pad>0 & $pad<=8){
$cleartext = substr($cleartext, 0, strlen($cleartext) - $pad);
}
return $cleartext;
} |
Partager