PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/utilities/simplecrypt.php

http://github.com/joomla/joomla-platform
PHP | 291 lines | 164 code | 25 blank | 102 comment | 11 complexity | fff86e31b457fd8099075718d1897b7b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Utilities
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 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 encrypting/decrypting strings
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Utilities
  15. * @since 11.1
  16. */
  17. class JSimpleCrypt extends JObject
  18. {
  19. /**
  20. * Encryption/Decryption Key
  21. *
  22. * @var string
  23. */
  24. protected $_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. * @since 11.1
  32. */
  33. public function __construct($key = null)
  34. {
  35. if ($key)
  36. {
  37. $this->_key = (string) $key;
  38. }
  39. else
  40. {
  41. $conf = &JFactory::getConfig();
  42. $this->_key = md5($conf->get('secret'));
  43. }
  44. }
  45. /**
  46. * Decrypt a string
  47. *
  48. * @param string $s String to decrypt
  49. *
  50. * @return string
  51. *
  52. * @since 11.1
  53. */
  54. public function decrypt($s)
  55. {
  56. $ai = $this->_hexToIntArray($s);
  57. (string) $s1 = $this->_xorString($ai);
  58. return $s1;
  59. }
  60. /**
  61. * Encrypt a string
  62. *
  63. * @param string $s String to encrypt
  64. *
  65. * @return string
  66. *
  67. * @since 11.1
  68. */
  69. public function encrypt($s)
  70. {
  71. $ai = $this->_xorCharString($s);
  72. $s1 = '';
  73. for ($i = 0, $count = count($ai); $i < $count; $i++)
  74. {
  75. $s1 = $s1 . $this->_intToHex((int) $ai[$i]);
  76. }
  77. return $s1;
  78. }
  79. /**
  80. * Convert hex to an integer
  81. *
  82. * @param string $s The hex string to convert.
  83. * @param integer $i The offset?
  84. *
  85. * @return integer
  86. *
  87. * @since 11.1
  88. */
  89. protected function _hexToInt($s, $i)
  90. {
  91. (int) $j = $i * 2;
  92. (string) $s1 = $s;
  93. // Get the char at position $j, length 1
  94. (string) $c = substr($s1, $j, 1);
  95. // Get the char at postion $j + 1, length 1
  96. (string) $c1 = substr($s1, $j + 1, 1);
  97. (int) $k = 0;
  98. switch ($c)
  99. {
  100. case "A":
  101. $k += 160;
  102. break;
  103. case "B":
  104. $k += 176;
  105. break;
  106. case "C":
  107. $k += 192;
  108. break;
  109. case "D":
  110. $k += 208;
  111. break;
  112. case "E":
  113. $k += 224;
  114. break;
  115. case "F":
  116. $k += 240;
  117. break;
  118. case " ":
  119. $k += 0;
  120. break;
  121. default:
  122. (int) $k = $k + (16 * (int) $c);
  123. break;
  124. }
  125. switch ($c1)
  126. {
  127. case "A":
  128. $k += 10;
  129. break;
  130. case "B":
  131. $k += 11;
  132. break;
  133. case "C":
  134. $k += 12;
  135. break;
  136. case "D":
  137. $k += 13;
  138. break;
  139. case "E":
  140. $k += 14;
  141. break;
  142. case "F":
  143. $k += 15;
  144. break;
  145. case " ":
  146. $k += 0;
  147. break;
  148. default:
  149. $k += (int) $c1;
  150. break;
  151. }
  152. return $k;
  153. }
  154. /**
  155. * Convert hex to an array of integers
  156. *
  157. * @param string $s The hex string to convert to an integer array.
  158. *
  159. * @return array An array of integers.
  160. *
  161. * @since 11.1
  162. */
  163. protected function _hexToIntArray($s)
  164. {
  165. (string) $s1 = $s;
  166. (int) $i = strlen($s1);
  167. (int) $j = $i / 2;
  168. for ($l = 0; $l < $j; $l++)
  169. {
  170. (int) $k = $this->_hexToInt($s1, $l);
  171. $ai[$l] = $k;
  172. }
  173. return $ai;
  174. }
  175. /**
  176. * Convert character string to integer
  177. *
  178. * @param string $c The character to convert to an integer.
  179. *
  180. * @return integer
  181. *
  182. * @since 11.1
  183. */
  184. protected function _charToInt($c)
  185. {
  186. $ac[0] = $c;
  187. return $ac;
  188. }
  189. /**
  190. * XorString
  191. *
  192. * @param string $ai The string.
  193. *
  194. * @return string
  195. *
  196. * @since 11.1
  197. */
  198. protected function _xorString($ai)
  199. {
  200. $s = $this->_key;
  201. (int) $i = strlen($s);
  202. $ai1 = $ai;
  203. (int) $j = count($ai1);
  204. for ($i = 0; $i < $j; $i = strlen($s))
  205. {
  206. $s = $s . $s;
  207. }
  208. for ($k = 0; $k < $j; $k++)
  209. {
  210. (string) $c = substr($s, $k, 1);
  211. $ac[$k] = chr($ai1[$k] ^ ord($c));
  212. }
  213. (string) $s1 = implode('', $ac);
  214. return $s1;
  215. }
  216. /**
  217. * Convert integer to hex
  218. *
  219. * @param integer $i An integer value to convert.
  220. *
  221. * @return string
  222. *
  223. * @since 11.1
  224. */
  225. protected function _intToHex($i)
  226. {
  227. (int) $j = (int) $i / 16;
  228. if ((int) $j == 0)
  229. {
  230. (string) $s = " ";
  231. }
  232. else
  233. {
  234. (string) $s = strtoupper(dechex($j));
  235. }
  236. (int) $k = (int) $i - (int) $j * 16;
  237. (string) $s = $s . strtoupper(dechex($k));
  238. return $s;
  239. }
  240. /**
  241. * Use xor encryption
  242. *
  243. * @param string $s The string.
  244. *
  245. * @return array An array of integers
  246. *
  247. * @since 11.1
  248. */
  249. protected function _xorCharString($s)
  250. {
  251. $ac = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  252. (string) $s1 = $this->_key;
  253. (int) $i = strlen($s1);
  254. (int) $j = count($ac);
  255. for ($i = 0; $i < $j; $i = strlen($s1))
  256. {
  257. $s1 = $s1 . $s1;
  258. }
  259. for ($k = 0; $k < $j; $k++)
  260. {
  261. $c = substr($s1, $k, 1);
  262. $ai[$k] = ord($c) ^ ord($ac[$k]);
  263. }
  264. return $ai;
  265. }
  266. }