PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/utilities/simplecrypt.php

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