YourPaste - For your paste! Archive - Tools - Login

Generic encryption class php5

Posted by unknown on Thu 5 Mar 2009 12:31 319 views - Syntax: PHP - Expires: never - Report - IMG - Download -

  1. <?php
  2.  
  3. /**
  4.  * Generic Encryption Class
  5.  *
  6.  * @version 1.0
  7.  * @author Martijn Bogaard
  8.  * @copyright 2009 Perfect Networks
  9.  **/
  10.  
  11.  
  12. class Encryption {
  13.         private static $_secretKey = 'Eg/ObjSwroO{fUIrkE1rIFit]RosbUCu1wIngiApT#iO~BedOpeO9agOLuPat#UobN>RifEioN3SlAxbUi#I'; //This is the secret key. Randomize it for every application you use this class. Use at least a 100 character key.
  14.        
  15.         private static $_encryptPrefix = '$:';
  16.         private static $_encryptSuffix = '$';
  17.        
  18.         private static $_saltLength = 3;
  19.        
  20.         public static function isEncrypted($str) {
  21.                 if (substr($str, 0, strlen(self::$_encryptPrefix)) == self::$_encryptPrefix && substr($str, 0, strlen(self::$_encryptSuffix)) == self::$_encryptSuffix) {
  22.                         $encryptedData = substr($str, strlen(self::$_encryptPrefix), 0 - strlen(self::$_encryptSuffix));
  23.                        
  24.                         if (substr_count($encryptedData, ':') == 2) {
  25.                                 list($data, $checksum, $salt) = explode(':', $encryptedData);
  26.                                
  27.                                 if (substr(sha1($data), 0, 8) == $checksum) {
  28.                                         return true;
  29.                                 }
  30.                         }
  31.                 }
  32.                
  33.                 return false;
  34.         }
  35.        
  36.         public static function encryptData($data, $publicKey) {
  37.                 $salt = self::generateSalt();
  38.                 $encryptionKey = self::getEncryptionKey($publicKey, $salt);
  39.                
  40.                 $gzippedData = gzcompress($data, 9); //gzip
  41.                 $encryptedData = self::crypt($gzippedData, $encryptionKey); //encrypt
  42.                 $hexData = bin2hex($encryptedData); //bin2hex
  43.                
  44.                 $checksum = substr(sha1($hexData), 0, 8); //checksum
  45.                
  46.                 return self::$_encryptPrefix . $hexData . ':' . $checksum . ':' . $salt . self::$_encryptSuffix;
  47.         }
  48.        
  49.         public static function decryptData($data, $publicKey) {
  50.                 if (self::isEncrypted($data)) {
  51.                         list($data, $checksum, $salt) = explode(':', substr($data, strlen(self::$_encryptPrefix), 0 - strlen(self::$_encryptSuffix)));
  52.                         $encryptionKey = self::getEncryptionKey($publicKey, $salt);
  53.                        
  54.                         if (substr(sha1($data), 0, 8) == $checksum) {
  55.                                 $encryptedData = pack('H*', $data); //hex2bin
  56.                                 $gzippedData = self::crypt($encryptedData, $encryptionKey); //decrypt
  57.                                 return gzuncompress($gzippedData);
  58.                         }
  59.                 }
  60.                
  61.                 return false;
  62.         }
  63.        
  64.         private static function getEncryptionKey($publicKey, $salt) {
  65.                 $newKey = self::crypt(self::$_secretKey, $publicKey); //first we encode the private key with the public key
  66.                 return self::crypt($newKey, $salt); //and then we encode the key with the salt
  67.         }
  68.        
  69.         private static function generateSalt() {
  70.                 $possible = range('a', 'z').range('A', 'Z');
  71.                
  72.                 for($i = 0, $salt = ''; $i < self::$_saltLength; $i++) {
  73.                         $salt .= $possible{rand(0,strlen($possible)-1)};
  74.                 }
  75.                
  76.                 return $salt;
  77.         }
  78.        
  79.         private static function crypt($data, $key) {
  80.                 $strPos = 0;
  81.                 $tokenPos = 0;
  82.  
  83.                 while ($strPos < strlen($data)) {
  84.                         $data{$strPos} = $data{$strPos} ^ $key{$tokenPos};
  85.  
  86.                         $strPos++;
  87.                         $tokenPos++;
  88.  
  89.                         if ($tokenPos >= strlen($key))
  90.                         $tokenPos = 0;
  91.                 }
  92.  
  93.                 return $data;
  94.         }
  95. }
  96.  
  97.                  Encryption::encryptData('test1ioewijoiojew', 'een key'),
  98.                  Encryption::decryptData(Encryption::encryptData('test1ioewijoiojew', 'een key'), 'een key'),
  99.                  Encryption::isEncrypted(Encryption::encryptData('test1ioewijoiojew', 'een key'))
  100. );

Comments


Name:
Comment:

© 2010 YourPaste.net - Disclaimer