1--TEST--
2decrypt()
3--FILE--
4<?php
5require_once 'Crypt/XXTEA.php';
6
7$XXTEA = new Crypt_XXTEA();
8
9// key-plaintext tuples for string encryptiont test
10$tuples_string = array(
11    array('abcdefghijklmnop', 'UsFmrB/Q8zLl+5TIYicLk+mMgrj41jRB'),
12    array('gd2fk$2ds', '0ec5tIhpKCIiwdN05MynE+qbtjtD45pEUsaVGaKkHKlD2AewGrjAryZOjQIYLY20HZTbq6Y/YcCcNoAJ'),
13    array('p', 'iRj+IHHKrz5yXL6w7PnvflYzHoDItfi7Eg8+dyaL79vPbCUyRw2eVCKnU7LO2zbxYzj1T+o2kTA+MMhX85ktjddmC6FuVafophvx4Qxc9LU='),
14    array("\xb2\x93\x13\xe5\x96", 'bmeLisYRXKwB6xSK1v10c/QUYJh/iAtEX9q/qX74k8A3l/jVd++Gb41N327ARdZVrUxOG24Rw46WeS8LcEpML585kbOglxY6SbmUaw=='),
15);
16
17// key-plaintext tuples for long integer array encryptiont test
18$tuples_array = array(
19    array(array(0, 0, 0, 0), array(87491755, 1465748608)),
20    array(array(0), array(-1698224617, 1492735309, 566640726, 178473705, 1574735419)),
21);
22
23// the following key-plaintext tuples are from http://www.crypt.co.za/post/27
24$tuples_hex = array(
25    array('6a6f686e636b656e64616c6c6a6f686e', '014e7a34874eeb29'),
26    array('6a6f686e636b656e64616c6c6a6f686e', 'e9d39f636e9ed090'),
27    array('6a6f686e636b656e64616c6c6a6f686e', 'd20ec51c06feaf0e'),
28    array('6a6f686e636b656e64616c6c6a6f686e', 'b1551d6ffcd4b61b'),
29    array('6a6f686e636b656e64616c6c6a6f686e', '0ff91e518b9837e3'),
30    array('6a6f686e636b656e64616c6c6a6f686e', '7003fc98b6788a77'),
31    array('6a6f686e636b656e64616c6c6a6f686e', '93951ad360650022'),
32    array('6a6f686e636b656e64616c6c6a6f686e', 'cdeb72b9c903ce52'),
33    array('00000000000000000000000000000000', 'ab043705808c5d57'),
34    array('0102040810204080fffefcf8f0e0c080', 'd1e78be2c746728a'),
35    array('9e3779b99b9773e9b979379e6b695156', '67ed0ea8e8973fc5'),
36    array('0102040810204080fffefcf8f0e0c080', '8c3707c01c7fccc4'),
37    array('ffffffffffffffffffffffffffffffff', 'b2601cefb078b772abccba6a'),
38    array('9e3779b99b9773e9b979379e6b695156', '579016d143ed6247ac6710dd'),
39    array('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'c0a19f06ebb0d63925aa27f74cc6b2d0'),
40    array('9e3779b99b9773e9b979379e6b695156', '01b815fd2e4894d13555da434c9d868a'),
41    array('0102040810204080fffefcf8f0e0c080', '51f0ffeb46012a245e0c6c4fa097db27caec698d'),
42    array('9e3779b99b9773e9b979379e6b695156', '759e5b212ee58be734d610248e1daa1c9d0647d428b4f95a'),
43    array('9e3779b99b9773e9b979379e6b695156', '8e63ae7d8a119566990eb756f16abf94ff87359803ca12fbaa03fdfb'),
44    array('0102040810204080fffefcf8f0e0c080', '5ef1b6e010a2227ba337374b59beffc5263503054745fb513000641e2c7dd107')
45);
46
47$result = $XXTEA->decrypt(null);
48if (PEAR::isError($result)) {
49    echo $result->getMessage();
50} else {
51    echo base64_encode($result);
52}
53echo "\n";
54
55$XXTEA->setKey('abc');
56$result = $XXTEA->decrypt(null);
57if (PEAR::isError($result)) {
58    echo $result->getMessage();
59} else {
60    echo base64_encode($result);
61}
62echo "\n";
63
64foreach ($tuples_string as $tuple) {
65    $XXTEA->setKey($tuple[0]);
66    $result = $XXTEA->decrypt(base64_decode($tuple[1]));
67    echo $result;
68    echo "\n";
69}
70
71foreach ($tuples_array as $tuple) {
72    $XXTEA->setKey($tuple[0]);
73    $result = $XXTEA->decrypt($tuple[1]);
74    echo 'array(' . implode(', ', $result) . ')';
75    echo "\n";
76}
77
78foreach ($tuples_hex as $tuple) {
79    $XXTEA->setKey(hex2long($tuple[0]));
80    $result = $XXTEA->decrypt(hex2long($tuple[1]));
81    echo long2hex($result);
82    echo "\n";
83}
84
85function hex2long($hex) {
86    return array_values(unpack('V*', pack('H*', $hex)));
87}
88
89function long2hex($arr) {
90    $len = count($arr);
91    $hex = '';
92    for ($i = 0; $i < $len; $i++) {
93        $hex .= pack('V', $arr[$i]);
94    }
95    return array_shift(unpack('H*', $hex));
96}
97
98?>
99--EXPECT--
100Secret key is undefined.
101The chiper text must be a string or long integer array.
102[encryption test]
103PHP is a widely-used general-purpose scripting language
104It is especially suited for Web development and can be embedded into HTML
105PEAR is a framework and distribution system for reusable PHP components
106array(0, 0)
107array(0, 0, 0, 0, 0)
1084100000000000000
1094142000000000000
1104142430000000000
1114142434400000000
1124142434445000000
1134142434445460000
1144142434445464700
1154142434445464748
1160000000000000000
1170000000000000000
118ffffffffffffffff
119fffefcf8f0e0c080
120157c13a850ba5e57306d7791
121157c13a850ba5e57306d7791
1220102040810204080fffefcf8f0e0c080
1230102040810204080fffefcf8f0e0c080
124157c13a850ba5e57306d77916fa2c37be1949616
125690342f45054a708c475c91db77761bc01b815fd2e4894d1
1263555da434c9d868a1431e73e73372fc0688e09ce11d00b6fd936a764
127db9af3c96e36a30c643c6e97f4d75b7a4b51a40e9d8759e581e3c40b341b4436
128