Documentation is available at JaPCrypt.class.php
- <?php
- /**
- * JaPCrypt v0.1
- * HTTPS-like encryption by using Javascript and PHP
- */
- class JaPCrypt {
- /**
- * Set to true to send Unicode output
- *
- * @var boolean
- */
- public $unicode;
- /**
- * The TEA key
- *
- * @var string
- */
- public $TEAkey;
- /**
- * This function is used in cominbation with ob_start() to flush an encrypted output. An alternative to sendOutput().
- *
- * @param boolean $debug - used to show non-encrypted output for debugging
- */
- function ob_end_flush(boolean $debug=false) {
- $output = ob_get_contents();
- ob_end_clean();
- if ($debug == true) $this->sendOutput($output, true);
- else $this->sendOutput($output);
- }
- /**
- * Creates strings valid in "href" field of <a> tags.
- * Usage: <a href='<?= makeAnchor("/file.php?value=secret") ?>'>
- *
- * @param string $url the starting url
- * @return string the encrypted url
- */
- function makeAnchor(string $url) {
- $TEAkey = $this->TEAkey;
- $jap = new TEA;
- $url = parse_url($url);
- $newquery="";
- $array = split("&", $url['query']);
- $res = array();
- foreach ($array as $value) {
- preg_match("/(.*)=(.*)/", $value, $res);
- $res[2] = urlencode(base64_encode($jap->encrypt($res[2], $TEAkey)));
- $newquery .= $res[1]."=".$res[2]."&";
- }
- $return='';
- if ($url['scheme']) $return .= $url['scheme']."://";
- if ($url['user']) $return .= $url['user'].":";
- if ($url['pass']) $return .= $url['pass']."@";
- if ($url['host']) $return .= $url['host'];
- if ($url['port']) $return .= ":".$url['port'];
- if ($url['path']) $return .= $url['path'];
- if ($url['query']) $return .= "?".substr($newquery, 0, -1);
- if ($url['fragment']) $return .= "#".$url['fragment'];
- return $return;
- }
- /**
- * Decrypt GET or POST variables
- *
- * @param string $value GET or POST, specifying which values I want to decrypt
- */
- function getVars(string $value) {
- $TEAkey = $this->TEAkey;
- $jap = new TEA;
- if ($value == 'GET') {
- foreach($_GET as $key => $value) {
- $value = base64_decode($value);
- $_GET[$key] = $jap->decrypt($value, $TEAkey);
- }
- }
- else if ($value == 'POST') {
- foreach($_POST as $key => $value) {
- $value = base64_decode($value);
- $_POST[$key] = $jap->decrypt($value, $TEAkey);
- }
- }
- }
- /**
- * Sends encrypted output.
- *
- * @param string $TEAbody the content to be encrypted
- * @param boolean $debug used to show non-encrypted output for debugging
- */
- function sendOutput(string $TEAbody, boolean $debug=false) {
- $tea = new TEA();
- // $TEAkey = '6e6bc4e49dd477ebc98ef4046c067b5f';
- $TEAkey = $this->TEAkey;
- $output = $tea->encrypt($TEAbody, $TEAkey);
- $output = base64_encode($output);
- if ($debug === true) {
- echo "<!-- $TEAbody -->";
- }
- $this->sendJavascript($output);
- }
- /**
- * Function used by sendOutput() to generate the page loaded to send to the client.
- *
- * @param string $TEAjavascript the encrypted string
- */
- private function sendJavascript(string $TEAjavascript) {
- echo "<script type='text/javascript'>\n";
- include('js/functions.js');
- include('js/xxtea.js');
- include('js/cookies.js');
- include('js/base64.js');
- if ($this->unicode === true) include('js/unicode.js');
- echo "var TEAkey = Cookies.getTEAkey();\n";
- echo "var content = '{$TEAjavascript}';\n";
- echo "content = Base64.decode(content);\n";
- echo "content = TEA.decrypt(content, TEAkey);\n";
- if ($this->unicode === true) {
- echo "content = Utf8.decode(content);\n";
- }
- echo "document.write(content);\n";
- echo "</script>\n";
- }
- }
- /** XXTEA encryption arithmetic library.
- *
- * Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn>
- * Version: 1.5
- * LastModified: Dec 5, 2006
- * This library is free. You can redistribute it and/or modify it.
- */
- class TEA {
- private function long2str($v, $w) {
- $len = count($v);
- $n = ($len - 1) << 2;
- if ($w) {
- $m = $v[$len - 1];
- if (($m < $n - 3) || ($m > $n)) return false;
- $n = $m;
- }
- $s = array();
- for ($i = 0; $i < $len; $i++) {
- $s[$i] = pack("V", $v[$i]);
- }
- if ($w) {
- return substr(join('', $s), 0, $n);
- }
- else {
- return join('', $s);
- }
- }
- private function str2long($s, $w) {
- $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
- $v = array_values($v);
- if ($w) {
- $v[count($v)] = strlen($s);
- }
- return $v;
- }
- private function int32($n) {
- while ($n >= 2147483648) $n -= 4294967296;
- while ($n <= -2147483649) $n += 4294967296;
- return (int)$n;
- }
- /**
- * Encrypts a string using TEA algorithm.
- *
- * @param string $str - the string to be crypted
- * @param string $key - the key to be used
- * @return unknown - the encrypted string
- */
- function encrypt(string $str, string $key) {
- if ($str == "") {
- return "";
- }
- $v = $this->str2long($str, true);
- $k = $this->str2long($key, false);
- if (count($k) < 4) {
- for ($i = count($k); $i < 4; $i++) {
- $k[$i] = 0;
- }
- }
- $n = count($v) - 1;
- $z = $v[$n];
- $y = $v[0];
- $delta = 0x9E3779B9;
- $q = floor(6 + 52 / ($n + 1));
- $sum = 0;
- while (0 < $q--) {
- $sum = $this->int32($sum + $delta);
- $e = $sum >> 2 & 3;
- for ($p = 0; $p < $n; $p++) {
- $y = $v[$p + 1];
- $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
- $z = $v[$p] = $this->int32($v[$p] + $mx);
- }
- $y = $v[0];
- $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
- $z = $v[$n] = $this->int32($v[$n] + $mx);
- }
- return $this->long2str($v, false);
- }
- /**
- * Decrypts a string using TEA algorithm.
- *
- * @param string $str - the string to be decrypted
- * @param string $key - the key to be used
- * @return unknown - the decrypted string
- */
- function decrypt(string $str, string $key) {
- if ($str == "") {
- return "";
- }
- $v = $this->str2long($str, false);
- $k = $this->str2long($key, false);
- if (count($k) < 4) {
- for ($i = count($k); $i < 4; $i++) {
- $k[$i] = 0;
- }
- }
- $n = count($v) - 1;
- $z = $v[$n];
- $y = $v[0];
- $delta = 0x9E3779B9;
- $q = floor(6 + 52 / ($n + 1));
- $sum = $this->int32($q * $delta);
- while ($sum != 0) {
- $e = $sum >> 2 & 3;
- for ($p = $n; $p > 0; $p--) {
- $z = $v[$p - 1];
- $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
- $y = $v[$p] = $this->int32($v[$p] - $mx);
- }
- $z = $v[$n];
- $mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
- $y = $v[0] = $this->int32($v[0] - $mx);
- $sum = $this->int32($sum - $delta);
- }
- return $this->long2str($v, true);
- }
- }
- ?>
Documentation generated on Thu, 08 Nov 2007 09:55:13 +0100 by phpDocumentor 1.3.0RC3