home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / mcrypt.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  2.6 KB  |  90 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  *
  5.  * @version $Id: mcrypt.lib.php 10142 2007-03-20 10:32:13Z cybot_tm $
  6.  */
  7.  
  8. /**
  9.  * Initialization
  10.  * Store the initialization vector because it will be needed for
  11.  * further decryption. I don't think necessary to have one iv
  12.  * per server so I don't put the server number in the cookie name.
  13.  */
  14. if (!isset($_COOKIE['pma_mcrypt_iv'])) {
  15.     srand((double) microtime() * 1000000);
  16.     $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND);
  17.     PMA_setCookie('pma_mcrypt_iv', base64_encode($iv));
  18. } else {
  19.     $iv = base64_decode($_COOKIE['pma_mcrypt_iv']);
  20. }
  21.  
  22. /**
  23.  * String padding
  24.  *
  25.  * @param   string  input string
  26.  * @param   integer length of the result
  27.  * @param   string  the filling string
  28.  * @param   integer padding mode
  29.  *
  30.  * @return  string  the padded string
  31.  *
  32.  * @access  public
  33.  */
  34. function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
  35.     $str = '';
  36.     $length = $pad_length - strlen($input);
  37.     if ($length > 0) { // str_repeat doesn't like negatives
  38.         if ($pad_type == STR_PAD_RIGHT) { // STR_PAD_RIGHT == 1
  39.             $str = $input.str_repeat($pad_string, $length);
  40.         } elseif ($pad_type == STR_PAD_BOTH) { // STR_PAD_BOTH == 2
  41.             $str = str_repeat($pad_string, floor($length/2));
  42.             $str .= $input;
  43.             $str .= str_repeat($pad_string, ceil($length/2));
  44.         } else { // defaults to STR_PAD_LEFT == 0
  45.             $str = str_repeat($pad_string, $length).$input;
  46.         }
  47.     } else { // if $length is negative or zero we don't need to do anything
  48.         $str = $input;
  49.     }
  50.     return $str;
  51. }
  52. /**
  53.  * Encryption using blowfish algorithm (mcrypt)
  54.  *
  55.  * @param   string  original data
  56.  * @param   string  the secret
  57.  *
  58.  * @return  string  the encrypted result
  59.  *
  60.  * @access  public
  61.  *
  62.  * @author  lem9
  63.  */
  64. function PMA_blowfish_encrypt($data, $secret) {
  65.     global $iv;
  66.     // Seems we don't need the padding. Anyway if we need it,
  67.     // we would have to replace 8 by the next 8-byte boundary.
  68.     //$data = full_str_pad($data, 8, "\0", STR_PAD_RIGHT);
  69.     return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $secret, $data, MCRYPT_MODE_CBC, $iv));
  70. }
  71.  
  72. /**
  73.  * Decryption using blowfish algorithm (mcrypt)
  74.  *
  75.  * @param   string  encrypted data
  76.  * @param   string  the secret
  77.  *
  78.  * @return  string  original data
  79.  *
  80.  * @access  public
  81.  *
  82.  * @author  lem9
  83.  */
  84. function PMA_blowfish_decrypt($encdata, $secret) {
  85.     global $iv;
  86.     return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv));
  87. }
  88.  
  89. ?>
  90.