cahyadsn

TEA Algorithm Implementation on PHP

Jul 8th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.86 KB | None | 0 0
  1. <?php
  2. /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  3.  * Tiny Encryption Algorithm for PHP Version 1.1
  4.  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  5.  
  6. class TeaCrypt {
  7.   function tea_convert_u($x,$h,$l) {
  8.     if($x>=0){
  9.       $h=(int)($x/65536)&0xffff;
  10.       $l=(int)($x-$h*65536)&0xffff;
  11.     }
  12.     else{
  13.       while(($x=4294967296+$x)<0);// 2^32
  14.       $h=(int)($x/65536)&0xffff;
  15.       $l=(int)($x-$h*65536)&0xffff;
  16.     }
  17.   }
  18.  
  19.   function tea_xor($ax, $bx) {
  20.     if (PHP_OS=="WINNT" || PHP_INT_SIZE==8) return $ax^$bx;
  21.     /* for 32bit-unix */
  22.     $ah=0;
  23.     $al=0;
  24.     $bh=0;
  25.     $bl=0;
  26.     $bl=0;
  27.     //$ax=$ax&0xffffffff;
  28.     //$bx=$bx&0xffffffff;
  29.     //if(PHP_INT_SIZE==8)
  30.     //return $ax^$bx;
  31.     $this->tea_convert_u($ax,&$ah,&$al);
  32.     $this->tea_convert_u($bx,&$bh,&$bl);
  33.     return (($ah^$bh)*65536)|($al^$bl);
  34.   }
  35.  
  36.   function tea_add($ax, $bx) {
  37.     $eax=$ax+$bx;
  38.     if ($eax>4294967296) $eax-=4294967296;//2^32
  39.     return $eax;
  40.   }
  41.  
  42.   function tea_dec($ax, $bx) {
  43.     $eax=$ax-$bx;
  44.     if ($eax<0) $eax+=4294967296;//2^32
  45.     return $eax;
  46.   }
  47.  
  48.   function tea_left($ax, $bit) {
  49.     $eax=$ax<<$bit;
  50.     if ($eax>4294967296) $eax=$eax%4294967296;//2^32
  51.     return $eax;
  52.   }
  53.  
  54.   function tea_right($ax, $bit) {
  55.     $eax=$ax>>$bit;
  56.     if ($eax<0) $eax=$eax%4294967296;//2^32
  57.     return $eax;
  58.   }
  59.  
  60.   function GetRandomKey() {
  61.     mt_srand((double)microtime() * 1000000);
  62.     $zMD5=md5(mt_rand(0, 99999999));
  63.     $string="";
  64.     for ($i=0;$i<16;$i++) {
  65.       $string.=chr(HexDec(substr($zMD5,$i*2,2)));
  66.     }
  67.     return $string;
  68.   }
  69.  
  70.   function Hex2Dec($HEX) {
  71.     $HEX_ARRAY=array();
  72.     $HEX_ARRAY[0]=DecHex(ord($HEX[0]));
  73.     $HEX_ARRAY[1]=DecHex(ord($HEX[1]));
  74.     $HEX_ARRAY[2]=DecHex(ord($HEX[2]));
  75.     $HEX_ARRAY[3]=DecHex(ord($HEX[3]));
  76.  
  77.     if (strlen($HEX_ARRAY[0])<2) $HEX_ARRAY[0]="0".$HEX_ARRAY[0];
  78.     if (strlen($HEX_ARRAY[1])<2) $HEX_ARRAY[1]="0".$HEX_ARRAY[1];
  79.     if (strlen($HEX_ARRAY[2])<2) $HEX_ARRAY[2]="0".$HEX_ARRAY[2];
  80.     if (strlen($HEX_ARRAY[3])<2) $HEX_ARRAY[3]="0".$HEX_ARRAY[3];
  81.  
  82.     $DEC=HexDec($HEX_ARRAY[0].$HEX_ARRAY[1].$HEX_ARRAY[2].$HEX_ARRAY[3]);
  83.  
  84.     return $DEC;
  85.   }
  86.  
  87.   function Dec2Hex($DEC) {
  88.     $HEX=DecHex($DEC);
  89.     $len=strlen($HEX);
  90.     if ($len<8) {
  91.       $bad=8-$len;
  92.       for ($i=0;$i<$bad;$i++) {
  93.         $HEX="0".$HEX;
  94.       }
  95.     }
  96.  
  97.     $HEX_ARRAY=array();
  98.     $HEX_ARRAY[0]=$HEX[0].$HEX[1];
  99.     $HEX_ARRAY[1]=$HEX[2].$HEX[3];
  100.     $HEX_ARRAY[2]=$HEX[4].$HEX[5];
  101.     $HEX_ARRAY[3]=$HEX[6].$HEX[7];
  102.  
  103.     return chr(HexDec($HEX_ARRAY[0])).chr(HexDec($HEX_ARRAY[1])).chr(HexDec($HEX_ARRAY[2])).chr(HexDec($HEX_ARRAY[3]));
  104.   }
  105.  
  106.   function cs_rand() {
  107.     return 0xdead;
  108.     return rand(0, 0xffff);
  109.   }
  110.  
  111.   function teaEncipher($v,$k) {
  112.     $va=$v[0].$v[1].$v[2].$v[3];
  113.     $vb=$v[4].$v[5].$v[6].$v[7];
  114.     $ka=$k[0].$k[1].$k[2].$k[3];
  115.     $kb=$k[4].$k[5].$k[6].$k[7];
  116.     $kc=$k[8].$k[9].$k[10].$k[11];
  117.     $kd=$k[12].$k[13].$k[14].$k[15];
  118.  
  119.     $y_a=0;
  120.     $y_b=0;
  121.     $y_c=0;
  122.     $z_a=0;
  123.     $z_b=0;
  124.     $z_c=0;
  125.     $tmp_y=0;
  126.     $tmp_z=0;
  127.  
  128.     //unsigned int
  129.     $y     = $this->Hex2Dec($va);
  130.     $z     = $this->Hex2Dec($vb);
  131.     $a     = $this->Hex2Dec($ka);
  132.     $b     = $this->Hex2Dec($kb);
  133.     $c     = $this->Hex2Dec($kc);
  134.     $d     = $this->Hex2Dec($kd);
  135.     $n     = 0x10;        /* do encrypt 16 (0x10) times */
  136.     $sum   = 0;
  137.     $delta = 0x9E3779B9; /*  0x9E3779B9 - 0x100000000 = -0x61C88647 */
  138.  
  139.     //unsigned int max value is 4294967295
  140.     while ($n-- > 0) {
  141.       if (PHP_OS=="WINNT" || PHP_INT_SIZE==4) {
  142.         $sum = $sum + $delta;
  143.         $tmp_y = $this->tea_xor($this->tea_xor((($z << 4) + $a) , ($z + $sum)) , (($z/32) + $b));
  144.         $y = BinDec(DecBin($y + $tmp_y));
  145.         $tmp_z = $this->tea_xor($this->tea_xor((($y << 4) + $c) , ($y + $sum)) , (($y/32) + $d));
  146.         $z = BinDec(DecBin($z + $tmp_z));
  147.       }
  148.       else {
  149.         $sum = $this->tea_add($sum, $delta);
  150.  
  151.         $ya = $this->tea_add($this->tea_left($z, 4) , $a);
  152.         $yb = $this->tea_add($z , $sum);
  153.         $yc = $this->tea_add($this->tea_right($z , 5) , $b);
  154.  
  155.         $tmp_y = $this->tea_xor($this->tea_xor($ya , $yb) , $yc);
  156.         $y = $this->tea_add($y , $tmp_y);
  157.  
  158.         $za = $this->tea_add($this->tea_left($y, 4) , $c);
  159.         $zb = $this->tea_add($y , $sum);
  160.         $zc = $this->tea_add($this->tea_right($y , 5) , $d);
  161.  
  162.         $tmp_z = $this->tea_xor($this->tea_xor($za , $zb) , $zc);
  163.         $z = $this->tea_add($z , $tmp_z);
  164.       }
  165.     }
  166.     $w=$this->Dec2Hex($y).$this->Dec2Hex($z);
  167.  
  168.     return $w;
  169.   }
  170.  
  171.   function teaDecipher($v,$k) {
  172.     $va=$v[0].$v[1].$v[2].$v[3];
  173.     $vb=$v[4].$v[5].$v[6].$v[7];
  174.  
  175.     $ka=$k[0].$k[1].$k[2].$k[3];
  176.     $kb=$k[4].$k[5].$k[6].$k[7];
  177.     $kc=$k[8].$k[9].$k[10].$k[11];
  178.     $kd=$k[12].$k[13].$k[14].$k[15];
  179.  
  180.     $y_a=0;
  181.     $y_b=0;
  182.     $y_c=0;
  183.     $z_a=0;
  184.     $z_b=0;
  185.     $z_c=0;
  186.     $tmp_y=0;
  187.     $tmp_z=0;
  188.  
  189.     //unsigned int
  190.     $y     = $this->Hex2Dec($va);
  191.     $z     = $this->Hex2Dec($vb);
  192.     $a     = $this->Hex2Dec($ka);
  193.     $b     = $this->Hex2Dec($kb);
  194.     $c     = $this->Hex2Dec($kc);
  195.     $d     = $this->Hex2Dec($kd);
  196.     $n     = 0x10;
  197.     $sum   = 0xE3779B90;
  198.     $delta = 0x9E3779B9; /* why this ? must be related with n value*/
  199.  
  200.     /* sum = delta<<5, in general sum = delta * n */
  201.     while ($n-- > 0) {
  202.       if (PHP_OS=="WINNT" || PHP_INT_SIZE==4) {
  203.         $tmp_z = $this->tea_xor($this->tea_xor((($y << 4) + $c) , ($y + $sum)) , (($y/32) + $d));
  204.         $z = BinDec(DecBin($z - $tmp_z));
  205.         $tmp_y = $this->tea_xor($this->tea_xor((($z << 4) + $a) , ($z + $sum)) , (($z/32) + $b));
  206.         $y = BinDec(DecBin($y - $tmp_y));
  207.         $sum = $sum - $delta;
  208.       }
  209.       else {
  210.         $za = $this->tea_add($this->tea_left($y , 4) , $c);
  211.         $zb = $this->tea_add($y , $sum);
  212.         $zc = $this->tea_add($this->tea_right($y , 5) , $d);
  213.  
  214.         $tmp_z = $this->tea_xor($this->tea_xor($za , $zb) , $zc);
  215.         $z = $this->tea_dec($z , $tmp_z);
  216.  
  217.         $ya = $this->tea_add($this->tea_left($z , 4) , $a);
  218.         $yb = $this->tea_add($z , $sum);
  219.         $yc = $this->tea_add($this->tea_right($z , 5) , $b);
  220.  
  221.         $tmp_y = $this->tea_xor($this->tea_xor($ya , $yb) , $yc);
  222.         $y = $this->tea_dec($y , $tmp_y);
  223.  
  224.         $sum = $this->tea_dec($sum , $delta);
  225.       }
  226.     }
  227.     $w=$this->Dec2Hex($y).$this->Dec2Hex($z);
  228.  
  229.     return $w;
  230.   }
  231.  
  232.   function encrypt($instr,$key) {
  233.     $xx=0;//while counter
  234.     $outstr=array();
  235.  
  236.     $instrlen=strlen($instr);
  237.  
  238.     $plain=array(8);         /* plain text buffer*/
  239.     $plain_pre_8=array(8);   /* plain text buffer, previous 8 bytes*/
  240.     $zcrypted=array(8);      /* crypted text*/
  241.     $crypted_pre_8=array(8); /* crypted test, previous 8 bytes*/
  242.  
  243.     $pos_in_byte = 1;  /* loop in the byte */
  244.     $is_header=1;      /* header is one byte*/
  245.     $count=0;          /* number of bytes being crypted*/
  246.     $padding = 0;      /* number of padding stuff*/
  247.  
  248.  
  249.     $pos_in_byte = ($instrlen + 0x0a) % 8; /* header padding decided by instrlen*/
  250.  
  251.     if ($pos_in_byte) {
  252.       $pos_in_byte = 8 - $pos_in_byte;
  253.     }
  254.     $plain[0] = chr(($this->cs_rand() & 0xf8) | $pos_in_byte);
  255.  
  256.     //memset(plain+1, $this->cs_rand()&0xff, pos_in_byte++);
  257.     //memset(plain_pre_8, 0x00, sizeof(plain_pre_8));
  258.     $xval=chr($this->cs_rand() & 0xff);
  259.     for ($xx=0;$xx<$pos_in_byte;$xx++) {
  260.       $plain[$xx+1]=$xval;
  261.     }
  262.     $pos_in_byte++;
  263.  
  264.     for ($xx=0;$xx<8;$xx++) {
  265.       $plain_pre_8[$xx]="\x00";
  266.     }
  267.  
  268.     $padding = 1; /* pad some stuff in header*/
  269.     while ($padding <= 2) { /* at most two byte */
  270.       if($pos_in_byte < 8) {
  271.         $plain[$pos_in_byte++] = chr($this->cs_rand() & 0xff);
  272.         $padding++;
  273.       }
  274.       if($pos_in_byte == 8) {
  275.         //encrypt_every_8_byte();
  276.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  277.         /*** we encrypt every eight byte ***/
  278.         for ($pos_in_byte=0; $pos_in_byte<8; $pos_in_byte++) {
  279.           if($is_header) {
  280.             $plain[$pos_in_byte] = chr(ord($plain[$pos_in_byte]) ^ ord($plain_pre_8[$pos_in_byte]));
  281.  
  282.           }
  283.           else {
  284.             $plain[$pos_in_byte] = chr(ord($plain[$pos_in_byte]) ^ ord($crypted_pre_8[$pos_in_byte]));
  285.           }
  286.         }/* prepare plain text*/
  287.  
  288.         for ($xx=0;$xx<8;$xx++) {
  289.           $zcrypted[$xx]="\x00";
  290.         }
  291.         //(unsigned int *)plain
  292.         //(unsigned int *)key
  293.         //(unsigned int *)zcrypted
  294.         $zcrypted=$this->teaEncipher($plain,$key);   /* encrypt it*/
  295.  
  296.         $tmp_zcrypted=array();
  297.         for ($xx=0;$xx<8;$xx++) {
  298.           $tmp_zcrypted[$xx]=$zcrypted[$xx];
  299.         }
  300.         for($pos_in_byte=0; $pos_in_byte<8; $pos_in_byte++) {
  301.           $tmp_zcrypted[$pos_in_byte] = chr(ord($tmp_zcrypted[$pos_in_byte]) ^ ord($plain_pre_8[$pos_in_byte]));
  302.         }
  303.         for ($xx=0;$xx<8;$xx++) {
  304.           $zcrypted[$xx]=$tmp_zcrypted[$xx];
  305.         }
  306.  
  307.         for ($xx=0;$xx<8;$xx++) {
  308.           $plain_pre_8[$xx]=$plain[$xx];
  309.         }
  310.  
  311.         for ($xx=0;$xx<8;$xx++) {
  312.           $crypted_pre_8[$xx] = $zcrypted[$xx];
  313.         }
  314.         for ($xx=0;$xx<8;$xx++) {
  315.           $outstr[$count+$xx]=$zcrypted[$xx];
  316.         }
  317.         $count           +=  8;             /* outstrlen increase by 8*/
  318.         $pos_in_byte     =   0;
  319.         $is_header       =   0;             /* and exit header*/
  320.         /* encrypt_every_8_byte*/
  321.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  322.       }
  323.     }
  324.  
  325.     $inp_c=0;
  326.     while ($instrlen > 0) {
  327.       if ($pos_in_byte < 8) {
  328.         $plain[$pos_in_byte++] = $instr[$inp_c++];
  329.         $instrlen--;
  330.       }
  331.       if ($pos_in_byte == 8) {
  332.         //encrypt_every_8_byte();
  333.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  334.         /*** we encrypt every eight byte ***/
  335.         for ($pos_in_byte=0; $pos_in_byte<8; $pos_in_byte++) {
  336.           if($is_header) {
  337.             $plain[$pos_in_byte] = chr(ord($plain[$pos_in_byte]) ^ ord($plain_pre_8[$pos_in_byte]));
  338.           }
  339.           else {
  340.             $plain[$pos_in_byte] = chr(ord($plain[$pos_in_byte]) ^ ord($crypted_pre_8[$pos_in_byte]));
  341.           }
  342.         }/* prepare plain text*/
  343.  
  344.         for ($xx=0;$xx<8;$xx++) {
  345.           $zcrypted[$xx]="\x00";
  346.         }
  347.         //(unsigned int *)plain
  348.         //(unsigned int *)key
  349.         //(unsigned int *)zcrypted
  350.         $zcrypted=$this->teaEncipher($plain,$key);   /* encrypt it*/
  351.  
  352.         $tmp_zcrypted=array();
  353.         for ($xx=0;$xx<8;$xx++) {
  354.           $tmp_zcrypted[$xx]=$zcrypted[$xx];
  355.         }
  356.         for($pos_in_byte=0; $pos_in_byte<8; $pos_in_byte++) {
  357.           $tmp_zcrypted[$pos_in_byte] = chr(ord($tmp_zcrypted[$pos_in_byte]) ^ ord($plain_pre_8[$pos_in_byte]));
  358.         }
  359.         for ($xx=0;$xx<8;$xx++) {
  360.           $zcrypted[$xx]=$tmp_zcrypted[$xx];
  361.         }
  362.  
  363.         for ($xx=0;$xx<8;$xx++) {
  364.           $plain_pre_8[$xx]=$plain[$xx];
  365.         }
  366.  
  367.         for ($xx=0;$xx<8;$xx++) {
  368.           $crypted_pre_8[$xx] = $zcrypted[$xx];
  369.         }
  370.         for ($xx=0;$xx<8;$xx++) {
  371.           $outstr[$count+$xx]=$zcrypted[$xx];
  372.         }
  373.         $count           +=  8;             /* outstrlen increase by 8*/
  374.         $pos_in_byte     =   0;
  375.         $is_header       =   0;             /* and exit header*/
  376.         /* encrypt_every_8_byte*/
  377.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  378.       }
  379.     }
  380.  
  381.     $padding = 1; /* pad some stuff in tailer*/
  382.     while ($padding <= 7) { /* at most sever byte*/
  383.       if ($pos_in_byte < 8) {
  384.         $plain[$pos_in_byte++] = "\x00";
  385.         $padding++;
  386.       }
  387.       if ($pos_in_byte == 8) {
  388.         //encrypt_every_8_byte();
  389.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  390.         /*** we encrypt every eight byte ***/
  391.         for ($pos_in_byte=0; $pos_in_byte<8; $pos_in_byte++) {
  392.           if($is_header) {
  393.             $plain[$pos_in_byte] = chr(ord($plain[$pos_in_byte]) ^ ord($plain_pre_8[$pos_in_byte]));
  394.           }
  395.           else {
  396.             $plain[$pos_in_byte] = chr(ord($plain[$pos_in_byte]) ^ ord($crypted_pre_8[$pos_in_byte]));
  397.           }
  398.         }/* prepare plain text*/
  399.  
  400.         for ($xx=0;$xx<8;$xx++) {
  401.           $zcrypted[$xx]="\x00";
  402.         }
  403.         //(unsigned int *)plain
  404.         //(unsigned int *)key
  405.         //(unsigned int *)zcrypted
  406.         $zcrypted=$this->teaEncipher($plain,$key);   /* encrypt it*/
  407.  
  408.         $tmp_zcrypted=array();
  409.         for ($xx=0;$xx<8;$xx++) {
  410.           $tmp_zcrypted[$xx]=$zcrypted[$xx];
  411.         }
  412.         for($pos_in_byte=0; $pos_in_byte<8; $pos_in_byte++) {
  413.           $tmp_zcrypted[$pos_in_byte] = chr(ord($tmp_zcrypted[$pos_in_byte]) ^ ord($plain_pre_8[$pos_in_byte]));
  414.         }
  415.         for ($xx=0;$xx<8;$xx++) {
  416.           $zcrypted[$xx]=$tmp_zcrypted[$xx];
  417.         }
  418.  
  419.         for ($xx=0;$xx<8;$xx++) {
  420.           $plain_pre_8[$xx]=$plain[$xx];
  421.         }
  422.  
  423.         for ($xx=0;$xx<8;$xx++) {
  424.           $crypted_pre_8[$xx] = $zcrypted[$xx];
  425.         }
  426.         for ($xx=0;$xx<8;$xx++) {
  427.           $outstr[$count+$xx]=$zcrypted[$xx];
  428.         }
  429.         $count           +=  8;             /* outstrlen increase by 8*/
  430.         $pos_in_byte     =   0;
  431.         $is_header       =   0;             /* and exit header*/
  432.         /* encrypt_every_8_byte*/
  433.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  434.       }
  435.     }
  436.     //*outstrlen_ptr=count;/* everything is ok! set return string length*/
  437.  
  438.     return implode('',$outstr);
  439.   }
  440.  
  441.   function decrypt($instr,$key) {
  442.     $xx=0;//while counter
  443.     $zcrypt_buff_c=0;
  444.     $outstr=array();
  445.     $instrlen=strlen($instr);
  446.  
  447.     $decrypted=array(8);
  448.     $m[8]=array(8);
  449.     $zcrypt_buff=array(8);
  450.     $crypt_buff_pre_8=array(8);
  451.  
  452.     $bNeedRet=false;
  453.     $count=0;
  454.     $context_start=0;
  455.     $pos_in_byte=0;
  456.     $padding=0;
  457.  
  458.     /* at least 16 bytes and %8 == 0*/
  459.     if (($instrlen % 8) || ($instrlen < 16)) return;
  460.  
  461.     /* get information from header*/
  462.     $decrypted=$this->teaDecipher($instr,$key);
  463.  
  464.     $pos_in_byte = ord($decrypted[0]) & 0x7;
  465.     $count = $instrlen - $pos_in_byte - 10; /* this is the plaintext length*/
  466.  
  467.     /* return if outstr buffer is not large enought or error plaintext length*/
  468.     if ($count < 0) return;
  469.  
  470.     for ($xx=0;$xx<8;$xx++) {
  471.       $m[$xx]="\x00";
  472.       $crypt_buff_pre_8[$xx]="\x00";
  473.     }
  474.  
  475.     //*outstrlen_ptr = count;   /* everything is ok! set return string length*/
  476.  
  477.     $zcrypt_buff_c+=8;
  478.     for ($xx=0;$xx<8;$xx++) {
  479.       $zcrypt_buff[$xx]=$instr[$zcrypt_buff_c+$xx];
  480.     }
  481.  
  482.     $context_start = 8;        /* context is at the second 8 byte*/
  483.     $pos_in_byte++;            /* start of paddng stuffv*/
  484.  
  485.     $padding = 1;              /* at least one in header*/
  486.     while ($padding <= 2) {    /* there are 2 byte padding stuff in header*/
  487.       if ($pos_in_byte < 8) {  /* bypass the padding stuff, none sense data*/
  488.         $pos_in_byte++;
  489.         $padding++;
  490.       }
  491.       if ($pos_in_byte == 8) {
  492.         for ($xx=0;$xx<8;$xx++) {
  493.           $crypt_buff_pre_8[$xx]=$instr[$xx];
  494.         }
  495.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  496.         $bNeedRet = false;
  497.         for ($pos_in_byte = 0; $pos_in_byte < 8; $pos_in_byte++) {
  498.           if ($context_start + $pos_in_byte >= $instrlen) {
  499.             $bNeedRet = true;
  500.             break;
  501.           }
  502.           $decrypted[$pos_in_byte] = chr(ord($decrypted[$pos_in_byte]) ^ ord($zcrypt_buff[$pos_in_byte]));
  503.         }
  504.         if(!$bNeedRet) {
  505.           $decrypted=$this->teaDecipher($decrypted,$key);
  506.           $context_start += 8;
  507.           $zcrypt_buff_c+=8;
  508.           for ($xx=0;$xx<8;$xx++) {
  509.             // @@
  510.             $_t=$zcrypt_buff_c+$xx;
  511.             $_t>=strlen($instr) ? $_t=strlen($instr)-1 : "";
  512.             $zcrypt_buff[$xx]=$instr[$_t];
  513.           }
  514.  
  515.           $pos_in_byte = 0;
  516.         }
  517.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  518.       }
  519.     }
  520.  
  521.     $outp_c=0;
  522.     while($count !=0) {
  523.       if ($pos_in_byte < 8) {
  524.         $outstr[$outp_c++] = chr(ord($crypt_buff_pre_8[$pos_in_byte]) ^ ord($decrypted[$pos_in_byte]));
  525.         $count--;
  526.         $pos_in_byte++;
  527.       }
  528.       if ($pos_in_byte == 8) {
  529.         for($xx=0;$xx<8;$xx++)
  530.  {
  531.           $crypt_buff_pre_8[$xx]=$instr[$zcrypt_buff_c-8+$xx];
  532.         }
  533.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  534.         $bNeedRet = false;
  535.         for ($pos_in_byte = 0; $pos_in_byte < 8; $pos_in_byte++) {
  536.           if ($context_start + $pos_in_byte >= $instrlen) {
  537.             $bNeedRet = true;
  538.             break;
  539.           }
  540.           $decrypted[$pos_in_byte] = chr(ord($decrypted[$pos_in_byte]) ^ ord($zcrypt_buff[$pos_in_byte]));
  541.         }
  542.         if(!$bNeedRet) {
  543.           $decrypted=$this->teaDecipher($decrypted,$key);
  544.           $context_start += 8;
  545.           $zcrypt_buff_c+=8;
  546.  
  547.           for ($xx=0;$xx<8;$xx++) {
  548.             $zcrypt_buff_i=($zcrypt_buff_c+$xx>=$instrlen) ? "\x00" : $instr[$zcrypt_buff_c+$xx];//here may bigger
  549.             $zcrypt_buff[$xx]=$zcrypt_buff_i;
  550.           }
  551.  
  552.           $pos_in_byte = 0;
  553.         }
  554.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  555.       }
  556.     }
  557.  
  558.     for ($padding = 1; $padding < 8; $padding ++) {
  559.       if ($pos_in_byte < 8) {
  560.         if (ord($crypt_buff_pre_8[$pos_in_byte]) ^ ord($decrypted[$pos_in_byte])) return;
  561.         $pos_in_byte++;
  562.       }
  563.       if ($pos_in_byte == 8 ) {
  564.         for ($xx=0;$xx<8;$xx++) {
  565.           $crypt_buff_pre_8[$xx]=$zcrypt_buff[$xx];
  566.         }
  567.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  568.         $bNeedRet = false;
  569.         for ($pos_in_byte = 0; $pos_in_byte < 8; $pos_in_byte++) {
  570.           if ($context_start + $pos_in_byte >= $instrlen) {
  571.             $bNeedRet = true;
  572.             break;
  573.           }
  574.           $decrypted[$pos_in_byte] = chr(ord($decrypted[$pos_in_byte]) ^ ord($zcrypt_buff[$pos_in_byte]));
  575.         }
  576.         if(!$bNeedRet) {
  577.           $decrypted=$this->teaDecipher($decrypted,$key);
  578.           $context_start += 8;
  579.           $zcrypt_buff_c+=8;
  580.           for ($xx=0;$xx<8;$xx++) {
  581.             $zcrypt_buff[$xx]=$instr[$zcrypt_buff_c+$xx];
  582.           }
  583.  
  584.           $pos_in_byte = 0;
  585.         }
  586.         /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  587.       }
  588.     }
  589.  
  590.     return implode('',$outstr);
  591.   }
  592. }
  593.  
  594. //¾û
  595. $tcrypt=new TeaCrypt();
  596. $e=$tcrypt->encrypt("ÎÒ","1234567890abcdef");
  597. $d=$tcrypt->decrypt($e,"1234567890abcdef");
  598. echo "PHP_INT_SIZE=".PHP_INT_SIZE;
  599. echo "<br>";
  600. echo base64_encode($e);
  601. echo "<br>";
  602. echo $d;
  603. ?>
Advertisement
Add Comment
Please, Sign In to add comment