note 79121 deleted from function.crypt by ezyang

From: Date: Sat, 24 Nov 2007 01:06:42 +0000
Subject: note 79121 deleted from function.crypt by ezyang
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132520@lists.php.net to get a copy of this message
Note Submitter: gustav dot knutsson at mixterdata dot com ---- I actually created a XOR symmetric cryptclass which is quite easy to use the class: <?php final class crypt_engine_xor { //Use whatever key you would like... private $codeKey = "EVPOEC"; var $newStrArray; var $keyPos = 0; public function crypt_xor($str) { $this->newStrArray = $this->split_string_into_chars($str); $crypted = $this->crypted_xor(); $this->resetXOR(); return $crypted; } public function decrypt_xor($str) { $this->newStrArray = $this->split_string_into_chars($str); $crypted = $this->crypted_xor(); $this->resetXOR(); return $crypted; } private function split_string_into_chars($str) { return str_split($str); } private function crypted_xor() { $strLen = count($this->newStrArray); $string = ""; for($i=0;$i<$strLen;$i++){ $binRepres = $this->newStrArray[$i]; $string .= $this->match_key($binRepres); $this->calc_key_position(); } return $string; } private function match_key($represChar) { $A = $this->str2bin($represChar); $B = $this->str2bin($this->codeKey{$this->keyPos}); $byte = ""; for($i=0;$i<8;$i++){ if($A{$i} xor $B{$i} == 1){ $byte .= "1"; }else{ $byte .= "0"; } } $charRepres = $this->getCharFromBinary($byte); return $charRepres; } private function getCharFromBinary($byte) { $dec = bindec($byte); $char = chr($dec); return $char; } private function calc_key_position() { $this->keyPos += 1; if($this->keyPos == strlen($this->codeKey)){ $this->keyPos = 0; } } //I borrowed a php.net user function for this... private function str2bin($str) { for($a=0; $a < strlen($str); $a++) { $dec = ord(substr($str,$a,1)); $bin = ''; for($i=7; $i>=0; $i--) { if ( $dec >= pow(2, $i) ) { $bin .= "1"; $dec -= pow(2, $i); } else { $bin .= "0"; } } } return $bin; } private function resetXOR() { $this->newStrArray = ""; $this->keyPos = 0; } } ?> usage: <?php include_once('class.xor.php'); $cex = new crypt_engine_xor(); $crypted = $cex->crypt_xor("Hello there mate"); echo $crypted; echo '<br /><br />'; echo $cex->decrypt_xor($crypted); ?>

« previous php.notes (#132520) next »