[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/utils/ -> encryption.php (source)

   1  <?php
   2  /*********************************************************************************
   3   ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *
  10  ********************************************************************************/
  11  
  12  class Encryption 
  13  {
  14   	function encrypt($message)
  15       {
  16          //converting a string to binary
  17          $enc_message = $this->asc2bin($message);
  18          $enc_message = $this -> xor_string($enc_message);
  19          $enc_message = $this -> urlsafe_b64encode($enc_message);
  20          return $enc_message;
  21      }
  22   	function asc2bin($inputString, $byteLength=8)
  23      {
  24          $binaryOutput = '';
  25          $strSize = strlen($inputString);
  26          for($x=0; $x<$strSize; $x++)
  27          {
  28              $charBin = decbin(ord($inputString{$x}));
  29              $charBin = str_pad($charBin, $byteLength, '0', STR_PAD_LEFT);
  30              $binaryOutput .= $charBin;
  31          }
  32  
  33          return $binaryOutput;
  34      }
  35  
  36  	function bin2asc($binaryInput, $byteLength=8)
  37      {
  38          if (strlen($binaryInput) % $byteLength)
  39          {
  40              return false;
  41          }
  42          // why run strlen() so many times in a loop? Use of constants = speed increase.
  43          $strSize = strlen($binaryInput);
  44          $origStr = '';
  45          // jump between bytes.
  46          for($x=0; $x<$strSize; $x += $byteLength)
  47          {
  48              // extract character's binary code
  49              $charBinary = substr($binaryInput, $x, $byteLength);
  50              $origStr .= chr(bindec($charBinary)); // conversion to ASCII.
  51          }
  52          return $origStr;
  53      }
  54  
  55   	function decrypt($message)
  56       {
  57          $dec_message = $this -> urlsafe_b64decode($message);
  58          $dec_message = $this -> xor_string($dec_message);
  59          $dec_message = $this->bin2asc($dec_message);
  60          return $dec_message;
  61      }
  62  	 function xor_string($string)
  63       {
  64           $buf = '';
  65           $size = strlen($string);
  66           for ($i=0; $i<$size; $i++)
  67               $buf .= chr(ord($string[$i]) ^ 255);
  68           return $buf;
  69       }
  70  	function urlsafe_b64encode($string)
  71      {
  72          $data = base64_encode($string);
  73          $data = str_replace(array('+','/','='),array('-','_','.'),$data);
  74          return $data;
  75      }
  76  
  77  	function urlsafe_b64decode($string) 
  78      {
  79          $data = str_replace(array('-','_'),array('+','/'),$string);
  80          $mod4 = strlen($data) % 4;
  81          if ($mod4) 
  82          {
  83              $data .= substr('====', $mod4);
  84          }
  85          return base64_decode($data);
  86      }
  87  	function x_Encrypt($string, $key)
  88      {
  89          for($i=0; $i<strlen($string); $i++)
  90          {
  91              for($j=0; $j<strlen($key); $j++)
  92              {
  93                  $string[$i] = $string[$i]^$key[$j];
  94              }
  95          }
  96          return $string;
  97      }
  98  
  99  	function x_Decrypt($string, $key)
 100      {
 101          for($i=0; $i<strlen($string); $i++)
 102          {
 103              for($j=0; $j<strlen($key); $j++)
 104              {
 105                  $string[$i] = $key[$j]^$string[$i];
 106              }
 107          }
 108          return $string;
 109      }
 110  }
 111  


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1