iBet7o

Clase para encriptacion con el metodo AES128

Jul 31st, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Clase para encriptacion con el metodo AES128.
  4.  *
  5.  * @author Roberto I Ramirez N <robertoiran [at] gmail.com>
  6.  */
  7. final class CipherAES
  8. {
  9.     private $_key = NULL;
  10.  
  11.     private $_iv = NULL;
  12.  
  13.     public function __construct($key = NULL, $iv = NULL, $bin = TRUE)
  14.     {
  15.         $this->setKey($key, $bin);
  16.         $this->setIV($iv, $bin);
  17.     }
  18.  
  19.     public function setKey($key = NULL, $bin = TRUE)
  20.     {
  21.         $this->_key = $bin ? hex2bin($key) : $key;
  22.     }
  23.  
  24.     public function setIV($iv = NULL, $bin = TRUE)
  25.     {
  26.         $this->_iv = $bin ? hex2bin($iv) : $iv;
  27.     }
  28.  
  29.     public function encrypt($string = NULL, $output_hex = TRUE)
  30.     {
  31.         $enc = NULL;
  32.  
  33.         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  34.         $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  35.         if(mcrypt_generic_init($td, $this->_key, $this->_iv) != -1)
  36.         {
  37.             $clearPadd = $this->_pkcs5_pad($string,$size);
  38.             $enc = mcrypt_generic($td, $clearPadd);
  39.             mcrypt_generic_deinit($td);
  40.         }
  41.         mcrypt_module_close($td);
  42.         return $output_hex ? bin2hex($enc) : $enc;
  43.     }
  44.  
  45.     public function decrypt($string = NULL, $input_hex = TRUE)
  46.     {
  47.         $dec = NULL;
  48.         $string = $input_hex ? hex2bin($string) : $string;
  49.  
  50.         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  51.         $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  52.         if(mcrypt_generic_init($td, $this->_key, $this->_iv) != -1)
  53.         {
  54.             $dec = mdecrypt_generic($td, $string);
  55.             $decWOPadding = $this->_pkcs5_unpad($dec);
  56.             mcrypt_generic_deinit($td);
  57.         }
  58.         mcrypt_module_close($td);
  59.         return $decWOPadding;
  60.     }
  61.  
  62.     private function _pkcs5_pad($text, $blocksize)
  63.     {
  64.         $pad = $blocksize - (strlen($text) % $blocksize);
  65.         return $text . str_repeat(chr($pad), $pad);
  66.     }
  67.  
  68.     private function _pkcs5_unpad($text)
  69.     {
  70.         $pad = ord($text{strlen($text)-1});
  71.         if ($pad > strlen($text)) return false;
  72.         if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
  73.         return substr($text, 0, -1 * $pad);
  74.     }
  75. }
  76.  
  77. if(!function_exists('hex2bin'))
  78. {
  79.     function hex2bin($str)
  80.     {
  81.         return pack('H*',$str);
  82.     }
  83. }
  84.  
  85. /**
  86.  * Ejemplo de uso
  87.  */
  88. $size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  89. $_iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
  90. $_key = '74e6ac8eec9f8e9d2401f318cbd361551e3f721a4f4f9aab313bdfb9f61af932';
  91.  
  92. $CipherAES = new CipherAES($_key, bin2hex($_iv));
  93. $string = 'Hola Mundo!';
  94. $strEnc = $CipherAES->encrypt($string);
  95. $strDec = $CipherAES->decrypt($strEnc);
  96.  
  97. echo 'Cadena original: ', $string, '<br />';
  98. echo 'Cadena encriptada: ', $strEnc, '<br />';
  99. echo 'Cadena desencriptada: ', $strDec;
Advertisement
Add Comment
Please, Sign In to add comment