Answer the question
In order to leave comments, you need to log in
How can TEA encryption algorithm be implemented in php?
Hello, I'm trying to implement the TEA encryption method in PHP, but it doesn't work. I found an implementation only in C:
#include <stdint.h>
void encrypt (uint32_t* v, uint32_t* k)
{
/* set up */
uint32_t v0 = v[0];
uint32_t v1 = v[1];
uint32_t sum = 0;
uint32_t i;
/* a key schedule constant */
uint32_t delta = 0x9e3779b9;
/* cache key */
uint32_t k0 = k[0];
uint32_t k1 = k[1];
uint32_t k2 = k[2];
uint32_t k3 = k[3];
/* basic cycle start */
for (i = 0; i < 32; i++)
{
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;
}
void decrypt (uint32_t* v, uint32_t* k)
{
/* set up */
uint32_t v0 = v[0];
uint32_t v1 = v[1];
uint32_t sum = 0xC6EF3720;
uint32_t i;
/* a key schedule constant */
uint32_t delta = 0x9e3779b9;
/* cache key */
uint32_t k0 = k[0];
uint32_t k1 = k[1];
uint32_t k2 = k[2];
uint32_t k3 = k[3];
/* basic cycle start */
for (i = 0; i < 32; i++)
{
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;
}
<?
$key=generatePassword();
function generatePassword($length = 8){
$chars = 'abcdefghijklmnopqrstyzABCDEFGHIJKMNOPQRSTYZ0123456789';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < 8; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
?>
$text='HelloWorld';
$key0=substr($key, 0, 2);
$key1=substr($key, 2, 2);
$key2=substr($key, 4, 2);
$key3=substr($key, 6, 2);
//$text=base64_encode($text);
//$v['1']=pack('L*', $t2);
$v1=substr($text, 0, 4);
$v2=substr($text, 4, 4);
$encript=block_encrypt($v1, $v2, $key);
$decrypt=block_decrypt($encript['0'], $encript['1'], $key,$encript['sum']);
echo $key.'<br>';
echo $text.'<br>';
print_r($encript);
print_r($decrypt);
//echo $key.'<br>'.$key1.'<br>'.$key2.'<br>'.$key3.'<br>'.$key4;
function block_encrypt($y, $z, $key) {
$key0=substr($key, 0, 2);
$key1=substr($key, 2, 2);
$key2=substr($key, 4, 2);
$key3=substr($key, 6, 2);
$sum=0;
$delta=0x9e3779b9;
for ($i=0; $i<32; $i++)
{
$sum += $delta;
$y += (($z << 4) + $key0) ^ ($z + $sum) ^ (($z >> 5) + $key1);
$z += (($y << 4) + $key2) ^ ($y + $sum) ^ (($y >> 5) + $key3);
}
$v['0'] = $y;
$v['1'] = $z;
$v['sum']=$sum;
return $v;
}
function block_decrypt($y, $z, $key, $sum) {
$key0=substr($key, 0, 2);
$key1=substr($key, 2, 2);
$key2=substr($key, 4, 2);
$key3=substr($key, 6, 2);
$delta=0x9e3779b9;
for ($i=0; $i<32; $i++)
{
$z += (($y << 4) + $key2) ^ ($y + $sum) ^ (($y >> 5) + $key3);
$y += (($z << 4) + $key0) ^ ($z + $sum) ^ (($z >> 5) + $key1);
$sum -= $delta;
}
$v['0'] = $y;
$v['1'] = $z;
return $v;
}
?>
Answer the question
In order to leave comments, you need to log in
Take the finished implementation and hope it's good: rryqszq4/tea-php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question