Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.
Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.
<?php // RAY_encrypt_decrypt.php
error_reporting(E_ALL);
// MAN PAGE: http://us.php.net/manual/en/ref.mcrypt.php
class Encryption
{
private $eot;
private $key;
private $ivs;
private $iv;
public function __construct()
{
// END OF TEXT DELIMITER
$this->eot = '___EOT';
// KEY - MUST BE KNOWN TO BOTH PARTS OF THE ALGORITHM
$this->key = 'quay';
// INITIALIZATION VECTOR - MUST BE KNOWN TO BOTH PARTS OF THE ALGORITHM
$this->ivs = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$this->iv = mcrypt_create_iv($this->ivs);
}
public function Encrypt($text)
{
// APPEND END OF TEXT DELIMITER
$text .= $this->eot;
// ENCRYPT THE DATA
$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv);
// MAKE IT base64() STRING SAFE FOR STORAGE AND TRANSMISSION
$data = base64_encode($data);
return $data;
}
public function Decrypt($text)
{
// DECODE THE DATA INTO THE BINARY ENCRYPTED STRING
$text = base64_decode($text);
// DECRYPT THE STRING
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv);
// REMOVE END OF TEXT DELIMITER
$data = explode($this->eot, $data);
return $data[0];
}
}
// INSTANTIATE THE CLASS
$crypt = new Encryption();
// INITIALIZE VARS FOR LATER USE IN THE HTML FORM
$encoded = '';
$decoded = '';
// IF ANYTHING WAS POSTED
if (!empty($_POST["clearstring"]))
{
$encoded = $crypt->Encrypt($_POST["clearstring"]);
echo "<br/>{$_POST["clearstring"]} YIELDS "; var_dump($encoded);
}
if (!empty($_POST["cryptstring"]))
{
$decoded = $crypt->Decrypt($_POST["cryptstring"]);
echo "<br/>{$_POST["cryptstring"]} YIELDS "; var_dump($decoded);
}
?>
<form method="post">
<input name="clearstring" value="<?=$decoded?>" />
<input type="submit" value="ENCRYPT" />
<br/>
<input name="cryptstring" value="<?=$encoded?>" />
<input type="submit" value="DECRYPT" />
</form>
<html><head><title>PHP Simple Crypt</title><style type="text/css">.rd{color:#b00;}</style></head><body BGCOLOR="#E3F7FF"><h2>Test for PHP Simple Crypt</h2>
<p>
<?php
define("VALIDBB", chr(2).chr(247).chr(15).chr(128).chr(255).chr(3));
// no ascii charaters helps for unique
//define("VALIDBB", '*k0~M-');// if you would rather use ascii text
// NONE of the encryption methods here are up to any decent level of modern encryption
// but can offer a mild preventive for hiding data
function aryXOR($In,$ary){$len = strlen($In);
// this uses an array of arrays with TWO values to XOR all of the text
for($a = 0; $a < count($ary); ++$a){for($i = 0; $i < $len; ++$i){
$xor = ord($In{$i}) ^ $ary[$a][$i & 1];
$In[$i] = chr($xor);}
}
return $In;}
function byteRotate($In,$aryIn,$en=true){$len = strlen($In);
// this rotates the text values an the mods 256 out the result
$aryKey=array(227,79,13,162,244,31,126,65,62,250,21,3,99,211,104,111,145,222,44,81,99,203,50,100,200);// 25 count
$aryKey = array_merge($aryIn, $aryKey);
$kLen = count($aryKey);
if ($len > $kLen) for($i = $kLen; $i < $len; ++$i) $aryKey[] = $i % 256;
for($i = 0; $i < $len; ++$i){
$xor = ord($In{$i});
if ($en) $In[$i] = chr(($xor + $aryKey[$i]) % 256); else
$In[$i] = chr((($xor + 256) - $aryKey[$i]) % 256);
}
return $In;}
function shift3($In,$en=true){$len = strlen($In);
// this right shifts all text valus 3 bit position, and left shift for decrypt
$On = array();
for($i = 0; $i < $len; ++$i) $On[] = ord($In{$i});
if ($en) {$In[0] = chr((($On[0] & 31) << 3) | (($On[$len-1] & 224) >> 5));
for($i = 1; $i < $len; ++$i) {$In[$i] = chr((($On[$i-1] & 224) >> 5) | (($On[$i] & 31) << 3));}} else
{$hd = ($On[1] & 7)<<5; for ($i = 1; $i < $len-1; ++$i) {$In[$i] = chr((($On[$i] & 248) >> 3) | (($On[$i+1] & 7) << 5));}
$In[$len-1] = chr((($On[0] & 7) << 5) | (($On[$len-1] & 248) >> 3)); $In[0] = chr($hd + (($On[0] & 248)>>3));}
return $In;}
function hex2text($In){$Out='';
//$In=str_replace(' ', '', $In);
for($i=0; $i<strlen($In); $i+=2) $Out.=chr(hexdec(substr($In, $i, 2)));
return($Out);}
// // // // // // // end of functions, beginning of encrypt
$First1 = 'User Name-Version';// This is the string to be encrypted
echo 'This is the Input, Before encryption- <b>'.$First1.'</b><br /><br />';
/*
IMPORTANT - in my view -
To ensure that any old falsely encrypted data will NOT be used
I will make the first 6 bytes (characters) a special "unique" byte
block that will identify all entry's as being valid or not.
I use the VALIDBB defined above.
Remember any byte block that is put in will be decrypted,
no matter if it is valid or NOT
*/
$Test1 = VALIDBB.$First1;// add validation to beginning of Input
$ary1 = array(array(35,121),array(114,123),array(74,129),array(94,240),array(221,7));// ALL numbers are between 1 and 255
$Test1 = aryXOR($Test1,$ary1);
$ary1 = array(7,222,147,200,123,4);// ALL numbers are between 1 and 255
$Test1 = byteRotate($Test1,$ary1);
$Test1 = shift3($Test1);
$ary1 = array(array(145,36),array(14,227),array(114,239),array(184,30),array(116,231),array(99,177));
$Test1 = aryXOR($Test1,$ary1);
$ary1 = array(111,17,88);// ALL numbers are between 1 and 255
$Test1 = byteRotate($Test1,$ary1);
$ary1 = strlen($Test1);
$Test1 = substr($Test1.$Test1[0],1,$ary1);
$Test1 = shift3($Test1);
// End of Encryption
$order = array('<', '>');
$replace = array('<', '>');
$ary1 = str_replace($order, $replace, $Test1);
echo 'Encrypted string = '.$ary1.'<br />';
//I like to use the the HEX for web data because I can scramble the hex
$Test1 = bin2hex($Test1);
$ary1 = strlen($Test1);
$Test1 = substr($Test1.$Test1[0],1,$ary1);
// by changing One character a hex read will use the wrong pairing of hex
echo 'Encrypted HEX string used for web = '.$Test1.'<br /><br />';
// Begin Decrypt
// for WEB you should do a regex to ensure all characters are HEX
$ary1 = strlen($Test1);
$Test1 = substr($Test1[$ary1-1].$Test1,0,$ary1);
$Test1 = hex2text($Test1);
$Test1 = shift3($Test1,false);
$ary1 = strlen($Test1);
$Test1 = substr($Test1[$ary1-1].$Test1,0,$ary1);
$ary1 = array(111,17,88);
$Test1 = byteRotate($Test1,$ary1,false);
// IMPORTANT you must reverse the array order for the aryXOR function decrypt
$ary1 = array(array(99,177),array(116,231),array(184,30),array(114,239),array(14,227),array(145,36));
$Test1 = aryXOR($Test1,$ary1);
$Test1 = shift3($Test1,false);
$ary1 = array(7,222,147,200,123,4);
$Test1 = byteRotate($Test1,$ary1,false);
$ary1 = array(array(221,7),array(94,240),array(74,129),array(114,123),array(35,121));// ALL numbers are between 1 and 255
$Test1 = aryXOR($Test1,$ary1);
//GET THE VALIDATION BLOCK and test it
$ary1 = substr($Test1,0,6);
if ($ary1 == VALIDBB) echo 'VALIDBB is VALID '; else echo 'error VALIDBB is NOT VALID ';
$ary1 = strlen($Test1);
$Test1 = substr($Test1,6,$ary1);
echo '+ Decrypted string = <b>'.$Test1.'</b><br />';
?>
<h3>End of Test for PHP Simple Crypt</h3>
</body></html>
unit Cryptoe;
interface
type
PReCrypt = ^TReCrypt;
TReCrypt = record
Good: Boolean;
Out1: String;
end;
function InCrypt(Plain: String): TReCrypt;
//function UnCrypt(HexIn: String): TReCrypt;
implementation
uses Windows, SysUtils;
const
Verfy: array[0..3] of Byte = (2,15,255,3);
function hexRota(InStr:String;en:Boolean=true):String;
var
i, len, len2: LongInt;
Chr1: Char;
Coy: String;
begin
len := Length(InStr);
len2 := len div 2;
if not en then
begin
Coy:= InStr;
Chr1 := InStr[1];
for i := 1 to Pred(len) do InStr[i] := Coy[i+1];
InStr[len] := Chr1;
end;
for i := 1 to len2 do
begin
if i and 1 = 0 then continue;
Chr1 := InStr[i];
InStr[i] := InStr[len - (i-1)];
InStr[len - (i-1)] := Chr1;
end;
if en then
begin
Coy:= InStr;
InStr[1] := Coy[len];
for i := 2 to len do InStr[i] := Coy[i-1];
end;
Result := UpperCase(InStr);
end;
procedure cRoto(pIn:PByte; len: LongInt; en:Boolean=true; odd:Boolean=false);
var
pRef, pPos: PByte;
i, skip, len2: LongInt;
hd, bit1, tib1, bit2, tib2: Byte;
begin
if len < 4 then Exit;
pRef := pIn;
len2 := (len-(len and 1)) div 2;
if len and 1 = 1 then
begin
pPos := PByte(LongInt(pRef)+len2);
bit1 := (pPos^ and 3) shl 2;
tib1 := (pPos^ and 12) shr 2;
pPos^ := (pPos^ and 240)+tib1+bit1;
end;
if not odd then Inc(pIn);
for i := 0 to Pred(len2) do
begin
if odd then skip := i and 1 else skip := 1-(i and 1);
if skip = 1 then continue;
pPos := PByte(LongInt(pRef)+(len-(i+1)));
bit1 := (pPos^ and 12) shr 2;
tib1 := (pIn^ and 12) shr 2;
bit2 := (pPos^ and 3) shl 2;
tib2 := (pIn^ and 3) shl 2;
if en then
begin
pPos^ := (pPos^ and 240)+tib1+bit2;
pIn^ := (pIn^ and 240)+bit1+tib2;
end else
begin
pPos^ := (pPos^ and 240)+tib2+bit1;
pIn^ := (pIn^ and 240)+bit2+tib1;
end;
hd := pIn^;
pIn^ := pPos^;
pPos^ := hd;
Inc(pIn,2);
end;
end;
procedure transBit(pIn:PByte; len: LongInt; en:Boolean=true);
var
pMem, pRef, pDup, pDup2, pDup3: PByte;
i: LongInt;
begin
pRef := pIn;
GetMem(pMem, len+8);
CopyMemory(pMem, pIn, len);
pDup := pMem;
pDup2 := pDup;
Inc(pDup2);
pDup3 := pDup2;
Inc(pDup3);
PByte(LongInt(pMem)+len)^ := pDup^;
Inc(pDup);
PByte(LongInt(pMem)+len+1)^ := pDup^;
Dec(pDup);
if en then
begin
for i := 0 to Pred(len) do
begin
pIn^ := (pDup^ and 64) or (pDup2^ and 137) or (pDup3^ and 54);
Inc(pIn);
Inc(pDup);
Inc(pDup2);
Inc(pDup3);
end;
end else
begin
for i := 0 to Pred(len) do
begin
pIn^ := (pDup3^ and 64) or (pDup2^ and 137) or (pDup^ and 54);
Inc(pIn);
Inc(pDup);
Inc(pDup2);
Inc(pDup3);
end;
CopyMemory(pMem, pRef, len);
CopyMemory(pRef, PByte(LongInt(pMem)+(len-2)), 2);
CopyMemory(PByte(LongInt(pRef)+2), pMem, len-2);
end;
FreeMem(pMem);
end;
procedure shift3(pIn:PByte; len: LongInt; en:Boolean=true);
var
pMem, pDup, pRef: PByte;
i: LongInt;
hd: Byte;
begin
GetMem(pMem, len+4);
CopyMemory(pMem, pIn, len);
pDup := pMem;
pRef := pIn;
hd := PByte(LongInt(pMem)+(len-1))^;
if en then
begin
pIn^ := ((pDup^ and 31) shl 3) or ((hd and 224) shr 5);
Inc(pIn);
for i := 1 to Pred(len) do // len-1
begin
pIn^ := ((pDup^ and 224) shr 5);
Inc(pDup);
pIn^ := pIn^ or ((pDup^ and 31) shl 3);
Inc(pIn);
end;
end else
begin
for i := 2 to len do
begin
pIn^ := ((pDup^ and 248) shr 3);
Inc(pDup);
pIn^ := pIn^ or ((pDup^ and 7) shl 5);
Inc(pIn);
end;
PByte(LongInt(pRef)+(len-1))^ := ((pMem^ and 7) shl 5) or ((hd and 248) shr 3);
hd := (pDup^ and 7) shl 5;
pMem^ := hd + ((pMem^ and 248) shr 3);
end;
FreeMem(pMem);
end;
procedure byteRotate(pIn:PByte; pAry: PByte; len: LongInt; count: LongInt; en:Boolean=true);
const
aryK: array[0..25] of Byte =(227,79,13,162,244,31,126,65,62,250,21,3,99,211,104,111,145,222,44,81,99,203,50,100,200,5);
var
pMem, pKey: PByte; //, pRef
i: LongInt;
hd, a: Byte;
begin
//pRef := pIn;
GetMem(pMem, len+count+28);
pKey := pMem;
CopyMemory(pMem, pAry, count);
Inc(pKey,count);
//a := 0;
//for i := count to len-1 do begin pKey^ := aryK[a]; Inc(a); Inc(pKey); end;
CopyMemory(pKey, @aryK, 26);
Inc(pKey,26);
if len > count+26 then for i := count+26 to len-1 do begin pKey^ := i; Inc(pKey); end;
pKey := pMem;
Dec(pKey);
Inc(pIn);
hd := pIn^;
if en then pIn^ := (hd+33) mod 256 else begin hd := ((hd+256)-33) mod 256; pIn^ := hd; end;
if hd = 0 then hd := 31;
Dec(pIn,2);
for i := 0 to len-1 do
begin
Inc(pIn);
Inc(pKey);
if i<>1 then begin
a := pIn^;
if en then pIn^ := (a + pKey^ +hd) mod 256 else //
pIn^ := ((a + 256) - (pKey^ +hd)) mod 256; //+hd aryKey[i]
end;
end;
FreeMem(pMem);
end;
Procedure aryXOR(pIn:PByte; pAry: PByte; len: LongInt; count: LongInt);
var
i,a,nu: LongInt;
pOn: PByte;
sBy: Byte;
begin
Dec(len);
Dec(count);
pOn := pIn;
for i := 0 to count do
begin
nu := 0;
for a := 0 to len do
begin
sBy := pOn^;
pOn^ := sBy xor pAry^;
Inc(pOn);
if (a and 1) = 1 then Dec(pAry) else Inc(pAry);
nu := a;
end;
pOn := pIn;
if nu and 1 = 1 then Inc(pAry,2) else Inc(pAry);
end;
end;
function InCrypt(Plain: String): TReCrypt;
const
Verfy: array[0..3] of Byte = (2,15,255,3);
var
len, i: LongInt;
pMem,pBary: PByte;
hx: String;
Rary: array[0..8] of Byte;
xAry: array[0..6] of Word;
aryRan: array[0..3] of Byte;
do1:Boolean;
RanW: Word;
begin
Result.Good := false;
Result.Out1 := 'ERROR - String Length is less than one';
len := Length(Plain);
if len < 1 then exit;
if len > 120 then begin Result.Out1 := 'ERROR - String Length is More than 120'; exit; end;
GetMem(pMem, len+16);
Randomize;
aryRan[0] := 1+Random(254);
aryRan[1] := 1+Random(254);
aryRan[2] := 255-aryRan[0];
aryRan[3] := 255-aryRan[1];
CopyMemory(pMem, @Verfy[0], 4);
pBary := pMem;
for i := 0 to 3 do
begin
pBary^ := (pBary^ +aryRan[i]) mod 256;//Ran1
Inc(pBary);
end;
CopyMemory(pBary, PChar(Plain), len);
Inc(len,4);
pBary := pMem;
RanW := (aryRan[0] shl 8) +aryRan[1];
xAry[0] := $FFFF;// 255,255 0000
xAry[1] := $AA55;// 85,170
xAry[2] := $0000F00F;// 15,440
xAry[3] := $0000C003; // 3,192
xAry[4] := RanW;
RanW := (aryRan[1] shl 8) +((aryRan[1]+aryRan[0]) mod 256);
xAry[5] := RanW;
aryXOR(pBary,PByte(@xAry[0]),len,6);
Rary[0] := (aryRan[0]-aryRan[1]) mod 256;
Rary[1] := 172;
Rary[2] := aryRan[1];
Rary[3] := aryRan[3];
Rary[4] := aryRan[2];
Rary[5] := aryRan[0];
Rary[6] := (aryRan[0]+aryRan[1]) mod 256;
byteRotate(pBary,PByte(@Rary),len,7,true);
Inc(pBary,len);
pBary^ := aryRan[0];
Inc(len);
pBary := pMem;
shift3(pBary, len);
transBit(pBary,len);
do1 := (len and 1) = 0;
cRoto(pBary, len,true,do1);
transBit(pBary,len);
shift3(pBary, len);
xAry[0] := $3021;//33,48
xAry[1] := $F00F;// 15,240
xAry[2] := $7CA3;//163,124
RanW := (aryRan[1] shl 8) +(aryRan[3]);
xAry[3] := RanW;
aryXOR(pBary,PByte(@xAry[0]),len,4);
Inc(pBary,len);
pBary^ := aryRan[1];
Inc(len);
pBary := pMem;
Rary[0] := 31;
Rary[1] := 195;
Rary[2] := 8;
Rary[3] := 14;
Rary[4] := 237;
Rary[5] := 209;
byteRotate(pBary,PByte(@Rary),len,6,true);
for i := 0 to len -1 do begin hx := hx+IntToHex(pBary^,2);Inc(pBary); end;
hx := hexRota(hx,true);
Result.Good := true;
Result.Out1 := hx;
end;
{function UnCrypt(HexIn: String): String;
var
len, i: LongInt;
pMem: PByte;
begin
len := Length(HexIn);
end;}
end.
<?php
function letRev($In){
$len = strlen($In);
$len2 = ($len-($len & 1)) / 2;
for($i = 0; $i < $len2; $i+=2){$On = $In[$i]; $In[$i]=$In[$len-($i+1)]; $In[$len-($i+1)]=$On;}
return $In;}
function byteRotate($On,$aryIn,$en=true){$len = count($On);
$aryKey=array(227,79,13,162,244,31,126,65,62,250,21,3,99,211,104,111,145,222,44,81,99,203,50,100,200,5);// 26 count
$aryKey = array_merge($aryIn, $aryKey);
$kLen = count($aryKey);
if ($len > $kLen) for($i = $kLen; $i < $len; ++$i) $aryKey[] = $i % 256;
$hd = $On[1];
if ($en) $On[1] = ($hd+33)% 256; else {$hd = (($hd+256)-33)% 256; $On[1] = $hd;}
if($hd ==0)$hd = 31;
for($i = 0; $i < $len; ++$i){if($i==1)continue;$A = $On{$i};
if ($en) $On[$i] = ($A + $aryKey[$i]+$hd) % 256; else
$On[$i] = (($A + 256) - ($aryKey[$i]+$hd)) % 256;}
return $On;}
function aryXOR($On,$ary){$len = count($On);
// this uses an array of arrays with TWO values to XOR all of the text
for($a = 0; $a < count($ary); ++$a){for($i = 0; $i < $len; ++$i) $On[$i] = $On{$i} ^ $ary[$a][$i & 1];}
return $On;}
function shift3($In,$en=true){$len = count($In);
$On = array();
for($i = 0; $i < $len; ++$i) $On[] = $In[$i];
if ($en) {$In[0] = (($On[0] & 31) << 3) | (($On[$len-1] & 224) >> 5);
for($i = 1; $i < $len; ++$i) {$In[$i] = (($On[$i-1] & 224) >> 5) | (($On[$i] & 31) << 3);}} else
{$hd = ($On[1] & 7)<<5; for ($i = 1; $i < $len-1; ++$i) {$In[$i] = (($On[$i] & 248) >> 3) | (($On[$i+1] & 7) << 5);}
$In[$len-1] = (($On[0] & 7) << 5) | (($On[$len-1] & 248) >> 3); $In[0] = $hd + (($On[0] & 248)>>3);}
return $In;}
function transBit($In,$en=true){$len = count($In);
$On = array();
for($i = 0; $i < $len; ++$i) $On[] = $In[$i];
$On[] = $In[0]; $On[] = $In[1];
//137- 1,128,8 54-32,16,4,2
if ($en) {for($i = 0; $i < $len; ++$i) {$In[$i] = ($On[$i] & 64) | ($On[$i+1] & 137) | ($On[$i+2] & 54);}} else
{for ($i = 0; $i < $len; ++$i) {$In[$i] = ($On[$i+2] & 64) | ($On[$i+1] & 137) | ($On[$i] & 54);}
$A = array_pop($In); $On = array_pop($In); $In= array_merge(array($On,$A), $In);}
return $In;}
function cRoto($In,$en=true,$odd=false){$len = count($In);//5
$len2 = ($len-($len & 1)) / 2;//2
if ($len > 3) {
//if (!$en) {if ($len & 1) {$On = $In[0]; $In[0] = $In[$len2]; $In[$len2] =$On;}
// {$On = $In[2]; $In[2] = $In[1]; $In[1] = $On;}}
if ($len & 1) {$bit1 = ($In[$len2] & 3) << 2;
$tib1 = ($In[$len2] & 12) >> 2;
$In[$len2] = ($In[$len2] & 240)+$tib1+$bit1;
//$On = $In[0]; $In[0] = $In[$len2]; $In[$len2] =$On;
}
for($i = 0; $i < $len2; ++$i) {if ($odd) $pos=$i & 1;else $pos=!($i & 1);
if ($pos) continue; $pos=$len-($i+1); $On = $In[$pos]; $On2 = $In[$i];
$bit1 = ($On & 12)>>2; $tib1 = ($On2 & 12)>>2; $bit2 = ($On & 3)<<2; $tib2 = ($On2 & 3)<<2;
if ($en){$On = ($On & 240)+$tib1+$bit2; $On2 = ($On2 & 240)+$bit1+$tib2;}else{
$On = ($On & 240)+$tib2+$bit1; $On2 = ($On2 & 240)+$bit2+$tib1;}
$In[$pos] = $On2; $In[$i] = $On;}
//if ($en) {$On = $In[1]; $In[1] = $In[2]; $In[2] = $On;
//if ($len & 1) {$On = $In[0]; $In[0] = $In[$len2]; $In[$len2] =$On;}}
}
return $In;}
function hex2text($In){$Out='';
//$In=str_replace(' ', '', $In);
for($i=0; $i<strlen($In); $i+=2) $Out.=chr(hexdec(substr($In, $i, 2)));
return($Out);}
// / / / / / / / / / end of crypt functions
function UnCrypt($HexIn) {$outAry = array(false,'ERROR - String Length is less than 14');
$len = strlen($HexIn);
if ($len < 14) return $outAry;
if (preg_match('/[^a-fA-F0-9]/', $HexIn) < 1) {$HexIn = substr($HexIn.$HexIn[0],1,$len);
$HexIn = letRev($HexIn);
$HexIn = hex2text($HexIn);
$len = strlen($HexIn);
$Bary = array(); for($i = 0; $i < $len; ++$i) $Bary[] = ord($HexIn{$i});
echo 'len= '.$len.'<br />';
$ary2 = array(31,195,8,14,237,209);// ALL numbers are between 1 and 255
$Bary = byteRotate($Bary,$ary2,false);
$aryRan = array(); $aryRan[1] = $Bary[$len-1];
$aryRan[3] = 255-$aryRan[1];
unset($Bary[$len-1]);
$len = count($Bary);
echo $len.' Ran2 is '.$aryRan[1].'<br />';
$ary1 = array(array($aryRan[3],$aryRan[1]),array(163,124),array(15,240),array(33,48));
$Bary = aryXOR($Bary,$ary1);
$Bary = shift3($Bary,false);
$Bary = transBit($Bary,false);
$Bary = cRoto($Bary,false,!($len & 1));//
$Bary = transBit($Bary,false);
$Bary = shift3($Bary,false);
$aryRan[0] = $Bary[$len-1];
unset($Bary[$len-1]);
$len = count($Bary);
$aryRan[2] = 255-$aryRan[0];
echo $len.' Ran1 is '.$aryRan[0].'<br />';
$ary2 = array(($aryRan[0]-$aryRan[1]) % 256,172,$aryRan[1],$aryRan[3],$aryRan[2],$aryRan[0],($aryRan[0]+$aryRan[1]) % 256);
$Bary = byteRotate($Bary,$ary2,false);
$ary1 = array(array(($aryRan[1]+$aryRan[0]) % 256,$aryRan[1]),array($aryRan[1],$aryRan[0]),array(3,192),array(15,240),array(85,170),array(255,255));
$Bary = aryXOR($Bary,$ary1);
$ary2 = array(2,15,255,3);
$ary1 = true;
for($i = 0; $i < 4; ++$i) {$Bary[$i] = (($Bary[$i]+256)- $aryRan[$i]) % 256; if ($ary2[$i] != $Bary[$i]) $ary1 = false;}
//else { $HexIn =
$HexIn = 'K';
$len = count($Bary);
for($i = 4; $i < $len; ++$i) $HexIn[$i-4] = chr($Bary[$i]);
//echo $HexIn.'<br />';
if (!$ary1) $outAry[1] = 'ERROR - Verification is WRONG'; else {$outAry[0] =true; $outAry[1] =$HexIn;}
//}
} else $outAry[1] = 'ERROR - Invalid Hex Charaters in string';
return $outAry;}
?>
// PHP Code for both the encryption and decryption functions
// both functions return and array with an error code and output string
// both functions need an Array with 8 bytes (1 to 255) for the KEY values $aryKey
// both functions need an Array with 3 bytes for the Verify code to make sure it's real $VerAry
function TeaEncrypt($InStr, $aryKey, $VerAry){$len = strlen($InStr);
if ($len < 1) return array(101,'ERROR Input String is empty');
if ($len > 123) return array(102,'ERROR - the Input String has more than 123 characters');
$orx = ord($InStr[0]); $VerAry[1] = ($VerAry[1]+$orx) %256;
$VerAry[0] = $VerAry[0]^$orx;//
$len2 = chr($VerAry[0]).$InStr[0].chr($VerAry[1]);
if ($len > 1) {$orx = ord($InStr[1]); $VerAry[2] = ($VerAry[2]+$orx) %256;//$VerAry[2] ^= $orx;
$len2 .= $InStr[1].chr($VerAry[2]);} else $len2 .= chr($VerAry[2] ^(255-$orx));
if ($len > 2) $InStr = $len2.substr($InStr,2,$len); else $InStr = $len2;
$len +=3;
if ($len & 1) {$x = ord($InStr[$len-1]);
$x = (($x ^ $aryKey[1])+ $orx)%256;
$x = (($x ^ $aryKey[5])+ $aryKey[2]) %256;
$x = $x ^ $aryKey[$orx & 7];
$InStr[$len-1] = chr($x);}
for ($i = 0; $i < $len-($len & 1); $i+=2){$x = ord($InStr[$i]);
$y = ord($InStr[$i+1]);
$sum = 0;
for ($g = 0; $g < 8; ++$g){
if ($g & 1) {$gs = 1 << $g;
$bx = $x & $gs;
$by = $y & $gs;
$x = ($x & (255 - $gs))+$by;
$y = ($y & (255 - $gs))+$bx;
} else{$bx = $x & 7;
$y = $y ^ $aryKey[$bx];
$bx = $y & 7;
$x = $x ^ $aryKey[$bx];}
$sum+=7;
$orx = ($y & 7) ^ ($sum & 7);
$x=($x+($aryKey[$g] ^ $y) + ($sum ^ ($y >> ($sum & 3)))+$aryKey[$orx])%256;
$sum+=7;
$orx = ($x&7) ^ ($sum & 7);
$y=($y+($aryKey[7-$g] ^$x) + ($sum ^ ($x >> ($sum & 3)))+$aryKey[$orx])%256;
}
$InStr[$i] = chr($x);
$InStr[$i+1] = chr($y);
}
$InStr = bin2hex($InStr);
$len *= 2;
--$len;
$sum = $InStr[$len];
$InStr = $sum.substr($InStr, 0, $len);
return array(0,$InStr);}
function TeaDecrypt($HexStr, $aryKey, $VerAry){$len = strlen($HexStr);
if ($len < 8) return array(101,'ERROR - the Input Hex String is less than 8 charaters');
if ($len & 1) return array(102,'ERROR - the Input Hex String has an Odd number of charaters');
if (preg_match('/[^a-fA-F0-9]/', $HexStr) > 0) return array(103,'ERROR - the Input Hex String has Non-Hex charaters');
$sum = $HexStr[0];
$HexStr = substr($HexStr, 1, $len).$sum;
$sum = '';
for($i=0; $i<$len; $i+=2) {$sum.=chr(hexdec(substr($HexStr, $i, 2)));}
$HexStr=$sum;
$len = strlen($HexStr);
for ($i = 0; $i < $len-($len & 1); $i+=2){$x = ord($HexStr[$i]);
$y = ord($HexStr[$i+1]);
$sum = 112;
for ($g = 7; $g > -1; --$g){$orx = ($x&7) ^ ($sum & 7);
$y=(($y+256)-((($aryKey[7-$g] ^ $x) +($sum ^ ($x >> ($sum & 3)))+$aryKey[$orx])%256))%256;
$sum-=7;
$orx = ($y & 7) ^ ($sum & 7);
$x=(($x+256)-((($aryKey[$g] ^ $y) +($sum ^ ($y >> ($sum & 3)))+$aryKey[$orx])%256))%256;
$sum-=7;
if ($g & 1) {$gs = 1 << $g;
$bx = $x & $gs;
$by = $y & $gs;
$x = ($x & (255 - $gs))+$by;
$y = ($y & (255 - $gs))+$bx;
} else{$bx = $y & 7;
$x = $x ^ $aryKey[$bx];
$bx = $x & 7;
$y = $y ^ $aryKey[$bx];}
}
$HexStr[$i] = chr($x);
$HexStr[$i+1] = chr($y);
}
$gs = 0;
$sum = '';
$sum .= $HexStr[1];
$x = ord($HexStr[1]);
$y = ord($HexStr[0]) ^ $x;
if ($VerAry[0] != $y) $gs = 1;
$y = ord($HexStr[2]);
$y = (($y +256) -$x)%256;
if ($VerAry[1] != $y) $gs = 1;
if ($len > 4) {$sum .= $HexStr[3];
$x = ord($HexStr[3]);
if ($len & 1) {$y = ord($HexStr[$len-1]);
$y = $y ^ $aryKey[$x & 7];
$y = ((($y + 256)- $aryKey[2])%256) ^ $aryKey[5];
$y = ((($y + 256)- $x)%256) ^ $aryKey[1];
$HexStr[$len-1] = chr($y);}
$y = ord($HexStr[4]);
$y = (($y +256) -$x)%256;
if ($VerAry[2] != $y) $gs = 1;
} else{$y = ord($HexStr[3]); $y = $y ^ (255-$x); if ($VerAry[2] != $y) $gs = 1;}
if ($len > 5) $sum .= substr($HexStr,5,$len);
if ($gs == 1) {return array(202,'ERROR Verify numbers did not match');} else
return array(0,$sum);}
type
TAryVerify = array[0..2] of Byte;
TAryKey = array[0..7] of Byte;
PTeaRe = ^TTeaRe;
TTeaRe = record
Error: LongInt;
Out1: String;
end;
function TeaEncrypt(InStr: String; aryKey: TAryKey; VerAry: TAryVerify): TTeaRe;
var
i,len,len2,g: LongInt;// ,ke
pIn,pMem,pTa: PByte;
x,y,sum,orx,gs,bx,by: Byte;
begin
Result.Error := 101;
Result.Out1 := 'ERROR - the Input String is Empty';
len := Length(InStr);
if len < 1 then Exit;
if len > 123 then
begin
Result.Error := 102;
Result.Out1 := 'ERROR - the Input String has more than 123 characters';
Exit;
end;
GetMem(pMem, len+16);
pIn := pMem;
pTa := PByte(PChar(InStr));
orx := pTa^;
pIn^ := VerAry[0] xor orx;
Inc(pIn);
pIn^ := pTa^;
if len > 1 then
begin
Inc(pIn);
pIn^ := VerAry[1]+ orx;
Inc(pIn);
Inc(pTa);
orx := pTa^;
pIn^ := pTa^;
Inc(pIn);
pIn^ := VerAry[2]+orx;
end else
begin
Inc(pIn);
pIn^ := VerAry[1]+ orx;
Inc(pIn);
pIn^ := VerAry[2] xor (255 - orx);
end;
if len > 2 then
begin
Inc(pIn);
Inc(pTa);
CopyMemory(pIn, pTa, len-2);
end;
Inc(len,3);
if Boolean(len and 1) then
begin
pIn := PByte(LongInt(pMem)+(len-1));
pIn^ := (pIn^ xor aryKey[1])+ orx;
pIn^ := (pIn^ xor aryKey[5])+ aryKey[2];
pIn^ := pIn^ xor aryKey[orx and 7];
Form1.Label4.Caption := IntToStr(orx);
end;
pIn := pMem;
len2 := ((len-(len and 1)) div 2)-1;
for i := 0 to len2 do
begin
x := pIn^;
Inc(pIn);
y := pIn^;
sum := 0;
for g := 0 to 7 do
begin
if Boolean(g and 1) then
begin
gs := 1 shl g;
bx := x and gs;
by := y and gs;
x := (x and (255 - gs))+by;
y := (y and (255 - gs))+bx;
end else
begin
bx := x and 7;
y := y xor aryKey[bx];
bx := y and 7;
x := x xor aryKey[bx];
end;
Inc(sum,7);
orx := (y and 7) xor (sum and 7);
Inc(x,(aryKey[g] xor y) + ((sum xor (y shr (sum and 3)))+aryKey[orx]));
Inc(sum,7);
orx := (x and 7) xor (sum and 7);
Inc(y,(aryKey[7-g] xor x) + ((sum xor (x shr (sum and 3)))+aryKey[orx]));
end;
pIn^ := y;
Dec(pIn);
pIn^ := x;
Inc(pIn,2);
end;
Result.Error := 0;
Result.Out1 := '';
pIn := pMem;
for i := 0 to Pred(len) do
begin
Result.Out1 := Result.Out1+IntToHex(pIn^,2);
Inc(pIn);
end;
len := len * 2;
InStr := Result.Out1[len];
Result.Out1 := Copy(Result.Out1, 1, Pred(len));
Result.Out1 := InStr+Result.Out1;
FreeMem(pMem);
end;
function TeaDecrypt(InStr: String; aryKey: TAryKey; VerAry: TAryVerify): TTeaRe;
var
i,len,len2,ke,g:LongInt;//
pIn,pMem,pLast: PByte;
x,y,sum,orx,gs,bx,by: Byte;
begin
Result.Error := 101;
Result.Out1 := 'ERROR - the Input Hex String is less than 8 charaters';
len2 := Length(InStr);
if len2 < 8 then Exit;
if Boolean(len2 and 1) then
begin
Result.Error := 102;
Result.Out1 := 'ERROR - the Input Hex String has an Odd number of charaters';
Exit;
end;
Result.Out1 := InStr[1];
InStr := Copy(InStr, 2, len2-1);
InStr := InStr+Result.Out1;
len := len2 div 2;
InStr := UpperCase(InStr);
pLast := PByte(PChar(InStr));
pIn := pLast;
ke := 0;
bx := 0; by := 0;
for i := 1 to len do
begin
if pLast^ IN [48..57] then bx:=(pLast^ -48) shl 4 else
if pLast^ IN [65..70] then bx := (pLast^ -55) shl 4 else
begin
ke := 1;
break;
end;
Inc(pLast);
if pLast^ IN [48..57] then by := pLast^ -48 else
if pLast^ IN [65..70] then by := pLast^ -55 else
begin
ke := 1;
break;
end;
pIn^ := bx + by;
Inc(pLast);
Inc(pIn);
end;
if ke > 0 then
begin
Result.Error := 103;
Result.Out1 := 'ERROR - the Input Hex String has Non-Hex charaters';
Exit;
end;
GetMem(pMem, len+16);
pIn := pMem;
CopyMemory(pIn, PChar(InStr), len);
len2 := ((len-(len and 1)) div 2)-1;
for i := 0 to len2 do
begin
x := pIn^;
Inc(pIn);
y := pIn^;
sum := 112;
for g := 7 downto 0 do
begin
orx := (x and 7) xor (sum and 7);
Dec(y,(aryKey[7-g] xor x) + ((sum xor (x shr (sum and 3)))+aryKey[orx]));
Dec(sum,7);
orx := (y and 7) xor (sum and 7);
Dec(x,(aryKey[g] xor y) + ((sum xor (y shr (sum and 3)))+aryKey[orx]));
Dec(sum,7);
if Boolean(g and 1) then
begin
gs := 1 shl g;
bx := x and gs;
by := y and gs;
x := (x and (255 - gs))+by;
y := (y and (255 - gs))+bx;
end else
begin
bx := y and 7;
x := x xor aryKey[bx];
bx := x and 7;
y := y xor aryKey[bx];
end;
end;
pIn^ := y;
Dec(pIn);
pIn^ := x;
Inc(pIn,2);
end;
x := 0;
pIn := pMem;
Inc(pIn);
orx := pIn^;
Result.Out1 := Chr(orx);
Dec(pIn);
pIn^ := pIn^ xor orx;
if pIn^ <> VerAry[0] then Inc(x);
Inc(pIn,2);
pIn^ := (pIn^+256) - orx;
if pIn^ <> VerAry[1] then Inc(x);
Inc(pIn);
if len > 4 then
begin
orx := pIn^;
Result.Out1 := Result.Out1+Chr(orx);
Inc(pIn);
if Boolean(len and 1) then
begin
pLast := PByte(LongInt(pMem)+(len-1));
pLast^ := pLast^ xor aryKey[orx and 7];
pLast^ := ((pLast^ + 256)- aryKey[2]) xor aryKey[5];
pLast^ := ((pLast^ + 256)- orx) xor aryKey[1];
end;
pIn^ := (pIn^+256)-orx;
if pIn^ <> VerAry[2] then Inc(x);
Form1.Label4.Caption := Form1.Label4.Caption+' '+IntToStr(pIn^)+' '+IntToStr(VerAry[2]);
end else
begin
pIn^ := pIn^ xor (255-orx);
if pIn^ <> VerAry[2] then Inc(x);
end;
if x <> 0 then
begin
Result.Error := 202;
Result.Out1 := 'ERROR Verify numbers did not match';
Form1.Label6.Caption := 'FAILED Verify';
FreeMem(pMem);
Exit;
end else Form1.Label6.Caption := 'Verify was Correct';
Result.Error := 0;
Inc(pIn);
if len > 5 then for i := 0 to len-6 do
begin
Result.Out1 := Result.Out1+Chr(pIn^);
Inc(pIn);
end;
FreeMem(pMem);
end;
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
shape, triangle, dbctrlgrid | 3 | 17 | |
PHP Curl to output a url | 7 | 48 | |
Using Modal's in to Retrieve Data from MySql and Populate Forms | 13 | 37 | |
Selenium: Transitioning from unit testing to automated testing | 4 | 24 |
Join the community of 500,000 technology professionals and ask your questions.