Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program which sends header with file encoded using Encoding.ASCII.GetBytes(byte[]); but it seems that this won't work with arbitrary data. I tried sending image, videos, audio files which gets corrupted but text files such as pdf, text, xls files are received correctly. Is there a way to use Encoding.ASCII.GetBytes(byte[]); for encoding the data.

What I have tried:

C#
OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                string filename = op.SafeFileName;
                string requestString = "Admin Request|requestSplit|requestSplit|requestSplitFile Transfer Request/dataSplit" + filename + "/dataSplit";
                byte[] data = File.ReadAllBytes(op.FileName);
                byte[] clientBufferByte = Encoding.ASCII.GetBytes(requestString);
                byte[] finalArray = new byte[data.Length + clientBufferByte.Length];
                Console.WriteLine("Data Length: " + data.Length);
                Array.Copy(clientBufferByte, 0, finalArray, 0, clientBufferByte.Length);
                Array.Copy(data, 0, finalArray, clientBufferByte.Length, data.Length);
                

                byte[] requestByte = finalArray;
                if (requestByte != null)
                {
                    string text = Encoding.ASCII.GetString(requestByte);
                    var request = text.Split(new[] { "|requestSplit" }, StringSplitOptions.None)[0];
                    Console.WriteLine("Request: " + request);

                    if (request == "Admin Request")
                    {
                        string midMessage1 = text.Split(new[] { "|requestSplit" }, StringSplitOptions.None)[1];
                        string midMessage2 = text.Split(new[] { "|requestSplit" }, StringSplitOptions.None)[2];
                        string feature = text.Split(new[] { "|requestSplit" }, StringSplitOptions.None)[3];

                        if (midMessage1 == "File Transfer")
                        {
                        }
                        string filenameString = feature.Split(new[] { "/dataSplit" }, StringSplitOptions.None)[1];
                        string dataString = feature.Split(new[] { "/dataSplit" }, StringSplitOptions.None)[2];
                        Console.WriteLine("Data Length: " + dataString.Length);

                        byte[] dataToDownload = Encoding.ASCII.GetBytes(dataString);
                        File.WriteAllBytes("C:\\Users\\User\\Desktop\\SS\\" + filename, dataToDownload);
                        Console.WriteLine("C:\\Users\\User\\Desktop\\SS\\" + filename);
                        Console.ReadLine();
                    }
                }
            }
Posted
Updated 19-Jan-23 23:26pm

First off, would not suggest to use ASCII encoding. As mentioned here[^]:
Quote:
ASCIIEncoding does not provide error detection. Any Unicode character greater than U+007F is encoded as the ASCII question mark ("?").

For security reasons, you should use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

Second, believe content of EncodedPicture or EncodedVideo is a encoded representation of the data. Try to convert that back to get the desired value. Use FromBase64String.

Try
C#
 // Assuming you have UTF8 encoded
File.WriteAllBytes("C:\\Users\\User\\Desktop\\SS\\" + filename, Convert.FromBase64String(dataToDownload));
 
Share this answer
 
v2
Comments
Member 14623639 19-Jan-23 23:55pm    
So should I change my whole architecture of the application to UTF8Encoding? Because how my application works is client request server data by sending a string as request type|request data|ip address and server replies with request type|requested data|actual data so this is all happening in ASCII. So are you suggesting me to do everything in UTF8Encoding, UnicodeEncoding, or UTF32Encoding?

Second, I'm using byte[] dataToDownload = Encoding.ASCII.GetBytes(dataString); and later using it in File.WriteAllBytes() which is not working.
Sandeep Mewara 20-Jan-23 0:58am    
UTF-8 extends the ASCII character set to use 8-bit code points, which allows for up to 256 different characters. This means that UTF-8 can represent all of the printable ASCII characters, as well as the non-printable characters.

From security aspect as well as UTF8 supporting more, suggestion would be to use it. You can try and see if UTF8 works for your images and take specific calls.
Member 14623639 20-Jan-23 3:40am    
I tried using Encoding.UTF8.GetBytes() and Encoding.UTF8.GetString().

I tried
byte[] dataRead = File.ReadAllBytes(op.FileName);
byte[] data = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(dataRead));
to get the file into UTF8 and after writing it returns with corrupt file with invalid size
File.WriteAllBytes(filenameandpath, data);
You should not use any text encoding on binary data like images. Use System.Convert.ToBase64string to to get a text string representing your data, then use System.Convert.FromBase64String to get your data back once it is received.

ASCII will work in your case, but there is no need to use it. Just make it a habit to use UTF8 everywhere, then you don't have to think about which strings will work. But not for your images of course.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900