PHP how encode/decode text with key?

PHP how encode/decode text with key?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Data Encrypted with mcrypt_ecb in PHP

You have successfully encrypted a string using the following snippet, likely within an older PHP environment:

$result = mcrypt_encryption(MCRYPT_3DES, 'test', $string, MCRYPT_ENCRYPT);

Note: The source material provided uses mcrypt_ecb, but standard PHP documentation for this function typically follows the pattern mcrypt_encrypt. Assuming the intent was to use mcrypt_encrypt with ECB mode and 3DES, which is the standard signature.

The code above takes $string and produces $result. However, encryption is a two-way process; without the correct inverse operation, $result is just gibberish. To retrieve your original data, you must reverse the cryptographic process using the exact same parameters and algorithm.

The Fundamental Rule of Symmetric Encryption

The most critical rule in symmetric cryptography is consistency. You cannot decrypt what you did not encrypt with the same key, or vice versa. If you encrypted using Triple-DES (3DES) in Electronic Codebook (ECB) mode, your decryption function must also use 3DES in ECB mode. Furthermore, the encryption key must be identical.

In your specific case, the key is the string 'test'. While using a hardcoded string like this for development is acceptable for learning or controlled environments, it introduces significant security risks in production. The key must remain constant between the encryption and decryption operations. Changing the key even slightly will result in an inability to decrypt the data, often returning false or a completely different hash.

Implementing the Decryption Logic

To decode $result, you simply invoke the same function with the MCRYPT_DECRYPT flag instead of MCRYPT_ENCRYPT. The structure of the call remains identical; only the direction of the operation changes.

Here is how you would implement the decryption counterpart:

// $result contains the encrypted data (base64 encoded usually, but raw here)
$key = 'test';
$mode = MCRYPT_3DES;

// Decrypt the result
$decrypted = mcrypt_decrypt($mode, $key, $result, MCRYPT_DECRYPT);

if ($decrypted !== false) {
    echo $decrypted;
} else {
    // Handle decryption failure (wrong key, corrupted data, or invalid padding)
    throw new Exception("Decryption failed.");
}

Handling Output Formats

A common point of confusion arises from how the output is stored. The mcrypt_encrypt function outputs binary data. If you stored $result directly into a file or database field without encoding it, it might appear as binary garbage when inspected in a text editor.

If you intended to store this in a web-friendly format (like JSON, SQL, or a URL), you likely need to encode the output using Base64 before saving it. Consequently, your decryption routine must decode that Base64 string before passing it to mcrypt_decrypt.

// If $result was stored as a Base64 string
$binaryResult = base64_decode($result);
$decrypted = mcrypt_decrypt(MCRYPT_3DES, 'test', $binaryResult, MCRYPT_DECRYPT);

Critical Limitations and Modern Alternatives

While the code above answers your immediate question, relying on mcrypt functions is a technical debt you should address. The mcrypt extension has been deprecated in PHP 7.1 and removed entirely in PHP 8.0. If you are running modern PHP versions, this code will not work unless you install legacy extensions, which is generally discouraged.

Beyond availability, ECB mode itself is cryptographically weak for most use cases. ECB encrypts identical blocks of plaintext into identical blocks of ciphertext. This leaks patterns; if an image is encrypted with ECB, the outline of the image remains visible in the encrypted data. For sensitive data, you should always use modes that incorporate randomness, such as CBC (Cipher Block Chaining) or GCM (Galois/Counter Mode), which utilize an Initialization Vector (IV).

Additionally, hardcoding keys like 'test' is insecure. Production systems require robust key management strategies, often involving environment variables or a dedicated vault service.

For modern PHP applications, the industry standard has shifted away from the raw mcrypt API to the OpenSSL extension. The openssl_encrypt and openssl_decrypt functions offer better security features, including automatic IV generation and support for authenticated encryption modes like GCM. If you are starting a new project or refactoring legacy code, migrating to the OpenSSL interface is the recommended path forward.

Our network