DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. PHP's mcrypt

PHP's mcrypt

Giorgio Sironi user avatar by
Giorgio Sironi
·
Feb. 25, 13 · Interview
Like (1)
Save
Tweet
Share
5.96K Views

Join the DZone community and get the full member experience.

Join For Free

This a symmetric encryption primer for PHP: you'll learn how to call the mcrypt API to encrypt and decrypt strings, using a single key in both processes.

The theory

In symmetric cryptography, the key for encrypting and decrypting data is the same and is shared between parties while being kept secrets. The primitives take as configuration the key and other parameters, and transforms a value into its encrypted version and back.

Once someone gets hold of an encrypted version and assuming the scheme and your implementation do not contain flaws, it is necessary to know the key to get to the plain text version.

There are many different algorithms in cryptography, but we will take a look at block ciphers only; this category of ciphers is able to encrypt a block of a small, fixed size at a time, but can be used in different operational modes to encrypt an arbitrarily long string. 

Example

The mcrypt extension provides functions for crypting and decrypting, but also for generating configuration and some reflection of the algorithms (such as the lenght of their key).

For example, let's choose the popular Triple DES algorithm, and choose a key for it:
$key = "0x0a0x880x090x790x6f0xac0x6c0x410x1c0xc00xe50x320xce0xfd0xa70xdd0x
250x3b0x3c0xe70x3d0xa00x190x54"; // generated at random.org, 24 bytes
This key must be stored somewhere, at least in a file outside the document root, but it will have to be accessible to PHP scripts.

Given the key, you can encrypt a string with:
mcrypt_encrypt(MCRYPT_3DES, $key, $string, MCRYPT_MODE_ECB);
and decrypt it with:
rtrim(mcrypt_decrypt(MCRYPT_3DES, $key, $encryptedData, MCRYPT_MODE_ECB), "\0"); 
// rtrim() will cut padding NULL bytes
I have chosen Triple DES in this example as AES is not available; it's usually safer to rely on a language implementation of a less powerful algorithm than to try implementing yourself one. I'm not a fan of importing libraries and frameworks by default, but cryptography is a difficult field and you're better off leaving the complexity of the mathematics involved to someone else.

In this example, we instantiated the block cipher in ECB (Electronic Code Book) mode: this means every block is encrypted separately, in the simplest variant of encryption modes. In the case of Triple DEs, the length of the block is 192 bits, as is that of the key.

With ECB, equal plain text blocks will always result in equal encrypted ones; this causes noticeable patterns in the encrypted data. For this reason, you should only use ECB to encrypt data smaller than a block (192 bits means 24 bytes of data), for example an email address.

Modes

Other modes of operations ramp up the complexity of implementation, but allows to encrypt arbitrarily long strings with a cipher that is only designed for a block of hundreds of bits. For example CBC (Cipher Block Chaining) uses the result of the encryption of block N in the computation of the encryption of block N+1, so that two equal blocks are effectively mapped to different encrypted versions.

However, this non deterministic output means that on the decrypting side additional information will be required with respect to the key. An initialization vector is the block 0 of an encryption mode, used as the preceding block of the first one to be encrypted. It's similar to a salt used in hashing, as it renders the output non predictable even for perfectly identical plain texts. You can specify an initialization vector, for example, in CBC mode.

The initialization vector must be random, uniform and unique for each value to crypt. It can be shared in plain text with whoever is receiving the crypted information; for example, you may send this JSON document over the wire:
{
    crypted: "...",
    iv: "",  // a X-long vector
}
The only other information necessary on the other side are the name of the algorithm and its mode, which are usually shared out-of-band during design.

Blocks PHP Plain text

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is API-First?
  • Tracking Software Architecture Decisions
  • How To Build a Spring Boot GraalVM Image
  • The Path From APIs to Containers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: