What are some common pitfalls when implementing a Caesar cipher-like symmetric encryption in PHP?

One common pitfall when implementing a Caesar cipher-like symmetric encryption in PHP is using a fixed shift value for encryption and decryption, which can make the encryption vulnerable to brute force attacks. To solve this issue, you should use a randomly generated shift value for each encryption operation to enhance security.

// Generate a random shift value
$shift = rand(1, 25);

// Encrypt function with random shift value
function caesarEncrypt($text, $shift) {
    $result = '';
    foreach (str_split($text) as $char) {
        $result .= chr(((ord($char) - 65 + $shift) % 26) + 65);
    }
    return $result;
}

// Decrypt function with random shift value
function caesarDecrypt($text, $shift) {
    $result = '';
    foreach (str_split($text) as $char) {
        $result .= chr(((ord($char) - 65 - $shift + 26) % 26) + 65);
    }
    return $result;
}

// Example usage
$text = "Hello, World!";
$encryptedText = caesarEncrypt($text, $shift);
echo "Encrypted text: " . $encryptedText . "\n";
echo "Decrypted text: " . caesarDecrypt($encryptedText, $shift);