PageRenderTime 92ms CodeModel.GetById 79ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/libraries/joomla/utilities/simplecrypt.php

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