Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encryption key in CodeIgniter

Tags:

codeigniter

The CodeIgniter 2.0.2 requires to set an encryption key in the config file i.e. $config['encryption_key'] , if you want to use Session class. Can it be any string? Any example of secure encryption_key?

Thanks.

like image 859
Roman Avatar asked May 30 '11 08:05

Roman


People also ask

How to use encryption in CodeIgniter?

Encrypting and decrypting data with the already configured library settings is simple. As simple as just passing the string to the encrypt() and/or decrypt() methods: $plain_text = 'This is a plain-text message! '; $ciphertext = $this->encryption->encrypt($plain_text); // Outputs: This is a plain-text message!

How to encrypt and decrypt data in CodeIgniter?

This key should be any random string but not a simple plain text and should be 32 characters in length (128 bits). Below we have created an example to show, how to encode and decode the data. To decode the code use: $this->encrypt->decode() // Decrypts an encoded string.

What is encryption technique?

Encryption is the method by which information is converted into secret code that hides the information's true meaning. The science of encrypting and decrypting information is called cryptography. In computing, unencrypted data is also known as plaintext, and encrypted data is called ciphertext.


2 Answers

The key should be as random as possible and it must not be a regular text string, nor the output of a hashing function, etc.

To save your key to your application/config/config.php, open the file and set:

$config['encryption_key'] = 'yourKeyHere'

Random Key Generator

It's important for you to know that the encoded messages the encryption function generates will be approximately 2.6 times longer than the original message. For example, if you encrypt the string "my super secret data", which is 21 characters in length, you'll end up with an encoded string that is roughly 55 characters (we say "roughly" because the encoded string length increments in 64 bit clusters, so it's not exactly linear). Keep this information in mind when selecting your data storage mechanism. Cookies, for example, can only hold 4K of information.

like image 55
Otto Avatar answered Sep 20 '22 17:09

Otto


In addition to the answer by Chumillas, I personally use this Random Key Generator for my CodeIgniter encryption strings. Quick and easy.

like image 41
Fuseblown Avatar answered Sep 24 '22 17:09

Fuseblown