CI_Encrypt

class CI_Encrypt

encode($string[, $key = ''])
Parameters:
  • $string (string) – Data to encrypt
  • $key (string) – Encryption key
Returns:

Encrypted string

Return type:

string

Performs the data encryption and returns it as a string. Example:

$msg = 'My secret message';

$encrypted_string = $this->encrypt->encode($msg);

You can optionally pass your encryption key via the second parameter if you don’t want to use the one in your config file:

$msg = 'My secret message';
$key = 'super-secret-key';

$encrypted_string = $this->encrypt->encode($msg, $key);
decode($string[, $key = ''])
Parameters:
  • $string (string) – String to decrypt
  • $key (string) – Encryption key
Returns:

Plain-text string

Return type:

string

Decrypts an encoded string. Example:

$encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';

$plaintext_string = $this->encrypt->decode($encrypted_string);

You can optionally pass your encryption key via the second parameter if you don’t want to use the one in your config file:

$msg = 'My secret message';
$key = 'super-secret-key';

$encrypted_string = $this->encrypt->decode($msg, $key);
set_cipher($cipher)
Parameters:
  • $cipher (int) – Valid PHP MCrypt cypher constant
Returns:

CI_Encrypt instance (method chaining)

Return type:

CI_Encrypt

Permits you to set an Mcrypt cipher. By default it uses MCRYPT_RIJNDAEL_256. Example:

$this->encrypt->set_cipher(MCRYPT_BLOWFISH);

Please visit php.net for a list of available ciphers.

If you’d like to manually test whether your server supports MCrypt you can use:

echo extension_loaded('mcrypt') ? 'Yup' : 'Nope';
set_mode($mode)
Parameters:
  • $mode (int) – Valid PHP MCrypt mode constant
Returns:

CI_Encrypt instance (method chaining)

Return type:

CI_Encrypt

Permits you to set an Mcrypt mode. By default it uses MCRYPT_MODE_CBC. Example:

$this->encrypt->set_mode(MCRYPT_MODE_CFB);

Please visit php.net for a list of available modes.

encode_from_legacy($string[, $legacy_mode = MCRYPT_MODE_ECB[, $key = '']])
Parameters:
  • $string (string) – String to encrypt
  • $legacy_mode (int) – Valid PHP MCrypt cipher constant
  • $key (string) – Encryption key
Returns:

Newly encrypted string

Return type:

string

Enables you to re-encode data that was originally encrypted with CodeIgniter 1.x to be compatible with the Encrypt library in CodeIgniter 2.x. It is only necessary to use this method if you have encrypted data stored permanently such as in a file or database and are on a server that supports Mcrypt. “Light” use encryption such as encrypted session data or transitory encrypted flashdata require no intervention on your part. However, existing encrypted Sessions will be destroyed since data encrypted prior to 2.x will not be decoded.

Important

Why only a method to re-encode the data instead of maintaining legacy methods for both encoding and decoding? The algorithms in the Encrypt library have improved in CodeIgniter 2.x both for performance and security, and we do not wish to encourage continued use of the older methods. You can of course extend the Encryption library if you wish and replace the new methods with the old and retain seamless compatibility with CodeIgniter 1.x encrypted data, but this a decision that a developer should make cautiously and deliberately, if at all.

$new_data = $this->encrypt->encode_from_legacy($old_encrypted_string);
Parameter Default Description
$orig_data n/a The original encrypted data from CodeIgniter 1.x’s Encryption library
$legacy_mode MCRYPT_MODE_ECB The Mcrypt mode that was used to generate the original encrypted data. CodeIgniter 1.x’s default was MCRYPT_MODE_ECB, and it will assume that to be the case unless overridden by this parameter.
$key n/a The encryption key. This it typically specified in your config file as outlined above.
doc_CodeIgniter
2016-10-15 16:31:30
Comments
Leave a Comment

Please login to continue.