note 96929 deleted from mcrypt.examples by danbrown
| From: | danbrown@php.net | Date: | Tue, 23 Mar 2010 18:50:48 +0000 |
| Subject: | note 96929 deleted from mcrypt.examples by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167325@lists.php.net to get a copy of this message | ||
Note Submitter: Diogo Besson
----
Jizz,
Gostei tanto da sua implementação que passei ela para dentro de um objeto.
just take a look:
:)
<?php
class Password {
private $key = "E4HD9h4DhS23DYfhHemkS3Nf";// 24 bit Key
private $iv = "fYfhHeDm";// 8 bit IV
private $bit_check=8;// bit amount for diff algor.
public function encrypt($text) {
$text_num =str_split($text,$this->bit_check);
$text_num = $this->bit_check-strlen($text_num[count($text_num)-1]);
for ($i=0;$i<$text_num; $i++) {$text = $text . chr($text_num);}
$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
mcrypt_generic_init($cipher, $this->key, $this->iv);
$decrypted = mcrypt_generic($cipher,$text);
mcrypt_generic_deinit($cipher);
return base64_encode($decrypted);
}
public function decrypt($encrypted_text){
$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
mcrypt_generic_init($cipher, $this->key, $this->iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted_text));
mcrypt_generic_deinit($cipher);
$last_char=substr($decrypted,-1);
for($i=0;$i<$this->bit_check-1; $i++){
if(chr($i)==$last_char){
$decrypted=substr($decrypted,0,strlen($decrypted)-$i);
break;
}
}
return $decrypted;
}
}
/***
* DADOS DE TESTE
*
* @var Password
*
* $password = new Password();
*
* $input = "Teste de senha";
*
* $str= $password->encrypt($input);
*
* echo "Iniciou como: $input <br /> Codificado: $str <br /> Traduzido:
".$password->decrypt($str);
*
*/
?>
other php notes: http://diogobesson.wordpress.com
thanks a lot, man!