PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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