joomla25 /libraries/joomla/utilities/simplecrypt.php

Language PHP Lines 300
MD5 Hash 82e0abd8c9673ca3a9829863fa25dd0b Estimated Cost $4,048 (why?)
Repository https://bitbucket.org/asosso/joomla25.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
 * @package     Joomla.Platform
 * @subpackage  Utilities
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

/**
 * JSimpleCrypt is a very simple encryption algorithm for encrypting/decrypting strings
 *
 * @package     Joomla.Platform
 * @subpackage  Utilities
 * @since       11.1
 * @deprecated  12.3  Use JCrypt instead.
 */
class JSimpleCrypt extends JObject
{
	/**
	 * Encryption/Decryption Key
	 *
	 * @var    string
	 */
	protected $_key;

	/**
	 * Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the
	 * secret word from the configuration object is used.
	 *
	 * @param   string  $key  Optional encryption key
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	public function __construct($key = null)
	{
		JLog::add('JSimpleCrypt is deprecated. Use JCrypt instead.', JLog::WARNING, 'deprecated');

		if ($key)
		{
			$this->_key = (string) $key;
		}
		else
		{
			$conf = &JFactory::getConfig();
			$this->_key = md5($conf->get('secret'));
		}
	}

	/**
	 * Decrypt a string
	 *
	 * @param   string  $s  String to decrypt
	 *
	 * @return  string
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	public function decrypt($s)
	{
		$ai = $this->_hexToIntArray($s);
		(string) $s1 = $this->_xorString($ai);

		return $s1;
	}

	/**
	 * Encrypt a string
	 *
	 * @param   string  $s  String to encrypt
	 *
	 * @return  string
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	public function encrypt($s)
	{
		$ai = $this->_xorCharString($s);
		$s1 = '';

		for ($i = 0, $count = count($ai); $i < $count; $i++)
		{
			$s1 = $s1 . $this->_intToHex((int) $ai[$i]);
		}

		return $s1;
	}

	/**
	 * Convert hex to an integer
	 *
	 * @param   string   $s  The hex string to convert.
	 * @param   integer  $i  The offset?
	 *
	 * @return  integer
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	protected function _hexToInt($s, $i)
	{
		(int) $j = $i * 2;
		(string) $s1 = $s;
		(string) $c = substr($s1, $j, 1); // get the char at position $j, length 1
		(string) $c1 = substr($s1, $j + 1, 1); // get the char at postion $j + 1, length 1
		(int) $k = 0;

		switch ($c)
		{
			case "A":
				$k += 160;
				break;
			case "B":
				$k += 176;
				break;
			case "C":
				$k += 192;
				break;
			case "D":
				$k += 208;
				break;
			case "E":
				$k += 224;
				break;
			case "F":
				$k += 240;
				break;
			case " ":
				$k += 0;
				break;
			default:
				(int) $k = $k + (16 * (int) $c);
				break;
		}

		switch ($c1)
		{
			case "A":
				$k += 10;
				break;
			case "B":
				$k += 11;
				break;
			case "C":
				$k += 12;
				break;
			case "D":
				$k += 13;
				break;
			case "E":
				$k += 14;
				break;
			case "F":
				$k += 15;
				break;
			case " ":
				$k += 0;
				break;
			default:
				$k += (int) $c1;
				break;
		}

		return $k;
	}
	/**
	 * Convert hex to an array of integers
	 *
	 * @param   string  $s  The hex string to convert to an integer array.
	 *
	 * @return  array  An array of integers.
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	protected function _hexToIntArray($s)
	{
		(string) $s1 = $s;
		(int) $i = strlen($s1);
		(int) $j = $i / 2;
		for ($l = 0; $l < $j; $l++)
		{
			(int) $k = $this->_hexToInt($s1, $l);
			$ai[$l] = $k;
		}

		return $ai;
	}

	/**
	 * Convert character string to integer
	 *
	 * @param   string  $c  The character to convert to an integer.
	 *
	 * @return  integer
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	protected function _charToInt($c)
	{
		$ac[0] = $c;

		return $ac;
	}

	/**
	 * XorString
	 *
	 * @param   string  $ai  The string.
	 *
	 * @return  string
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	protected function _xorString($ai)
	{
		$s = $this->_key;
		(int) $i = strlen($s);
		$ai1 = $ai;
		(int) $j = count($ai1);
		for ($i = 0; $i < $j; $i = strlen($s))
		{
			$s = $s . $s;
		}

		for ($k = 0; $k < $j; $k++)
		{
			(string) $c = substr($s, $k, 1);
			$ac[$k] = chr($ai1[$k] ^ ord($c));
		}

		(string) $s1 = implode('', $ac);
		return $s1;
	}

	/**
	 * Convert integer to hex
	 *
	 * @param   integer  $i  An integer value to convert.
	 *
	 * @return  string
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	protected function _intToHex($i)
	{
		(int) $j = (int) $i / 16;
		if ((int) $j == 0)
		{
			(string) $s = " ";
		}
		else
		{
			(string) $s = strtoupper(dechex($j));
		}
		(int) $k = (int) $i - (int) $j * 16;
		(string) $s = $s . strtoupper(dechex($k));

		return $s;
	}

	/**
	 * Use xor encryption
	 *
	 * @param   string  $s  The string.
	 *
	 * @return  array  An array of integers
	 *
	 * @since   11.1
	 * @deprecated  12.3  Use JCrypt instead.
	 */
	protected function _xorCharString($s)
	{
		$ac = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
		(string) $s1 = $this->_key;
		(int) $i = strlen($s1);
		(int) $j = count($ac);
		for ($i = 0; $i < $j; $i = strlen($s1))
		{
			$s1 = $s1 . $s1;
		}

		for ($k = 0; $k < $j; $k++)
		{
			$c = substr($s1, $k, 1);
			$ai[$k] = ord($c) ^ ord($ac[$k]);
		}

		return $ai;
	}
}
Back to Top