What are the best practices for handling ASCII values in PHP when creating a symmetric encryption algorithm?

When creating a symmetric encryption algorithm in PHP, it is important to properly handle ASCII values to ensure compatibility and consistency. One of the best practices is to convert the input string to ASCII values before encryption and then convert it back to the original string after decryption. This ensures that the encryption process does not alter the data and allows for seamless encryption and decryption operations.

// Convert string to ASCII values before encryption
function stringToAscii($string) {
    $ascii = '';
    for ($i = 0; $i < strlen($string); $i++) {
        $ascii .= ord($string[$i]) . ' ';
    }
    return trim($ascii);
}

// Convert ASCII values back to string after decryption
function asciiToString($ascii) {
    $string = '';
    $asciiArr = explode(' ', $ascii);
    foreach ($asciiArr as $char) {
        $string .= chr($char);
    }
    return $string;
}

// Example usage
$inputString = "Hello, World!";
$encrypted = encrypt(stringToAscii($inputString));
$decrypted = asciiToString(decrypt($encrypted));

echo "Original String: $inputString\n";
echo "Encrypted String: $encrypted\n";
echo "Decrypted String: $decrypted\n";