S
S
sparco3562017-04-04 21:50:11
PHP
sparco356, 2017-04-04 21:50:11

What is my mistake in php code?

Hello! Please tell me what is my mistake when trying to run the TEA encryption method in PHP?
I found on a foreign site the implementation of this method in two languages ​​JS and PHP. I'm trying to encrypt the same data, in php it gives out something that is not clear, but in JS it works correctly, tell me please, kind people, what is the mistake?
I am attaching the code.

<?php
 
/**
 * @author www.Tutorialspots.com
 * @copyright 2017
 */
 $a=array(65,65);
 print_r($a);
 $k=array(65,65,65,65);   


$p1=TEA::TEAencrypt($a,$k);
 print_r($p1);
 $p2=TEA::TEAdecrypt($p1,$k);
 print_r($p2);

class TEA{
    /**
     * TEA encrypt
     * @param $v 64bits
     * @param $k 128bits
     */
    public static function TEAencrypt ($v, $k) {
        $v0=$v[0]; $v1=$v[1]; $sum=0;           /* set up */
        $delta=0x9e3779b9;                     /* a key schedule constant */
        $k0=$k[0]; $k1=$k[1]; $k2=$k[2]; $k3=$k[3];   /* cache key */
        for ($i=0; $i < 32; $i++) {                       /* basic cycle start */
            $sum += $delta;
            $v0 += (($v1<<4) + $k0) ^ ($v1 + $sum) ^ (($v1>>5) + $k1);
            $v1 += (($v0<<4) + $k2) ^ ($v0 + $sum) ^ (($v0>>5) + $k3);
        }
        echo $sum;                                              /* end cycle */
        $v[0]=$v0; $v[1]=$v1; 
        return $v;
    }
     
    /**
     * TEA decrypt
     * @param $v 64bits
     * @param $k 128bits
     */
    public static function TEAdecrypt ($v, $k) {
        $v0=$v[0]; $v1=$v[1]; $sum=0xC6EF3720;  /* set up */
        $delta=0x9e3779b9;                     /* a key schedule constant */
        $k0=$k[0]; $k1=$k[1]; $k2=$k[2]; $k3=$k[3];   /* cache key */
        for ($i=0; $i<32; $i++) {                         /* basic cycle start */
            $v1 -= (($v0<<4) + $k2) ^ ($v0 + $sum) ^ (($v0>>5) + $k3);
            $v0 -= (($v1<<4) + $k0) ^ ($v1 + $sum) ^ (($v1>>5) + $k1);
            $sum -= $delta;
        }   
        echo $sum;                                           /* end cycle */
        $v[0]=$v0; $v[1]=$v1;
        return $v;
    }
}
 
?>

<script type="text/javascript">
    var TEA = {};
   /**
     * TEA encrypt
     * @param v 64bits
     * @param k 128bits
     */
TEA.TEAencrypt = function (v, k) {
  var v0=v[0], v1=v[1], sum=0,           /* set up */
  delta=0x9e3779b9,                     /* a key schedule constant */
  k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
  for (var i=0; i < 32; i++) {                       /* basic cycle start */
    sum += delta;
    v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
    v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
  }                                              /* end cycle */
  v[0]=v0; v[1]=v1;
  return v;
}
 
   /**
     * TEA decrypt
     * @param v 64bits
     * @param k 128bits
     */
TEA.TEAdecrypt = function (v, k) {
  var v0=v[0], v1=v[1], sum=0xC6EF3720,  /* set up */
  delta=0x9e3779b9,                     /* a key schedule constant */
  k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
  for (var i=0; i<32; i++) {                         /* basic cycle start */
    v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
    sum -= delta;
  }                                              /* end cycle */
  v[0]=v0; v[1]=v1;
  return v;
}
var x=TEA.TEAencrypt([65,65],[65,65,65,65]);
console.log(x);//[-3257293243,6661612959]
alert(TEA.TEAdecrypt(x,[65,65,65,65]));
</script>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question