0
votes

so I have the following problem: I'm encoding a file's contents in C# with UTF-8 into base64, then URL-encoding it and sending a GET request to my php, then I url decode it there and then base64 decode it. The outcome is not 1:1 to the file I encoded in C#, no matter what. I've tried all encodings in C#, neither of them work. Am I supposed to do something else here? I'd also like to mention that I'm echoing back the file into an octet stream, perhaps echo can't "echo" UTF-8 characters?

Here's my Base64 Encoding in C#:

    public static string Base64Encode(string plainText)
    {
         var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
         string base64 = System.Convert.ToBase64String(plainTextBytes);
         return WebUtility.UrlEncode(base64);
    }

Here's how I decode it in PHP:

base64_decode(urldecode($_GET["parameter"]));

And here's how I'm downloading it:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="<filename>"');
echo(base64_decode(urldecode($_GET["parameter"])));

Thanks.

1
no code, no help. - Franz Gleichmann
Added the code. - L4P4D4
Yeah, I've just fixed that - still no luck. - L4P4D4
Have you got a short failure case? - Llama
Thanks, it's working fine, if you leave an answer I'll accept it as a solution. - L4P4D4

1 Answers

0
votes

Instead of reading the file as a string, and then converting it back to binary in order to convert it to a base64 string, you could simply read the file as bytes:

var fileBytes = File.ReadAllBytes(fileName);

And then convert this to base64:

var fileBytes = File.ReadAllBytes(fileName);
var base64 = Convert.ToBase64String(fileBytes);