note 79121 added to function.crypt
| From: | gustav dot knutsson at mixterdata dot com | Date: | Mon, 12 Nov 2007 14:52:46 +0000 |
| Subject: | note 79121 added to function.crypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132204@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 212.63.193.10
Probable Submitter: 195.67.10.122
----
Manual Page -- http://www.php.net/manual/en/function.crypt.php
Edit -- https://master.php.net/note/edit/79121
Del: integrated -- https://master.php.net/note/delete/79121/integrated
Del: useless -- https://master.php.net/note/delete/79121/useless
Del: bad code -- https://master.php.net/note/delete/79121/bad+code
Del: spam -- https://master.php.net/note/delete/79121/spam
Del: non-english -- https://master.php.net/note/delete/79121/non-english
Del: in docs -- https://master.php.net/note/delete/79121/in+docs
Del: other reasons-- https://master.php.net/note/delete/79121
Reject -- https://master.php.net/note/reject/79121
Search -- https://master.php.net/manage/user-notes.php