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

/libraries/joomla/utilities/simplecrypt.php

https://github.com/dg482/joomla-platform
PHP | 289 lines | 164 code | 23 blank | 102 comment | 11 complexity | 81f345c0a30603a673b92515cb19939f MD5 | raw file
  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. * @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. * @return void
  32. *
  33. * @since 11.1
  34. */
  35. public function __construct($key = null)
  36. {
  37. if ($key)
  38. {
  39. $this->_key = (string) $key;
  40. }
  41. else
  42. {
  43. $conf = &JFactory::getConfig();
  44. $this->_key = md5($conf->get('secret'));
  45. }
  46. }
  47. /**
  48. * Decrypt a string
  49. *
  50. * @param string $s String to decrypt
  51. *
  52. * @return string
  53. *
  54. * @since 11.1
  55. */
  56. public function decrypt($s)
  57. {
  58. $ai = $this->_hexToIntArray($s);
  59. (string) $s1 = $this->_xorString($ai);
  60. return $s1;
  61. }
  62. /**
  63. * Encrypt a string
  64. *
  65. * @param string $s String to encrypt
  66. *
  67. * @return string
  68. *
  69. * @since 11.1
  70. */
  71. public function encrypt($s)
  72. {
  73. $ai = $this->_xorCharString($s);
  74. $s1 = '';
  75. for ($i = 0, $count = count($ai); $i < $count; $i++)
  76. {
  77. $s1 = $s1 . $this->_intToHex((int) $ai[$i]);
  78. }
  79. return $s1;
  80. }
  81. /**
  82. * Convert hex to an integer
  83. *
  84. * @param string $s The hex string to convert.
  85. * @param integer $i The offset?
  86. *
  87. * @return integer
  88. *
  89. * @since 11.1
  90. */
  91. protected function _hexToInt($s, $i)
  92. {
  93. (int) $j = $i * 2;
  94. (string) $s1 = $s;
  95. (string) $c = substr($s1, $j, 1); // get the char at position $j, length 1
  96. (string) $c1 = substr($s1, $j + 1, 1); // get the char at postion $j + 1, length 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. }