Location: PHPKode > scripts > SRCrypt > srcrypt/srcrypt.class.php
<?php
  //defined("_CONNECT") || die("Access denied!");
  /*
  CLASS: CRCrypt
  @Author      : Romeu
  @Author Email: hide@address.com
  @Date        : 14-04-2009
  @Version     : 1.0
  @Licence     : LGPL
  ***************************************************
  USAGE:
  
  require_once("srcrypt.class.php");
  $sr = new SRCrypt($key);
  $sr->encrypt($data);
  -OR-
  $sr->decrypt($data);
  ***************************************************
  The Key must be a random string up to how many characters wanted.
  Encryption strength depends on how long the key is.
  ***************************************************
  PURPOUSE:
  
  Encrypt data for server to server transactions.
  Eg. for send and receive XML from a remote server.
  Key has to be equal on both ends.
  
  */
  class SRCrypt{
  
      //Holds the results
        var $error = "";
        var $key = array();
        var $result = "";
        
        function SRCrypt($key){
            $this->key = $this->make_key($key);
        }
  //Converts the key into an array
  function make_key($cert){
     $key = array();
     for($l = 0; $l <= strlen($cert); $l++){
         $key[] = ord(substr($cert,$l,1));
     }
     return $key;
  }
//Encrypts the data  
    function encrypt($txt){
        $key = $this->key;
        $km = sizeOf($key) - 1;
        $kp = 0;
        $txt = "TESTE:" . $txt . ":TESTE";
        $encstr = "";
        for($l = 0; $l < strlen($txt);$l++){
           $cval = ord(substr($txt,$l,1));
           $cenc = $cval + $key[$kp];
           if($cenc > 255) $cenc = $cenc - 255;
           $encstr .= chr($cenc);
           $kp++;
           if($kp > $km) $kp = 0; 
        }
        $this->result = gzcompress($encstr,9);
    }  
//Decrypts the data
    function decrypt($txt){
        $txt = gzuncompress($txt);
        $key = $this->key;
        $km = sizeOf($key) - 1;
        $kp = 0;
        $encstr = "";
        for($l = 0; $l <= strlen($txt);$l++){
           $cval = ord(substr($txt,$l,1));
           $cenc = $cval - $key[$kp];
           if($cenc < 0) $cenc = $cenc + 255;
           $encstr .= chr($cenc);
           $kp++;
           if($kp > $km) $kp = 0; 
        }
        //echo $encstr;
        if(substr($encstr,0,6) != "TESTE:") $encstr = "KE";
        if(substr($encstr,strlen($encstr) - 7,6) != ":TESTE") $encstr = "KE";
        if($encstr != "KE"){
            $encstr = substr($encstr,6);    
            $encstr = substr($encstr,0,strlen($encstr) - 7);
            $this->result = $encstr;
        }else {
            $this->error = "INVALID KEY!";
        }
    }       
  }
?>
Return current item: SRCrypt