Source for file simple.php
Documentation is available at simple.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* JCrypt cipher for Simple encryption, decryption and key generation.
* @package Joomla.Platform
* Method to decrypt a data string.
* @param string $data The encrypted string to decrypt.
* @param JCryptKey $key The key[/pair] object to use for decryption.
* @return string The decrypted data string.
* @throws InvalidArgumentException
public function decrypt($data, JCryptKey $key)
if ($key->type !=
'simple')
throw
new InvalidArgumentException('Invalid key of type: ' .
$key->type .
'. Expected simple.');
// Convert the HEX input into an array of integers and get the number of characters.
$chars =
$this->_hexToIntArray($data);
$charCount =
count($chars);
// Repeat the key as many times as necessary to ensure that the key is at least as long as the input.
for ($i =
0; $i <
$charCount; $i =
strlen($tmp))
// Get the XOR values between the ASCII values of the input and key characters for all input offsets.
for ($i =
0; $i <
$charCount; $i++
)
$decrypted .=
chr($chars[$i] ^
ord($tmp[$i]));
* Method to encrypt a data string.
* @param string $data The data string to encrypt.
* @param JCryptKey $key The key[/pair] object to use for encryption.
* @return string The encrypted data string.
* @throws InvalidArgumentException
public function encrypt($data, JCryptKey $key)
if ($key->type !=
'simple')
throw
new InvalidArgumentException('Invalid key of type: ' .
$key->type .
'. Expected simple.');
// Split up the input into a character array and get the number of characters.
$chars =
preg_split('//', $data, -
1, PREG_SPLIT_NO_EMPTY);
$charCount =
count($chars);
// Repeat the key as many times as necessary to ensure that the key is at least as long as the input.
for ($i =
0; $i <
$charCount; $i =
strlen($tmp))
// Get the XOR values between the ASCII values of the input and key characters for all input offsets.
for ($i =
0; $i <
$charCount; $i++
)
$encrypted .=
$this->_intToHex(ord($tmp[$i]) ^
ord($chars[$i]));
* Method to generate a new encryption key[/pair] object.
* @param array $options Key generation options.
// Create the new encryption key[/pair] object.
// Just a random key of a given length.
$key->private =
$this->_getRandomKey();
$key->public =
$key->private;
* Method to generate a random key of a given length.
* @param integer $length The length of the key to generate.
private function _getRandomKey($length =
256)
$salt =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for ($i =
0; $i <
$length; $i++
)
$key .=
$salt[mt_rand(0, $saltLength -
1)];
* Convert hex to an integer
* @param string $s The hex string to convert.
* @param integer $i The offset?
private function _hexToInt($s, $i)
// Get the character at position $j.
// Get the character at position $j + 1.
(int)
$k =
$k +
(16 * (int)
$c);
* Convert hex to an array of integers
* @param string $hex The hex string to convert to an integer array.
* @return array An array of integers.
private function _hexToIntArray($hex)
for ($i =
0; $i <
$j; $i++
)
$array[$i] = (int)
$this->_hexToInt($hex, $i);
* Convert an integer to a hexadecimal string.
* @param integer $i An integer value to convert to a hex string.
private function _intToHex($i)
// Get the first character of the hexadecimal string if there is one.
// Get the second character of the hexadecimal string.
Documentation generated on Tue, 19 Nov 2013 15:13:00 +0100 by phpDocumentor 1.4.3