PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/joomla/utilities/simplecrypt.php

https://github.com/reechalee/joomla1.6
PHP | 200 lines | 150 code | 20 blank | 30 comment | 13 complexity | 9b258615327dc929d25978cd3c611154 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. /**
  3. * @version $Id: simplecrypt.php 20196 2011-01-09 02:40:25Z ian $
  4. * @package Joomla.Framework
  5. * @subpackage Utilities
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * JSimpleCrypt is a very simple encryption algorithm for encyrpting/decrypting strings
  13. *
  14. * @static
  15. * @package Joomla.Framework
  16. * @subpackage Utilities
  17. * @since 1.5
  18. */
  19. class JSimpleCrypt extends JObject
  20. {
  21. /**
  22. * Encryption/Decryption Key
  23. * @access private
  24. * @var string
  25. */
  26. var $_key;
  27. /**
  28. * Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the
  29. * secret word from the configuration object is used.
  30. *
  31. * @access protected
  32. * @param string $key Optional encryption key
  33. * @return void
  34. * @since 1.5
  35. */
  36. function __construct($key = null)
  37. {
  38. if ($key) {
  39. $this->_key = (string) $key;
  40. } else {
  41. $conf = &JFactory::getConfig();
  42. $this->_key = md5($conf->get('secret'));
  43. }
  44. }
  45. function decrypt($s)
  46. {
  47. $ai = $this->_hexToIntArray($s);
  48. (string) $s1 = $this->_xorString($ai);
  49. return $s1;
  50. }
  51. function encrypt($s)
  52. {
  53. $ai = $this->_xorCharString($s);
  54. $s1 = "";
  55. for ($i = 0; $i < count($ai); $i++)
  56. $s1 = $s1 . $this->_intToHex((int) $ai[$i]);
  57. return $s1;
  58. }
  59. function _hexToInt($s, $i)
  60. {
  61. (int) $j = $i * 2;
  62. (string) $s1 = $s;
  63. (string) $c = substr($s1, $j, 1); // get the char at position $j, length 1
  64. (string) $c1 = substr($s1, $j +1, 1); // get the char at postion $j + 1, length 1
  65. (int) $k = 0;
  66. switch ($c)
  67. {
  68. case "A" :
  69. $k += 160;
  70. break;
  71. case "B" :
  72. $k += 176;
  73. break;
  74. case "C" :
  75. $k += 192;
  76. break;
  77. case "D" :
  78. $k += 208;
  79. break;
  80. case "E" :
  81. $k += 224;
  82. break;
  83. case "F" :
  84. $k += 240;
  85. break;
  86. case " " :
  87. $k += 0;
  88. break;
  89. default :
  90. (int) $k = $k + (16 * (int) $c);
  91. break;
  92. }
  93. switch ($c1)
  94. {
  95. case "A" :
  96. $k += 10;
  97. break;
  98. case "B" :
  99. $k += 11;
  100. break;
  101. case "C" :
  102. $k += 12;
  103. break;
  104. case "D" :
  105. $k += 13;
  106. break;
  107. case "E" :
  108. $k += 14;
  109. break;
  110. case "F" :
  111. $k += 15;
  112. break;
  113. case " " :
  114. $k += 0;
  115. break;
  116. default :
  117. $k += (int) $c1;
  118. break;
  119. }
  120. return $k;
  121. }
  122. function _hexToIntArray($s)
  123. {
  124. (string) $s1 = $s;
  125. (int) $i = strlen($s1);
  126. (int) $j = $i / 2;
  127. for ($l = 0; $l < $j; $l++) {
  128. (int) $k = $this->_hexToInt($s1, $l);
  129. $ai[$l] = $k;
  130. }
  131. return $ai;
  132. }
  133. function _charToInt($c)
  134. {
  135. $ac[0] = $c;
  136. return $ac;
  137. }
  138. function _xorString($ai)
  139. {
  140. $s = $this->_key; //
  141. (int) $i = strlen($s);
  142. $ai1 = $ai;
  143. (int) $j = count($ai1);
  144. for ($i = 0; $i < $j; $i = strlen($s))
  145. $s = $s . $s;
  146. for ($k = 0; $k < $j; $k++) {
  147. (string) $c = substr($s, $k, 1);
  148. $ac[$k] = chr($ai1[$k] ^ ord($c));
  149. }
  150. (string) $s1 = implode('', $ac);
  151. return $s1;
  152. }
  153. function _intToHex($i)
  154. {
  155. (int) $j = (int) $i / 16;
  156. if ((int) $j == 0) {
  157. (string) $s = " ";
  158. } else {
  159. (string) $s = strtoupper(dechex($j));
  160. }
  161. (int) $k = (int) $i - (int) $j * 16;
  162. (string) $s = $s . strtoupper(dechex($k));
  163. return $s;
  164. }
  165. function _xorCharString($s)
  166. {
  167. $ac = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  168. (string) $s1 = $this->_key;
  169. (int) $i = strlen($s1);
  170. (int) $j = count($ac);
  171. for ($i = 0; $i < $j; $i = strlen($s1)) {
  172. $s1 = $s1 . $s1;
  173. }
  174. for ($k = 0; $k < $j; $k++) {
  175. $c = substr($s1, $k, 1);
  176. $ai[$k] = ord($c) ^ ord($ac[$k]);
  177. }
  178. return $ai;
  179. }
  180. }