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