PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/utilities/simplecrypt.php

https://bitbucket.org/joomla/joomla-platform/
PHP | 255 lines | 150 code | 12 blank | 93 comment | 13 complexity | 22acb2d23817723e082c011ee195cb0f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Utilities
  5. *
  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
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * JSimpleCrypt is a very simple encryption algorithm for encyrpting/decrypting strings
  12. *
  13. * @static
  14. * @package Joomla.Platform
  15. * @subpackage Utilities
  16. * @since 11.1
  17. */
  18. class JSimpleCrypt extends JObject
  19. {
  20. /**
  21. * Encryption/Decryption Key
  22. * @var string
  23. */
  24. private $_key;
  25. /**
  26. * Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the
  27. * secret word from the configuration object is used.
  28. *
  29. * @param string $key Optional encryption key
  30. *
  31. * @return void
  32. * @since 11.1
  33. */
  34. public function __construct($key = null)
  35. {
  36. if ($key) {
  37. $this->_key = (string) $key;
  38. } else {
  39. $conf = &JFactory::getConfig();
  40. $this->_key = md5($conf->get('secret'));
  41. }
  42. }
  43. /**
  44. * Decrypt
  45. *
  46. * @param string $s
  47. *
  48. * @return string
  49. * @since 11.1
  50. */
  51. public function decrypt($s)
  52. {
  53. $ai = $this->_hexToIntArray($s);
  54. (string) $s1 = $this->_xorString($ai);
  55. return $s1;
  56. }
  57. /**
  58. * Encrypt
  59. *
  60. * @param string $s
  61. *
  62. * @return string
  63. * @since 11.1
  64. */
  65. public function encrypt($s)
  66. {
  67. $ai = $this->_xorCharString($s);
  68. $s1 = "";
  69. for ($i = 0; $i < count($ai); $i++)
  70. $s1 = $s1 . $this->_intToHex((int) $ai[$i]);
  71. return $s1;
  72. }
  73. /**
  74. * HextoInt
  75. *
  76. * @param string $s
  77. * @param integer $i
  78. *
  79. * @return integer
  80. * @since 11.1
  81. */
  82. protected function _hexToInt($s, $i)
  83. {
  84. (int) $j = $i * 2;
  85. (string) $s1 = $s;
  86. (string) $c = substr($s1, $j, 1); // get the char at position $j, length 1
  87. (string) $c1 = substr($s1, $j +1, 1); // get the char at postion $j + 1, length 1
  88. (int) $k = 0;
  89. switch ($c)
  90. {
  91. case "A" :
  92. $k += 160;
  93. break;
  94. case "B" :
  95. $k += 176;
  96. break;
  97. case "C" :
  98. $k += 192;
  99. break;
  100. case "D" :
  101. $k += 208;
  102. break;
  103. case "E" :
  104. $k += 224;
  105. break;
  106. case "F" :
  107. $k += 240;
  108. break;
  109. case " " :
  110. $k += 0;
  111. break;
  112. default :
  113. (int) $k = $k + (16 * (int) $c);
  114. break;
  115. }
  116. switch ($c1)
  117. {
  118. case "A" :
  119. $k += 10;
  120. break;
  121. case "B" :
  122. $k += 11;
  123. break;
  124. case "C" :
  125. $k += 12;
  126. break;
  127. case "D" :
  128. $k += 13;
  129. break;
  130. case "E" :
  131. $k += 14;
  132. break;
  133. case "F" :
  134. $k += 15;
  135. break;
  136. case " " :
  137. $k += 0;
  138. break;
  139. default :
  140. $k += (int) $c1;
  141. break;
  142. }
  143. return $k;
  144. }
  145. /**
  146. * HexToIntArray
  147. *
  148. * @param string $s
  149. *
  150. * @return array
  151. * @since 11.1
  152. */
  153. protected function _hexToIntArray($s)
  154. {
  155. (string) $s1 = $s;
  156. (int) $i = strlen($s1);
  157. (int) $j = $i / 2;
  158. for ($l = 0; $l < $j; $l++) {
  159. (int) $k = $this->_hexToInt($s1, $l);
  160. $ai[$l] = $k;
  161. }
  162. return $ai;
  163. }
  164. /**
  165. * CharToInt
  166. *
  167. * @param string $c
  168. *
  169. * @return integer
  170. * @since 11.1
  171. */
  172. protected function _charToInt($c)
  173. {
  174. $ac[0] = $c;
  175. return $ac;
  176. }
  177. /**
  178. * XorString
  179. *
  180. * @param string $ai
  181. *
  182. * @return string
  183. * @since 11.1
  184. */
  185. protected function _xorString($ai)
  186. {
  187. $s = $this->_key; //
  188. (int) $i = strlen($s);
  189. $ai1 = $ai;
  190. (int) $j = count($ai1);
  191. for ($i = 0; $i < $j; $i = strlen($s))
  192. $s = $s . $s;
  193. for ($k = 0; $k < $j; $k++) {
  194. (string) $c = substr($s, $k, 1);
  195. $ac[$k] = chr($ai1[$k] ^ ord($c));
  196. }
  197. (string) $s1 = implode('', $ac);
  198. return $s1;
  199. }
  200. /**
  201. * inToHex
  202. *
  203. * @param integer $i
  204. *
  205. * @return string
  206. * @since 11.1
  207. */
  208. protected function _intToHex($i)
  209. {
  210. (int) $j = (int) $i / 16;
  211. if ((int) $j == 0) {
  212. (string) $s = " ";
  213. } else {
  214. (string) $s = strtoupper(dechex($j));
  215. }
  216. (int) $k = (int) $i - (int) $j * 16;
  217. (string) $s = $s . strtoupper(dechex($k));
  218. return $s;
  219. }
  220. /**
  221. * Decrypt
  222. *
  223. * @param string $s
  224. *
  225. * @return
  226. * @since 11.1
  227. */
  228. protected function _xorCharString($s)
  229. {
  230. $ac = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  231. (string) $s1 = $this->_key;
  232. (int) $i = strlen($s1);
  233. (int) $j = count($ac);
  234. for ($i = 0; $i < $j; $i = strlen($s1)) {
  235. $s1 = $s1 . $s1;
  236. }
  237. for ($k = 0; $k < $j; $k++) {
  238. $c = substr($s1, $k, 1);
  239. $ai[$k] = ord($c) ^ ord($ac[$k]);
  240. }
  241. return $ai;
  242. }
  243. }