On this page
Symmetric Key Algorithms
A Symmetric-Key Algorithm uses a string of data to encrypt and decrypt information. This string of data acts like a real-world key which can lock and unlock a door. In fact, it is often called a “key” or a “password”. With symmetric-key algorithms, the same key is used for encrypting and for decrypting (that is what makes it “symmetric”).
Anyone who knows or possesses the key can decrypt the information. Anyone who does not know or possess the key cannot decrypt it easily. It cannot be simply unscrambled like a substitution cipher. Someone would need to use advanced techniques to “break” the encryption.
If the algorithm is hard to break, this is a better way to encrypt messages than a substitution cipher. Messages can be sent publicly, but only the recipient who knows the password can decrypt it. Much of the history of cryptography (and military communication/intelligence) has been dedicated towards either developing stronger algorithms or trying to break current algorithms.
The rapid increase of computing power beginning in the 1970s transformed the cryptography landscape. Hundreds of algorithms have been developed and hundreds have been broken. There are three algorithms which are notable for their wide-spread usage, though not all of them are still considered secure.
-
The Data Encryption Standard (DES) algorithm was developed at IBM and first published in 1977. It was one of the first N.S.A. approved standards for encryption. It was widely used from 1977 until 2000. It was also widely studied and informs the design of later algorithms. However, it is now considered insecure. Modern computers can decrypt a DES encrypted message in less than a day. Its successor Triple DES (DES encryption applied three times) is also no longer recommended: NIST disallowed three-key Triple DES (TDEA) for applying cryptographic protection after December 31, 2023 and withdrew SP 800-67 Rev. 2 in January 2024. Use AES instead.
-
The Advanced Encryption Standard (AES) algorithm was selected by N.I.S.T. as the winner of an algorithm competition that concluded in October 2000, and was published as the federal standard FIPS PUB 197 on November 26, 2001. It is also known as “Rijndael” (pronounced rain-dahl) because that was its name during the competition. AES has not yet been broken and is still considered strong enough to encrypt U.S. classified data.
-
The Blowfish algorithm was designed in 1993. It has not yet been broken, even though a few technical and theoretical weaknesses have been identified. Its 64-bit block size is a practical weakness, though: the Sweet32 birthday attack showed that when a long-lived connection encrypts hundreds of gigabytes under one key with a 64-bit block cipher such as Blowfish or Triple DES, an attacker can recover secrets such as HTTP cookies, regardless of key length. The Sweet32 authors advise that the obvious defense is to stop using legacy 64-bit block ciphers, so prefer AES (128-bit blocks) for new encryption. It is also widely used in many encryption software products. Blowfish encryption itself is fast — its designer benchmarked it at roughly 18 cycles per byte on a Pentium (Schneier, “The Blowfish Encryption Algorithm”) — but it has a deliberately expensive key-setup phase. The bcrypt password-hashing algorithm exploits that property: it runs a modified Blowfish key schedule (Eksblowfish) for a configurable number of rounds to slow down password cracking (Wikipedia: “bcrypt”). It is bcrypt’s iterated key setup, not Blowfish encryption, that makes bcrypt-hashed passwords expensive to brute-force.
Symmetric Key Algorithms Pros and Cons
Symmetric key algorithms are very fast and can encrypt large amounts of data.
The primary drawback of symmetric key algorithms is the “key distribution problem”. Once data is encrypted it can be sent publicly, however, the key or password must also be given to the recipient. If the key is sent with the data or sent in plain text through another visible channel, then it is easily intercepted and the data will be easily decrypted.
One common solution is to use symmetric key algorithms to encrypt the data and then to use asymmetric key algorithms (a.k.a. public key cryptography) to encrypt only the key which unlocks the encrypted data.
Encryption/Decryption in PHP using AES
Use PHP’s OpenSSL functions to encrypt data with a key. The example below uses AES-256-GCM, an authenticated (AEAD) cipher mode: in addition to the ciphertext, GCM produces a short authentication tag that lets the recipient detect any tampering with the ciphertext, IV, or associated data. OWASP’s Cryptographic Storage Cheat Sheet recommends GCM (or CCM) as the first choice precisely because unauthenticated modes such as CBC or CTR provide no integrity guarantee and require a separate MAC to be safe against active attackers. openssl_encrypt() and openssl_decrypt() have supported GCM since PHP 7.1 via the $tag by-reference parameter (see the PHP manual).
An AES-256 key must be 32 bytes of raw randomness from a cryptographically-secure source — not a short string padded out to length. OWASP’s Cryptographic Storage Cheat Sheet states that keys “should not be based on common words or phrases, or on ‘random’ characters generated by mashing the keyboard”; a construction like str_pad('a1b2c3d4e5', 32, '*') produces a 32-byte string, but the effective search space is only the entropy of the original 10-character seed (roughly 50 bits), which is trivially brute-forceable regardless of the cipher. Use random_bytes(32) — the PHP manual notes it is “suitable for all applications, including the generation of long-term secrets, such as encryption keys” — and persist the result in a secrets manager, KMS, or environment variable rather than hard-coding it in source. If the key must come from a user-supplied passphrase, run the passphrase through a KDF first: hash_pbkdf2('sha256', $passphrase, $salt, 600000, 32, true) follows OWASP’s current PBKDF2-HMAC-SHA256 recommendation of 600,000 iterations. Never rely on openssl_encrypt()’s own $passphrase argument to stretch a short key — the PHP manual is explicit that “there is no key derivation function used for passphrase as its name might suggest. The only operation used is padding with NUL characters or truncation if the length is different than expected.”
<?php
const CIPHER_METHOD = 'AES-256-GCM';
$plaintext = 'This is a secret.';
// Generate a fresh 256-bit key from PHP's CSPRNG. In production
// you generate this ONCE, persist it in a secrets manager or KMS,
// and load it from there on subsequent runs -- never hard-code a
// key in source, and never derive one by padding a short string
// (str_pad('a1b2c3d4e5', 32, '*') looks like a 32-byte key but
// carries only the ~50 bits of entropy from the original 10 chars).
// If the key must come from a user passphrase, run it through
// hash_pbkdf2('sha256', $passphrase, $salt, 600000, 32, true)
// first -- openssl_encrypt() itself does NOT stretch its input.
$key = random_bytes(32);
// Create an initialization vector (called a "nonce" in GCM).
// openssl_cipher_iv_length() returns 12 bytes for AES-256-GCM,
// the recommended nonce size. A fresh random nonce MUST be used
// for every encryption performed with the same key.
// random_bytes() is the modern, cryptographically-secure source for
// key material and nonces (PHP 7.0+); it throws on failure rather
// than silently returning false the way openssl_random_pseudo_bytes()
// can on older PHP versions.
$iv_length = openssl_cipher_iv_length(CIPHER_METHOD);
$iv = random_bytes($iv_length);
// $tag is populated by reference: openssl_encrypt() writes the GCM
// authentication tag here (16 bytes by default).
$tag = '';
// Encrypt
$ciphertext = openssl_encrypt(
$plaintext, CIPHER_METHOD, $key, OPENSSL_RAW_DATA, $iv, $tag
);
// openssl_encrypt() returns false on failure (unknown cipher, bad
// key/IV length, OpenSSL error). Concatenating a false return below
// would coerce it to an empty string and silently produce a malformed
// message -- always check before building the bundle.
if ($ciphertext === false) {
throw new RuntimeException('Encryption failed.');
}
// The IV, ciphertext, and tag must all be sent to the recipient.
// Base64-encode the bundle so it can be stored or sent as text.
$message = base64_encode($iv . $ciphertext . $tag);
// The recipient needs BOTH the base64-encoded message AND the key
// (the latter delivered out-of-band via a secrets manager, not on
// the same channel as the ciphertext). Hex and base64 just make the
// bytes viewable and savable; the decryption example below reuses
// the in-memory $key and $message directly.
echo 'key : ' . bin2hex($key) . "\n";
echo 'message: ' . $message . "\n";
// Example output shape (a new random key and IV are chosen every
// run, so the printed values differ each time):
// key : 64 hex characters (the 32-byte key)
// message: 60 base64 characters encoding 45 bytes -- the 12-byte
// nonce, 17 bytes of ciphertext (GCM ciphertext is the
// same length as the 17-byte plaintext), and the 16-byte tag
?>
Use PHP’s OpenSSL functions to decrypt data with a key. The example below continues the encryption script above and reuses its in-memory $key and $message, so the two blocks run together as one file and print the original plaintext. The recipient needs both the base64-encoded message and the 32-byte key (delivered out-of-band, e.g. via a shared secrets manager or KMS handoff). With GCM, decryption only succeeds if the supplied authentication tag matches the ciphertext, IV, and key; otherwise openssl_decrypt() returns false and the plaintext is never produced. Always check that return value and refuse to act on a false result — treating it as an empty string would let an attacker bypass the integrity check.
<?php
// This block continues the encryption script above: paste it after
// that code in the same file. It reuses the CIPHER_METHOD constant
// and the in-memory $key and $message. A PHP constant cannot be
// redefined once set, so CIPHER_METHOD is not declared again here.
//
// In production the recipient is usually a separate process: load
// the 32-byte key from a secrets manager, KMS, or environment
// variable (do NOT hard-code it in source), and read the base64
// message from wherever it was stored or received. A key saved in
// the hex form printed above converts back to raw bytes with hex2bin().
const TAG_LENGTH = 16;
// Base64 decode before decrypting. Pass `true` for strict mode so
// invalid characters cause a `false` return instead of being silently
// discarded -- otherwise a tampered or malformed message could quietly
// mis-split into nonce/ciphertext/tag.
$bundle = base64_decode($message, true);
$iv_length = openssl_cipher_iv_length(CIPHER_METHOD);
if ($bundle === false || strlen($bundle) < $iv_length + TAG_LENGTH) {
throw new RuntimeException('Decryption failed: malformed message.');
}
// Split the stored bundle into nonce, ciphertext, and auth tag.
$iv = substr($bundle, 0, $iv_length);
$tag = substr($bundle, -TAG_LENGTH);
$ciphertext = substr($bundle, $iv_length, -TAG_LENGTH);
// Decrypt AND verify the tag in a single call. Returns false if the
// ciphertext, IV, or tag has been tampered with.
$plaintext = openssl_decrypt(
$ciphertext, CIPHER_METHOD, $key, OPENSSL_RAW_DATA, $iv, $tag
);
if ($plaintext === false) {
// Authentication failed: forged, corrupted, or wrong key. Do not
// fall through to using a false/empty plaintext.
throw new RuntimeException('Decryption failed: invalid authentication tag.');
}
echo $plaintext;
// This is a secret.
?>