PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/crypt/cipher/simple.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 288 lines | 152 code | 33 blank | 103 comment | 13 complexity | cd53f062a4e146b75833ba1d4dae3bbc MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Crypt
  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. * JCrypt cipher for Simple encryption, decryption and key generation.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Crypt
  15. * @since 12.1
  16. */
  17. class JCryptCipherSimple implements JCryptCipher
  18. {
  19. /**
  20. * Method to decrypt a data string.
  21. *
  22. * @param string $data The encrypted string to decrypt.
  23. * @param JCryptKey $key The key[/pair] object to use for decryption.
  24. *
  25. * @return string The decrypted data string.
  26. *
  27. * @since 12.1
  28. * @throws InvalidArgumentException
  29. */
  30. public function decrypt($data, JCryptKey $key)
  31. {
  32. // Validate key.
  33. if ($key->type != 'simple')
  34. {
  35. throw new InvalidArgumentException('Invalid key of type: ' . $key->type . '. Expected simple.');
  36. }
  37. // Initialise variables.
  38. $decrypted = '';
  39. $tmp = $key->public;
  40. // Convert the HEX input into an array of integers and get the number of characters.
  41. $chars = $this->_hexToIntArray($data);
  42. $charCount = count($chars);
  43. // Repeat the key as many times as necessary to ensure that the key is at least as long as the input.
  44. for ($i = 0; $i < $charCount; $i = strlen($tmp))
  45. {
  46. $tmp = $tmp . $tmp;
  47. }
  48. // Get the XOR values between the ASCII values of the input and key characters for all input offsets.
  49. for ($i = 0; $i < $charCount; $i++)
  50. {
  51. $decrypted .= chr($chars[$i] ^ ord($tmp[$i]));
  52. }
  53. return $decrypted;
  54. }
  55. /**
  56. * Method to encrypt a data string.
  57. *
  58. * @param string $data The data string to encrypt.
  59. * @param JCryptKey $key The key[/pair] object to use for encryption.
  60. *
  61. * @return string The encrypted data string.
  62. *
  63. * @since 12.1
  64. * @throws InvalidArgumentException
  65. */
  66. public function encrypt($data, JCryptKey $key)
  67. {
  68. // Validate key.
  69. if ($key->type != 'simple')
  70. {
  71. throw new InvalidArgumentException('Invalid key of type: ' . $key->type . '. Expected simple.');
  72. }
  73. // Initialise variables.
  74. $encrypted = '';
  75. $tmp = $key->private;
  76. // Split up the input into a character array and get the number of characters.
  77. $chars = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);
  78. $charCount = count($chars);
  79. // Repeat the key as many times as necessary to ensure that the key is at least as long as the input.
  80. for ($i = 0; $i < $charCount; $i = strlen($tmp))
  81. {
  82. $tmp = $tmp . $tmp;
  83. }
  84. // Get the XOR values between the ASCII values of the input and key characters for all input offsets.
  85. for ($i = 0; $i < $charCount; $i++)
  86. {
  87. $encrypted .= $this->_intToHex(ord($tmp[$i]) ^ ord($chars[$i]));
  88. }
  89. return $encrypted;
  90. }
  91. /**
  92. * Method to generate a new encryption key[/pair] object.
  93. *
  94. * @param array $options Key generation options.
  95. *
  96. * @return JCryptKey
  97. *
  98. * @since 12.1
  99. */
  100. public function generateKey(array $options = array())
  101. {
  102. // Create the new encryption key[/pair] object.
  103. $key = new JCryptKey('simple');
  104. // Just a random key of a given length.
  105. $key->private = $this->_getRandomKey();
  106. $key->public = $key->private;
  107. return $key;
  108. }
  109. /**
  110. * Method to generate a random key of a given length.
  111. *
  112. * @param integer $length The length of the key to generate.
  113. *
  114. * @return string
  115. *
  116. * @since 12.1
  117. */
  118. private function _getRandomKey($length = 256)
  119. {
  120. // Initialise variables.
  121. $key = '';
  122. $salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  123. $saltLength = strlen($salt);
  124. // Build the random key.
  125. for ($i = 0; $i < $length; $i++)
  126. {
  127. $key .= $salt[mt_rand(0, $saltLength - 1)];
  128. }
  129. return $key;
  130. }
  131. /**
  132. * Convert hex to an integer
  133. *
  134. * @param string $s The hex string to convert.
  135. * @param integer $i The offset?
  136. *
  137. * @return integer
  138. *
  139. * @since 11.1
  140. */
  141. private function _hexToInt($s, $i)
  142. {
  143. // Initialise variables.
  144. $j = (int) $i * 2;
  145. $k = 0;
  146. $s1 = (string) $s;
  147. // Get the character at position $j.
  148. $c = substr($s1, $j, 1);
  149. // Get the character at position $j + 1.
  150. $c1 = substr($s1, $j + 1, 1);
  151. switch ($c)
  152. {
  153. case 'A':
  154. $k += 160;
  155. break;
  156. case 'B':
  157. $k += 176;
  158. break;
  159. case 'C':
  160. $k += 192;
  161. break;
  162. case 'D':
  163. $k += 208;
  164. break;
  165. case 'E':
  166. $k += 224;
  167. break;
  168. case 'F':
  169. $k += 240;
  170. break;
  171. case ' ':
  172. $k += 0;
  173. break;
  174. default:
  175. (int) $k = $k + (16 * (int) $c);
  176. break;
  177. }
  178. switch ($c1)
  179. {
  180. case 'A':
  181. $k += 10;
  182. break;
  183. case 'B':
  184. $k += 11;
  185. break;
  186. case 'C':
  187. $k += 12;
  188. break;
  189. case 'D':
  190. $k += 13;
  191. break;
  192. case 'E':
  193. $k += 14;
  194. break;
  195. case 'F':
  196. $k += 15;
  197. break;
  198. case ' ':
  199. $k += 0;
  200. break;
  201. default:
  202. $k += (int) $c1;
  203. break;
  204. }
  205. return $k;
  206. }
  207. /**
  208. * Convert hex to an array of integers
  209. *
  210. * @param string $hex The hex string to convert to an integer array.
  211. *
  212. * @return array An array of integers.
  213. *
  214. * @since 11.1
  215. */
  216. private function _hexToIntArray($hex)
  217. {
  218. // Initialise variables.
  219. $array = array();
  220. $j = (int) strlen($hex) / 2;
  221. for ($i = 0; $i < $j; $i++)
  222. {
  223. $array[$i] = (int) $this->_hexToInt($hex, $i);
  224. }
  225. return $array;
  226. }
  227. /**
  228. * Convert an integer to a hexadecimal string.
  229. *
  230. * @param integer $i An integer value to convert to a hex string.
  231. *
  232. * @return string
  233. *
  234. * @since 11.1
  235. */
  236. private function _intToHex($i)
  237. {
  238. // Sanitize the input.
  239. $i = (int) $i;
  240. // Get the first character of the hexadecimal string if there is one.
  241. $j = (int) ($i / 16);
  242. if ($j === 0)
  243. {
  244. $s = ' ';
  245. }
  246. else
  247. {
  248. $s = strtoupper(dechex($j));
  249. }
  250. // Get the second character of the hexadecimal string.
  251. $k = $i - $j * 16;
  252. $s = $s . strtoupper(dechex($k));
  253. return $s;
  254. }
  255. }