InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Encrypting and Decrypting Data in PHP
PHP

Encrypting and Decrypting Data in PHP

InfinityCoder December 21, 2016

You want to encrypt and decrypt data using one of a variety of popular algorithms.

Use PHP’s mcrypt extension:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$algorithm = MCRYPT_BLOWFISH;
$key = 'That golden key that opens the palace of eternity.';
$data = 'The chicken escapes at dawn. Send help with Mr. Blue.';
$mode = MCRYPT_MODE_CBC;
 
$iv = mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode),
                       MCRYPT_DEV_URANDOM);
 
$encrypted_data = mcrypt_encrypt($algorithm, $key, $data, $mode, $iv);
$plain_text = base64_encode($encrypted_data);
echo $plain_text . "\n";
 
$encrypted_data = base64_decode($plain_text);
$decoded = mcrypt_decrypt($algorithm, $key, $encrypted_data, $mode, $iv);
// trim() will remove any trailing NULL bytes that mcrypt_decrypt() may
// have added to pad the output to be a whole number of 8-byte blocks
echo trim($decoded) . "\n";

This prints:

1
2
Cd4Uzc1c5lDxxWc7rXv+mbsElwj2ENrYg5HAPiaOpe7Wr8UAG5aXD9CoG6NdKoOWLSumg9ffSnE=
The chicken escapes at dawn. Send help with Mr. Blue.

The mcrypt extension is an interface with mcrypt, a library that implements many different encryption algorithms.

The data is encrypted and decrypted by mcrypt_encrypt() and mcrypt_decrypt(), respectively. They each take five arguments.

The first is the algorithm to use. To find which algorithms mcrypt supports on your system, call mcrypt_list_algorithms().

The second argument is the encryption key; the third argument is the data to encrypt or decrypt. The fourth argument is the mode for the encryption or decryption (a list of supported modes is returned by mcrypt_list_modes()).

The fifth argument is an initialization vector (IV), used by some modes as part of the encryption or decryption process.
Except for the data to encrypt or decrypt, all the other arguments must be the same when encrypting and decrypting.

If you’re using a mode that requires an initialization vector, it’s OK to pass the initialization vector in the clear with the encrypted text.
The different modes are appropriate in different circumstances. Cipher Block Chaining (CBC) mode encrypts the data in blocks, and uses the encrypted value of each block (as well as the key) to compute the encrypted value of the next block.

The initialization vector affects the encrypted value of the first block. Cipher Feedback (CFB) and Output Feedback (OFB) also use an initialization vector, but they encrypt data in units smaller than the block size.

Note that OFB mode has security problems if you encrypt data in smaller units than its block size. Electronic Code Book (ECB) mode encrypts data in discrete blocks that don’t depend on each other.

ECB mode doesn’t use an initialization vector. It is also less secure than other modes for repeated use, because the same plain text with a given key always produces the same cipher text.

Different algorithms have different block sizes. You can retrieve the block size for a particular algorithm with mcrypt_get_block_size().

Similarly, the initialization vector size is determined by the algorithm and the mode. mcrypt_create_iv() and
mcrypt_get_iv_size() make it easy to create an appropriate random initialization vector:

1
2
$iv = mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode),
                       MCRYPT_DEV_URANDOM);

The first argument to mcrypt_create_iv() is the size of the vector, and the second is a source of randomness.

You have three choices for the source of randomness: MCRYPT_DEV_RANDOM reads from the pseudodevice /dev/random, MCRYPT_DEV_URANDOMreads from the pseudodevice /dev/urandom, and MCRYPT_RAND uses an internal random number generator.

Not all operating systems support random-generating pseudodevices.
Make sure to call srand() before using MCRYPT_RAND in order to get a nonrepeating random number stream.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Localizing Numbers in PHP
You want to display numbers in a locale-specific format. Use …

Localizing Numbers in PHP

Encrypting Email with GPG in PHP
You want to send encrypted email messages. For example, you …

Encrypting Email with GPG in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap