In cryptography, Corrected Block TEA (often referred to as XXTEA) is a block cipher designed to correct weaknesses in the original Block TEA.
Source: https://en.wikipedia.org/wiki/XXTEA
We provide some function implement in PHP and Javascript:
PHP
<?php /** * @author www.Tutorialspots.com * @copyright 2017 */ /** * Corrected Block TEA implement in PHP * @param $v: n word data vector * @param $key: 4 word key * @param $n: length of data vector $v (positive for encrypt, negative for decrypt) */ function XXTEA($v, $key, $n = 2) { $delta = 0x9e3779b9; if ($n > 1) { /* Coding Part */ $rounds = 6 + 52 / $n; $sum = 0; $z = $v[$n - 1]; do { $sum += $delta; $e = ($sum >> 2) & 3; for ($p = 0; $p < $n - 1; $p++) { $y = $v[$p + 1]; $z = $v[$p] += ((($z >> 5 ^ $y << 2) + ($y >> 3 ^ $z << 4)) ^ (($sum ^ $y) + ($key[($p & 3) ^ $e] ^ $z))); } $y = $v[0]; $z = $v[$n - 1] += ((($z >> 5 ^ $y << 2) + ($y >> 3 ^ $z << 4)) ^ (($sum ^ $y) + ($key[($p & 3) ^ $e] ^ $z))); } while (--$rounds); } else if ($n < -1) { /* Decoding Part */ $n = -$n; $rounds = 6 + 52 / $n; $sum = $rounds * $delta; $y = $v[0]; do { $e = ($sum >> 2) & 3; for ($p = $n - 1; $p > 0; $p--) { $z = $v[$p - 1]; $y = $v[$p] -= ((($z >> 5 ^ $y << 2) + ($y >> 3 ^ $z << 4)) ^ (($sum ^ $y) + ($key[($p & 3) ^ $e] ^ $z))); } $z = $v[$n - 1]; $y = $v[0] -= ((($z >> 5 ^ $y << 2) + ($y >> 3 ^ $z << 4)) ^ (($sum ^ $y) + ($key[($p & 3) ^ $e] ^ $z))); $sum -= $delta; } while (--$rounds); } return $v; } ?>
Javascript:
/** * Corrected Block TEA implement in PHP by www.Tutorialspots.com * @param v: n word data vector * @param key: 4 word key * @param n: length of data vector v (positive for encrypt, negative for decrypt) */ function XXTEA(v, key, n) { var y, z, sum, p, rounds, e, delta = 0x9e3779b9; if (n > 1) { /* Coding Part */ rounds = 6 + 52 / n; sum = 0; z = v[n - 1]; do { sum += delta; e = (sum >> 2) & 3; for (p = 0; p < n - 1; p++) { y = v[p + 1]; z = v[p] += (((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))); } y = v[0]; z = v[n - 1] += (((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))); } while (--rounds); } else if (n < -1) { /* Decoding Part */ n = -n; rounds = 6 + 52 / n; sum = rounds * delta; y = v[0]; do { e = (sum >> 2) & 3; for (p = n - 1; p > 0; p--) { z = v[p - 1]; y = v[p] -= (((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))); } z = v[n - 1]; y = v[0] -= (((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))); sum -= delta; } while (--rounds); } return v; }
Example:
test = XXTEA([65,65],[65,65,65,65],2); console.log(test);//[-8128622196,2936119485] test2 = XXTEA(test,[65,65,65,65],-2); console.log(test2);//[65, 65]
Read more:
TEA block cipher implement in PHP and Javascript
Test vectors TEA
XTEA block cipher implement in PHP and Javascript
1 Comment
TEA block cipher implement in PHP and Javascript | Free Online Tutorials
(February 12, 2017 - 11:17 am)[…] Read more: Test vectors TEA XTEA block cipher implement in PHP and Javascript XXTEA block cipher implement in PHP and Javascript […]