print utilityEncrypt

Provides a simple wrapper around mcrypt functions to perform encryption and decryption duties. This class is designed to use the Rijndael 256bit encryption scheme. To use other schemes, extend and replace the various methods.

The key that is used should be kept protected at all times.

$oEnc = new utilityEncrypt();
$oEnc->setKey($key);
$encrypted = $oEnc->encrypt('Some data to encrypt');

// use in URI
echo $oEnc->toUriString($encrypted);

// restoring from URI data
$data = utilityEncrypt::factory($key)
            ->decrypt(utilityEncrypt::fromUriString($uriData));

Methods

public __construct()

Creates a new Encrypt object

public static factory($inKey)

Creates a new encryption utility using $inKey

public static createKeyString([$inLength = 32], [$inMinAscii = 32], [$inMaxAscii = 126], [$inIgnore = array()])

Creates a random string of $inLength

The string is created from the ASCII range specified by $inMinAscii and $inMaxAscii. These are both decimal integers e.g. 30 or 155 that correspond to the portion of the ASCII range to be used when generating a key. The default is to use characters between ASCII 32 (a space) and ASCII 126 (~). The range can be increased but may cause issues with displays that cannot handle particular character sequences.

If the length is not specified the max key length value will be used instead.

An optional array of characters can be specified that should NOT be used in the generated key string. This allows a large ASCII range to be used but to remove problematic characters such as back-ticks, quotes, braces etc.

public static toUriString($inString)

Creates a URI safe string from the data

public static fromUriString($inString)

Returns the encoded data from the URI safe string

public encrypt($inData)

Encrypt data $inData using the current key

The Rijndael 256bit scheme is used to encrypt the data. This method returns the encrypted data as is. To use in a URI context be sure to URI safe encode the data via utilityEncrypt::toUriString().

public decrypt($inData)

Decrypts data $inData using the current key

The Rijndael 256bit scheme is used to decrypt the data. This method returns the decrypted data. If decrypting a URI encoded string, be sure to first decode it via utilityEncrypt::fromUriString()

public getKey()

Returns the current encryption key

public setKey($inKey)

Sets the encryption key to use

If the key is longer than {@link utilityEncrypt::MAX_KEY_LENGTH} it will be trimmed to that number of characters.

<  1  >