Source for file JaPCrypt.class.php

Documentation is available at JaPCrypt.class.php

  1. <?php
  2. /**
  3. * JaPCrypt v0.1
  4. * HTTPS-like encryption by using Javascript and PHP
  5. */
  6. class JaPCrypt {
  7.  
  8. /**
  9. * Set to true to send Unicode output
  10. *
  11. * @var boolean
  12. */
  13. public $unicode;
  14. /**
  15. * The TEA key
  16. *
  17. * @var string
  18. */
  19. public $TEAkey;
  20.  
  21. /**
  22. * This function is used in cominbation with ob_start() to flush an encrypted output. An alternative to sendOutput().
  23. *
  24. * @param boolean $debug - used to show non-encrypted output for debugging
  25. */
  26. function ob_end_flush(boolean $debug=false) {
  27. $output = ob_get_contents();
  28. ob_end_clean();
  29. if ($debug == true) $this->sendOutput($output, true);
  30. else $this->sendOutput($output);
  31. }
  32.  
  33. /**
  34. * Creates strings valid in "href" field of <a> tags.
  35. * Usage: <a href='<?= makeAnchor("/file.php?value=secret") ?>'>
  36. *
  37. * @param string $url the starting url
  38. * @return string the encrypted url
  39. */
  40. function makeAnchor(string $url) {
  41. $TEAkey = $this->TEAkey;
  42. $jap = new TEA;
  43. $url = parse_url($url);
  44. $newquery="";
  45. $array = split("&", $url['query']);
  46. $res = array();
  47. foreach ($array as $value) {
  48. preg_match("/(.*)=(.*)/", $value, $res);
  49. $res[2] = urlencode(base64_encode($jap->encrypt($res[2], $TEAkey)));
  50. $newquery .= $res[1]."=".$res[2]."&";
  51. }
  52. $return='';
  53. if ($url['scheme']) $return .= $url['scheme']."://";
  54. if ($url['user']) $return .= $url['user'].":";
  55. if ($url['pass']) $return .= $url['pass']."@";
  56. if ($url['host']) $return .= $url['host'];
  57. if ($url['port']) $return .= ":".$url['port'];
  58. if ($url['path']) $return .= $url['path'];
  59. if ($url['query']) $return .= "?".substr($newquery, 0, -1);
  60. if ($url['fragment']) $return .= "#".$url['fragment'];
  61. return $return;
  62. }
  63.  
  64. /**
  65. * Decrypt GET or POST variables
  66. *
  67. * @param string $value GET or POST, specifying which values I want to decrypt
  68. */
  69. function getVars(string $value) {
  70. $TEAkey = $this->TEAkey;
  71. $jap = new TEA;
  72. if ($value == 'GET') {
  73. foreach($_GET as $key => $value) {
  74. $value = base64_decode($value);
  75. $_GET[$key] = $jap->decrypt($value, $TEAkey);
  76. }
  77. }
  78. else if ($value == 'POST') {
  79. foreach($_POST as $key => $value) {
  80. $value = base64_decode($value);
  81. $_POST[$key] = $jap->decrypt($value, $TEAkey);
  82. }
  83. }
  84. }
  85. /**
  86. * Sends encrypted output.
  87. *
  88. * @param string $TEAbody the content to be encrypted
  89. * @param boolean $debug used to show non-encrypted output for debugging
  90. */
  91. function sendOutput(string $TEAbody, boolean $debug=false) {
  92. $tea = new TEA();
  93. // $TEAkey = '6e6bc4e49dd477ebc98ef4046c067b5f';
  94. $TEAkey = $this->TEAkey;
  95. $output = $tea->encrypt($TEAbody, $TEAkey);
  96. $output = base64_encode($output);
  97. if ($debug === true) {
  98. echo "<!-- $TEAbody -->";
  99. }
  100. $this->sendJavascript($output);
  101. }
  102.  
  103. /**
  104. * Function used by sendOutput() to generate the page loaded to send to the client.
  105. *
  106. * @param string $TEAjavascript the encrypted string
  107. */
  108. private function sendJavascript(string $TEAjavascript) {
  109. echo "<script type='text/javascript'>\n";
  110. include('js/functions.js');
  111. include('js/xxtea.js');
  112. include('js/cookies.js');
  113. include('js/base64.js');
  114. if ($this->unicode === true) include('js/unicode.js');
  115. echo "var TEAkey = Cookies.getTEAkey();\n";
  116.  
  117. echo "var content = '{$TEAjavascript}';\n";
  118. echo "content = Base64.decode(content);\n";
  119. echo "content = TEA.decrypt(content, TEAkey);\n";
  120. if ($this->unicode === true) {
  121. echo "content = Utf8.decode(content);\n";
  122. }
  123. echo "document.write(content);\n";
  124. echo "</script>\n";
  125. }
  126. }
  127.  
  128. /** XXTEA encryption arithmetic library.
  129. *
  130. * Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn>
  131. * Version: 1.5
  132. * LastModified: Dec 5, 2006
  133. * This library is free. You can redistribute it and/or modify it.
  134. */
  135. class TEA {
  136.  
  137. private function long2str($v, $w) {
  138. $len = count($v);
  139. $n = ($len - 1) << 2;
  140. if ($w) {
  141. $m = $v[$len - 1];
  142. if (($m < $n - 3) || ($m > $n)) return false;
  143. $n = $m;
  144. }
  145. $s = array();
  146. for ($i = 0; $i < $len; $i++) {
  147. $s[$i] = pack("V", $v[$i]);
  148. }
  149. if ($w) {
  150. return substr(join('', $s), 0, $n);
  151. }
  152. else {
  153. return join('', $s);
  154. }
  155. }
  156.  
  157. private function str2long($s, $w) {
  158. $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
  159. $v = array_values($v);
  160. if ($w) {
  161. $v[count($v)] = strlen($s);
  162. }
  163. return $v;
  164. }
  165.  
  166. private function int32($n) {
  167. while ($n >= 2147483648) $n -= 4294967296;
  168. while ($n <= -2147483649) $n += 4294967296;
  169. return (int)$n;
  170. }
  171.  
  172. /**
  173. * Encrypts a string using TEA algorithm.
  174. *
  175. * @param string $str - the string to be crypted
  176. * @param string $key - the key to be used
  177. * @return unknown - the encrypted string
  178. */
  179. function encrypt(string $str, string $key) {
  180. if ($str == "") {
  181. return "";
  182. }
  183. $v = $this->str2long($str, true);
  184. $k = $this->str2long($key, false);
  185. if (count($k) < 4) {
  186. for ($i = count($k); $i < 4; $i++) {
  187. $k[$i] = 0;
  188. }
  189. }
  190. $n = count($v) - 1;
  191.  
  192. $z = $v[$n];
  193. $y = $v[0];
  194. $delta = 0x9E3779B9;
  195. $q = floor(6 + 52 / ($n + 1));
  196. $sum = 0;
  197. while (0 < $q--) {
  198. $sum = $this->int32($sum + $delta);
  199. $e = $sum >> 2 & 3;
  200. for ($p = 0; $p < $n; $p++) {
  201. $y = $v[$p + 1];
  202. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  203. $z = $v[$p] = $this->int32($v[$p] + $mx);
  204. }
  205. $y = $v[0];
  206. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  207. $z = $v[$n] = $this->int32($v[$n] + $mx);
  208. }
  209. return $this->long2str($v, false);
  210. }
  211.  
  212. /**
  213. * Decrypts a string using TEA algorithm.
  214. *
  215. * @param string $str - the string to be decrypted
  216. * @param string $key - the key to be used
  217. * @return unknown - the decrypted string
  218. */
  219. function decrypt(string $str, string $key) {
  220. if ($str == "") {
  221. return "";
  222. }
  223. $v = $this->str2long($str, false);
  224. $k = $this->str2long($key, false);
  225. if (count($k) < 4) {
  226. for ($i = count($k); $i < 4; $i++) {
  227. $k[$i] = 0;
  228. }
  229. }
  230. $n = count($v) - 1;
  231.  
  232. $z = $v[$n];
  233. $y = $v[0];
  234. $delta = 0x9E3779B9;
  235. $q = floor(6 + 52 / ($n + 1));
  236. $sum = $this->int32($q * $delta);
  237. while ($sum != 0) {
  238. $e = $sum >> 2 & 3;
  239. for ($p = $n; $p > 0; $p--) {
  240. $z = $v[$p - 1];
  241. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  242. $y = $v[$p] = $this->int32($v[$p] - $mx);
  243. }
  244. $z = $v[$n];
  245. $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
  246. $y = $v[0] = $this->int32($v[0] - $mx);
  247. $sum = $this->int32($sum - $delta);
  248. }
  249. return $this->long2str($v, true);
  250. }
  251. }
  252. ?>

Documentation generated on Thu, 08 Nov 2007 09:55:13 +0100 by phpDocumentor 1.3.0RC3