PHP sending encrypted data via the URL

PHP sending encrypted data via the URL

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

Stefan Bogdanescu

Founder & Senior Architect

PHP sending encrypted data via the URL

When transmitting cryptographic material over a URL query string, the immediate assumption is often that Base64 encoding is sufficient because it produces an alphanumeric output plus +, /, and =. However, the presence of these specific characters creates a collision course with how web servers and clients parse URLs. The core issue isn't necessarily the encryption algorithm itself, but rather the mismatch between the binary nature of encrypted data and the text-based constraints of the URI standard.

In your scenario, you are using mcrypt_encrypt to generate ciphertext, wrapping it in Base64, and passing it via file_get_contents. The example string oWCrVPaS+5GbxcQFc0fulUk/zRAkDD60av4zlPiWskE= demonstrates the problem clearly: the + character is a reserved symbol in URLs, often interpreted as a space or used for padding logic depending on the parser. Similarly, the forward slash / can be misinterpreted as path separators if not strictly escaped, and the trailing = signs are part of the Base64 alphabet but require careful handling during parsing to avoid confusion with query parameter delimiters.

The most robust solution is not to modify the encryption algorithm or force Base64 output into a URL-safe subset (like using only alphanumeric characters), which would break the standard Base64 decoding logic required by your decryption method. Instead, you must treat the encrypted string as a literal token that needs transport-layer protection. The standard approach is to apply URL encoding specifically to the query parameter value before appending it to the URL, and then strictly decode it immediately upon receipt.

If you attempt to use urlencode() directly on the Base64 string, you will encounter issues because urlencode() applies percent-encoding to all non-alphanumeric characters. While this technically solves the + and / problem by turning them into %2B and %2F, it can sometimes lead to confusion if the receiving end does not explicitly call urldecode() before passing the string to base64_decode().

Here is the corrected workflow for your PHP implementation:

Sending the Data:
Before constructing the URL, ensure the encrypted string is fully escaped. Using rawurlencode() is generally preferred over urlencode() in modern contexts because it does not double-encode plus signs as spaces, which aligns better with RFC 3986.

$encryptedString = $this->encrypt($string, $key);
// Escape the Base64 string so it can safely sit in a URL query parameter
$safeHash = rawurlencode($encryptedString);

$url = "anotherUrl.php?hash=" . $safeHash;
$data = file_get_contents($url);

Receiving and Decoding:
The server receiving the request must reverse this process immediately. The $_GET['hash'] variable will contain the percent-encoded version of your Base64 string. You must decode it back to its original Base64 representation before passing it to base64_decode().

public function decrypt($encrypted, $key) {
    // Ensure we are receiving a clean string from $_GET['hash']
    // If the input comes from raw URL parsing, urldecode handles it.
    
    // Note: You do not use urlencode/decode here to "clean" the hash 
    // before decryption; you only do that during transport.
    
    $decoded = base64_decode($encrypted); 
    
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), $decoded, MCRYPT_MODE_CBC, md5(md5($key))), "\0");
}

A critical pitfall to avoid is the interaction between + and Base64. In standard Base64, + is a valid character representing 18. However, in HTML forms and URL query strings, unencoded + characters are historically treated as spaces. If you rely on a library or framework that automatically parses form data, an unescaped + inside your encrypted token might be interpreted as whitespace before it ever reaches your decryption logic, causing a padding error when base64_decode() attempts to process the malformed string.

Furthermore, while mcrypt is shown in your code, it is worth noting that this extension has been deprecated in PHP 7.2 and removed in PHP 8.0+. For any production environment running modern PHP versions, you must migrate to the OpenSSL extension. The logic for URL encoding remains identical, but the implementation of encryption will change to openssl_encrypt. This migration is highly recommended to ensure future compatibility, as relying on deprecated cryptographic functions poses a security and stability risk regardless of how you encode the output string.

By strictly separating the transport layer (URL encoding) from the content layer (Base64/Cryptography), you eliminate the ambiguity that causes decryption failures. The + in your example string is harmless once wrapped in a percent-encoded query parameter, provided the receiver respects the URI standard by decoding the parameter value before any cryptographic operations occur.

Our network