PageRenderTime 53ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

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

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