Encryption Library — CodeIgniter 3.1.3 documentation (2024)

  • Docs »
  • Libraries »
  • Encryption Library
  • Encryption Library — CodeIgniter 3.1.3 documentation (1)

Important

DO NOT use this or any other encryption library foruser password storage! Passwords must be hashed instead, and youshould do that via PHP’s own Password Hashing extension.

The Encryption Library provides two-way data encryption. To do so ina cryptographically secure way, it utilizes PHP extensions that areunfortunately not always available on all systems.You must meet one of the following dependencies in order to use thislibrary:

If neither of the above dependencies is met, we simply cannot offeryou a good enough implementation to meet the high standards requiredfor proper cryptography.

  • Using the Encryption Library
    • Initializing the Class
    • Default behavior
    • Setting your encryption_key
    • Supported encryption ciphers and modes
      • Portable ciphers
      • Driver-specific ciphers
      • Encryption modes
    • Message Length
    • Configuring the library
    • Encrypting and decrypting data
      • How it works
      • Using custom parameters
      • Supported HMAC authentication algorithms
  • Class Reference

Using the Encryption Library

Initializing the Class

Like most other classes in CodeIgniter, the Encryption library isinitialized in your controller using the $this->load->library()method:

$this->load->library('encryption');

Once loaded, the Encryption library object will be available using:

$this->encryption

Default behavior

By default, the Encryption Library will use the AES-128 cipher in CBCmode, using your configured encryption_key and SHA512 HMAC authentication.

Note

AES-128 is chosen both because it is proven to be strong andbecause of its wide availability across different cryptographicsoftware and programming languages’ APIs.

However, the encryption_key is not used as is.

If you are somewhat familiar with cryptography, you should already knowthat a HMAC also requires a secret key and using the same key for bothencryption and authentication is a bad practice.

Because of that, two separate keys are derived from your already configuredencryption_key: one for encryption and one for authentication. This isdone via a technique called HMAC-based Key Derivation Function (HKDF).

Setting your encryption_key

An encryption key is a piece of information that controls thecryptographic process and permits a plain-text string to be encrypted,and afterwards - decrypted. It is the secret “ingredient” in the wholeprocess that allows you to be the only one who is able to decrypt datathat you’ve decided to hide from the eyes of the public.After one key is used to encrypt data, that same key provides the onlymeans to decrypt it, so not only must you chose one carefully, but youmust not lose it or you will also lose access to the data.

It must be noted that to ensure maximum security, such key should notonly be as strong as possible, but also often changed. Such behaviorhowever is rarely practical or possible to implement, and that is whyCodeIgniter gives you the ability to configure a single key that is to beused (almost) every time.

It goes without saying that you should guard your key carefully. Shouldsomeone gain access to your key, the data will be easily decrypted. Ifyour server is not totally under your control it’s impossible to ensurekey security so you may want to think carefully before using it foranything that requires high security, like storing credit card numbers.

Your encryption key must be as long as the encyption algorithm in useallows. For AES-128, that’s 128 bits or 16 bytes (charcters) long.You will find a table below that shows the supported key lengths ofdifferent ciphers.

The key should be as random as possible and it must not be a regulartext string, nor the output of a hashing function, etc. In order to createa proper key, you must use the Encryption library’s create_key() method

// $key will be assigned a 16-byte (128-bit) random key$key = $this->encryption->create_key(16);

The key can be either stored in your application/config/config.php, oryou can design your own storage mechanism and pass the key dynamicallywhen encrypting/decrypting.

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

$config['encryption_key'] = 'YOUR KEY';

You’ll notice that the create_key() method outputs binary data, whichis hard to deal with (i.e. a copy-paste may damage it), so you may usebin2hex(), hex2bin() or Base64-encoding to work with the key ina more friendly manner. For example:

// Get a hex-encoded representation of the key:$key = bin2hex($this->encryption->create_key(16));// Put the same value in your config with hex2bin(),// so that it is still passed as binary to the library:$config['encryption_key'] = hex2bin(<your hex-encoded key>);

Supported encryption ciphers and modes

Note

The terms ‘cipher’ and ‘encryption algorithm’ are interchangeable.

Portable ciphers

Because MCrypt and OpenSSL (also called drivers throughout this document)each support different sets of encryption algorithms and often implementthem in different ways, our Encryption library is designed to use them ina portable fashion, or in other words - it enables you to use theminterchangeably, at least for the ciphers supported by both drivers.

It is also implemented in a way that aims to match the standardimplementations in other programming languages and libraries.

Here’s a list of the so called “portable” ciphers, where“CodeIgniter name” is the string value that you’d have to pass to theEncryption library to use that cipher:

Cipher nameCodeIgniter nameKey lengths (bits / bytes)Supported modes
AES-128 / Rijndael-128aes-128128 / 16CBC, CTR, CFB, CFB8, OFB, ECB
AES-192aes-192192 / 24CBC, CTR, CFB, CFB8, OFB, ECB
AES-256aes-256256 / 32CBC, CTR, CFB, CFB8, OFB, ECB
DESdes56 / 7CBC, CFB, CFB8, OFB, ECB
TripleDEStripledes56 / 7, 112 / 14, 168 / 21CBC, CFB, CFB8, OFB
Blowfishblowfish128-448 / 16-56CBC, CFB, OFB, ECB
CAST5 / CAST-128cast588-128 / 11-16CBC, CFB, OFB, ECB
RC4 / ARCFourrc440-2048 / 5-256Stream

Important

Because of how MCrypt works, if you fail to provide a keywith the appropriate length, you might end up using a differentalgorithm than the one configured, so be really careful with that!

Note

In case it isn’t clear from the above table, Blowfish, CAST5and RC4 support variable length keys. That is, any number in theshown ranges is valid, although in bit terms that only happensin 8-bit increments.

Note

Even though CAST5 supports key lengths lower than 128 bits(16 bytes), in fact they will just be zero-padded to themaximum length, as specified in RFC 2144.

Note

Blowfish supports key lengths as small as 32 bits (4 bytes), butour tests have shown that only lengths of 128 bits (16 bytes) orhigher are properly supported by both MCrypt and OpenSSL. It isalso a bad practice to use such low-length keys anyway.

Driver-specific ciphers

As noted above, MCrypt and OpenSSL support different sets of encryptionciphers. For portability reasons and because we haven’t tested themproperly, we do not advise you to use the ones that are driver-specific,but regardless, here’s a list of most of them:

Cipher nameDriverKey lengths (bits / bytes)Supported modes
AES-128OpenSSL128 / 16CBC, CTR, CFB, CFB8, OFB, ECB, XTS
AES-192OpenSSL192 / 24CBC, CTR, CFB, CFB8, OFB, ECB, XTS
AES-256OpenSSL256 / 32CBC, CTR, CFB, CFB8, OFB, ECB, XTS
Rijndael-128MCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
Rijndael-192MCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
Rijndael-256MCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
GOSTMCrypt256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
TwofishMCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
CAST-128MCrypt40-128 / 5-16CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
CAST-256MCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
Loki97MCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
SaferPlusMCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
SerpentMCrypt128 / 16, 192 / 24, 256 / 32CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
XTEAMCrypt128 / 16CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
RC2MCrypt8-1024 / 1-128CBC, CTR, CFB, CFB8, OFB, OFB8, ECB
RC2OpenSSL8-1024 / 1-128CBC, CFB, OFB, ECB
Camellia-128OpenSSL128 / 16CBC, CFB, CFB8, OFB, ECB
Camellia-192OpenSSL192 / 24CBC, CFB, CFB8, OFB, ECB
Camellia-256OpenSSL256 / 32CBC, CFB, CFB8, OFB, ECB
SeedOpenSSL128 / 16CBC, CFB, OFB, ECB

Note

If you wish to use one of those ciphers, you’d have to passits name in lower-case to the Encryption library.

Note

You’ve probably noticed that all AES cipers (and Rijndael-128)are also listed in the portable ciphers list. This is becausedrivers support different modes for these ciphers. Also, it isimportant to note that AES-128 and Rijndael-128 are actuallythe same cipher, but only when used with a 128-bit key.

Note

CAST-128 / CAST-5 is also listed in both the portable anddriver-specific ciphers list. This is because OpenSSL’simplementation doesn’t appear to be working correctly withkey sizes of 80 bits and lower.

Note

RC2 is listed as supported by both MCrypt and OpenSSL.However, both drivers implement them differently and theyare not portable. It is probably worth noting that we onlyfound one obscure source confirming that it is MCrypt thatis not properly implementing it.

Encryption modes

Different modes of encryption have different characteristics and servefor different purposes. Some are stronger than others, some are fasterand some offer extra features.We are not going in depth into that here, we’ll leave that to thecryptography experts. The table below is to provide brief informationalreference to our more experienced users. If you are a beginner, juststick to the CBC mode - it is widely accepted as strong and secure forgeneral purposes.

Mode nameCodeIgniter nameDriver supportAdditional info
CBCcbcMCrypt, OpenSSLA safe default choice
CTRctrMCrypt, OpenSSLConsidered as theoretically better than CBC, but not as widely available
CFBcfbMCrypt, OpenSSLN/A
CFB8cfb8MCrypt, OpenSSLSame as CFB, but operates in 8-bit mode (not recommended).
OFBofbMCrypt, OpenSSLN/A
OFB8ofb8MCryptSame as OFB, but operates in 8-bit mode (not recommended).
ECBecbMCrypt, OpenSSLIgnores IV (not recommended).
XTSxtsOpenSSLUsually used for encrypting random access data such as RAM or hard-disk storage.
StreamstreamMCrypt, OpenSSLThis is not actually a mode, it just says that a stream cipher is being used. Required because of the general cipher+mode initialization process.

Message Length

It’s probably important for you to know that an encrypted string is usuallylonger than the original, plain-text string (depending on the cipher).

This is influenced by the cipher algorithm itself, the IV prepended to thecipher-text and the HMAC authentication message that is also prepended.Furthermore, the encrypted message is also Base64-encoded so that it is safefor storage and transmission, regardless of a possible character set in use.

Keep this information in mind when selecting your data storage mechanism.Cookies, for example, can only hold 4K of information.

Configuring the library

For usability, performance, but also historical reasons tied to our oldEncrypt Class, the Encryption library is designed touse repeatedly the same driver, encryption cipher, mode and key.

As noted in the “Default behavior” section above, this means using anauto-detected driver (OpenSSL has a higher priority), the AES-128 ciperin CBC mode, and your $config['encryption_key'] value.

If you wish to change that however, you need to use the initialize()method. It accepts an associative array of parameters, all of which areoptional:

OptionPossible values
driver‘mcrypt’, ‘openssl’
cipherCipher name (see Supported encryption ciphers and modes)
modeEncryption mode (see Encryption modes)
keyEncryption key

For example, if you were to change the encryption algorithm andmode to AES-256 in CTR mode, this is what you should do:

$this->encryption->initialize( array( 'cipher' => 'aes-256', 'mode' => 'ctr', 'key' => '<a 32-character random string>' ));

Note that we only mentioned that you want to change the ciper and mode,but we also included a key in the example. As previously noted, it isimportant that you choose a key with a proper size for the used algorithm.

There’s also the ability to change the driver, if for some reason youhave both, but want to use MCrypt instead of OpenSSL:

// Switch to the MCrypt driver$this->encryption->initialize(array('driver' => 'mcrypt'));// Switch back to the OpenSSL driver$this->encryption->initialize(array('driver' => 'openssl'));

Encrypting and decrypting data

Encrypting and decrypting data with the already configured librarysettings is simple. As simple as just passing the string to theencrypt() 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!echo $this->encryption->decrypt($ciphertext);

And that’s it! The Encryption library will do everything necessaryfor the whole process to be cryptographically secure out-of-the-box.You don’t need to worry about it.

Important

Both methods will return FALSE in case of an error.While for encrypt() this can only mean incorrectconfiguration, you should always check the return valueof decrypt() in production code.

How it works

If you must know how the process works, here’s what happens underthe hood:

  • $this->encryption->encrypt($plain_text)
    1. Derive an encryption key and a HMAC key from your configuredencryption_key via HKDF, using the SHA-512 digest algorithm.
    2. Generate a random initialization vector (IV).
    3. Encrypt the data via AES-128 in CBC mode (or another previouslyconfigured cipher and mode), using the above-mentioned derivedencryption key and IV.
    4. Prepend said IV to the resulting cipher-text.
    5. Base64-encode the resulting string, so that it can be safelystored or transferred without worrying about character sets.
    6. Create a SHA-512 HMAC authentication message using the derivedHMAC key to ensure data integrity and prepend it to the Base64string.
  • $this->encryption->decrypt($ciphertext)
    1. Derive an encryption key and a HMAC key from your configuredencryption_key via HKDF, using the SHA-512 digest algorithm.Because your configured encryption_key is the same, thiswill produce the same result as in the encrypt() methodabove - otherwise you won’t be able to decrypt it.
    2. Check if the string is long enough, separate the HMAC out ofit and validate if it is correct (this is done in a way thatprevents timing attacks agains it). Return FALSE if either ofthe checks fails.
    3. Base64-decode the string.
    4. Separate the IV out of the cipher-text and decrypt the saidcipher-text using that IV and the derived encryption key.

Using custom parameters

Let’s say you have to interact with another system that is outof your control and uses another method to encrypt data. Amethod that will most certainly not match the above-describedsequence and probably not use all of the steps either.

The Encryption library allows you to change how its encryptionand decryption processes work, so that you can easily tailor acustom solution for such situations.

Note

It is possible to use the library in this way, withoutsetting an encryption_key in your configuration file.

All you have to do is to pass an associative array with a fewparameters to either the encrypt() or decrypt() method.Here’s an example:

// Assume that we have $ciphertext, $key and $hmac_key// from on outside source$message = $this->encryption->decrypt( $ciphertext, array( 'cipher' => 'blowfish', 'mode' => 'cbc', 'key' => $key, 'hmac_digest' => 'sha256', 'hmac_key' => $hmac_key ));

In the above example, we are decrypting a message that was encryptedusing the Blowfish cipher in CBC mode and authenticated via a SHA-256HMAC.

Important

Note that both ‘key’ and ‘hmac_key’ are used in thisexample. When using custom parameters, encryption and HMAC keysare not derived like the default behavior of the library is.

Below is a list of the available options.

However, unless you really need to and you know what you are doing,we advise you to not change the encryption process as this couldimpact security, so please do so with caution.

OptionDefault valueMandatory / OptionalDescription
cipherN/AYesEncryption algorithm (see Supported encryption ciphers and modes).
modeN/AYesEncryption mode (see Encryption modes).
keyN/AYesEncryption key.
hmacTRUENoWhether to use a HMAC.Boolean. If set to FALSE, then hmac_digest andhmac_key will be ignored.
hmac_digestsha512NoHMAC message digest algorithm (see Supported HMAC authentication algorithms).
hmac_keyN/AYes, unless hmac is FALSEHMAC key.
raw_dataFALSENoWhether the cipher-text should be raw.Boolean. If set to TRUE, then Base64 encoding anddecoding will not be performed and HMAC will notbe a hexadecimal string.

Important

encrypt() and decrypt() will return FALSE ifa mandatory parameter is not provided or if a providedvalue is incorrect. This includes hmac_key, unless hmacis set to FALSE.

Supported HMAC authentication algorithms

For HMAC message authentication, the Encryption library supportsusage of the SHA-2 family of algorithms:

AlgorithmRaw length (bytes)Hex-encoded length (bytes)
sha51264128
sha3844896
sha2563264
sha2242856

The reason for not including other popular algorithms, such asMD5 or SHA1 is that they are no longer considered secure enoughand as such, we don’t want to encourage their usage.If you absolutely need to use them, it is easy to do so via PHP’snative hash_hmac() function.

Stronger algorithms of course will be added in the future as theyappear and become widely available.

Class Reference

class CI_Encryption
initialize($params)
Parameters:
  • $params (array) – Configuration parameters
Returns:

CI_Encryption instance (method chaining)

Return type:

CI_Encryption

Initializes (configures) the library to use a differentdriver, cipher, mode or key.

Example:

$this->encryption->initialize( array('mode' => 'ctr'));

Please refer to the Configuring the library section for detailed info.

encrypt($data[, $params = NULL])
Parameters:
  • $data (string) – Data to encrypt
  • $params (array) – Optional parameters
Returns:

Encrypted data or FALSE on failure

Return type:

string

Encrypts the input data and returns its ciphertext.

Example:

$ciphertext = $this->encryption->encrypt('My secret message');

Please refer to the Using custom parameters section for informationon the optional parameters.

decrypt($data[, $params = NULL])
Parameters:
  • $data (string) – Data to decrypt
  • $params (array) – Optional parameters
Returns:

Decrypted data or FALSE on failure

Return type:

string

Decrypts the input data and returns it in plain-text.

Example:

echo $this->encryption->decrypt($ciphertext);

Please refer to the Using custom parameters secrion for informationon the optional parameters.

create_key($length)
Parameters:
  • $length (int) – Output length
Returns:

A pseudo-random cryptographic key with the specified length, or FALSE on failure

Return type:

string

Creates a cryptographic key by fetching random data fromthe operating system’s sources (i.e. /dev/urandom).

hkdf($key[, $digest = 'sha512'[, $salt = NULL[, $length = NULL[, $info = '']]]])
Parameters:
  • $key (string) – Input key material
  • $digest (string) – A SHA-2 family digest algorithm
  • $salt (string) – Optional salt
  • $length (int) – Optional output length
  • $info (string) – Optional context/application-specific info
Returns:

A pseudo-random key or FALSE on failure

Return type:

string

Derives a key from another, presumably weaker key.

This method is used internally to derive an encryption and HMAC keyfrom your configured encryption_key.

It is publicly available due to its otherwise general purpose. It isdescribed in RFC 5869.

However, as opposed to the description in RFC 5869, this implementationdoesn’t support SHA1.

Example:

$hmac_key = $this->encryption->hkdf( $key, 'sha512', NULL, NULL, 'authentication');// $hmac_key is a pseudo-random key with a length of 64 bytes
Encryption Library — CodeIgniter 3.1.3 documentation (2024)
Top Articles
Package Stolen? Here are 5 Ways to Recover Stolen Deliveries
The Investor's Guide to Navigating Impermanent Loss
Pikes Suwanee
Progressive Care Rn A V1 Relias
Netgear Outage
351 Windsor For Sale Craigslist
Flanagan-Watts Funeral Home Obituaries
Beacon Schneider Gibson County
Medici Vermittlung GmbH sucht Facharzt (m/w/d) | Gynäkologie und Geburtshilfe (8662) in Cottbus | LinkedIn
Was ist die F74 Challenge? Bedeutung, Definition, Erklärung - Bedeutung Online
Meg 2: The Trench Showtimes Near Phoenix Theatres Laurel Park
Oracle Ttec Direct Deposit
Ds Cuts Saugus
Seo Glossary definition page
Panter Belico Net Worth
Dogs Craiglist
Is Bekah Birdsall Married
Rubber Expansion Joints for Pump and Piping Systems - Empowering Pumps and Equipment
Green Light Auto Sales Dallas Photos
Cars Under $1000 On Craigslist
Cb2 South Coast Plaza
1 P.m. Pdt
Strange World Showtimes Near Harkins Theatres Christown 14
Novant Mychart Nhrmc
Unlv 2024 Schedule
Ephesians 6 New Living Translation
Oels Prism Login
Blairsville Online Yard Sale
1800 Water Damage Princess Anne Va
Manhungay
Licorice Pizza 123Movies
Apolonia's Prime Steakhouse Okeechobee Fl
Weil Cornell Connect
Dunkin Donuts Corporate Jobs
25X11X10 Atv Tires Tractor Supply
Vfr Town Of Salem
Max80 List
Mamasan Massage
These Bathroom Cabinet Ideas Make Storage Look Beautiful
Fanfix Leaked
Makedonska Kursna Lista
Guadalajara Taqueria Cisco Menu
Hotels Near 9300 Sw 72Nd St Miami Fl 33173
R/Sandiego
Gluten Ease Walgreens
Mohave County Craiglist
Houston Max80
Craigslist Pets Pueblo
Joplin.craigslist
Craigslist Metal Roofing
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 5512

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.