Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:36:42+00:00 2026-05-26T21:36:42+00:00

I’m trying to securely transfer files between 2 devices, so I’m using an SslStream

  • 0

I’m trying to securely transfer files between 2 devices, so I’m using an SslStream attached to a TcpClient. Documents and text files come across just fine, but image files don’t show up correctly. The following is the server code:

listener = new TcpListener(IPAddress.Any, 1337);
listener.Start();

while (true)
{
    TcpClient client = listener.AcceptTcpClient();

    SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallback), new LocalCertificateSelectionCallback(CertificateSelectionCallback));
    var certificate = Connection.GetClientCertificate(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
    try
    {
        sslStream.AuthenticateAsServer(certificate, true, SslProtocols.Default, true);

        sslStream.ReadTimeout = 5000;
        sslStream.WriteTimeout = 5000;

        var messageData = ReadMessage(sslStream);

        var mode = messageData[0];
        var tokenBytes = messageData.Splice(1, 16);
        var fileNameBytes = messageData.Splice(17, 128);
        var fileBytes = messageData.Splice(146);

        var fileName = Encoding.ASCII.GetString(fileNameBytes).TrimEnd('\0');
        using (var tempFile = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
        {
            tempFile.Write(fileBytes, 0, fileBytes.Length);
            tempFile.Flush();
        }

        if (mode == 0)
            tempFiles.Add(fileName);

        Process.Start(fileName);
    }
    catch (AuthenticationException e)
    {
        MessageBox.Show("The other side failed to authenticate.");
    }
    finally
    {
        sslStream.Close();
        client.Close();
    }
}

And ReadMessage is defined as follows:

private static byte[] ReadMessage(SslStream sslStream)
{
    byte[] buffer = new byte[2048];
    MemoryStream stream = new MemoryStream();
    int bytes = -1;
    while (bytes != 0)
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);
        stream.Write(buffer, 0, bytes);
    }
    return stream.ToArray();
}

And then the client code is this:

TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse(ip), 1337));

SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallback), new LocalCertificateSelectionCallback(CertificateSelectionCallback));
var certificate = Connection.GetClientCertificate(ip);
try
{
    sslStream.AuthenticateAsClient(ip, new X509CertificateCollection() { certificate }, SslProtocols.Default, false);

    sslStream.ReadTimeout = 5000;
    sslStream.WriteTimeout = 5000;
    sslStream.Write(data);
}
catch (AuthenticationException e)
{
    MessageBox.Show("The other side failed to authenticate.");
}
finally
{
    sslStream.Close();
    client.Close();
}

And the code that calls into it just does:

var fileBytes = File.ReadAllBytes(file);
var tokenBytes = Encoding.UTF8.GetBytes(token);
var fileNameBytes = Encoding.UTF8.GetBytes(Path.GetFileName(file));

var buffer = new byte[145 + fileBytes.Length];
buffer[0] = 1;
for (int i = 0; i < 16; i++)
{
    buffer[i + 1] = tokenBytes[i];
}

for (int i = 0; i < fileNameBytes.Length; i++)
{
    buffer[i + 17] = fileNameBytes[i];
}

for (int i = 0; i < fileBytes.Length; i++)
{
    buffer[i + 145] = fileBytes[i];
}

SocketConnection.Send(ip, buffer);

Is there anything inherently wrong with what I’m doing, or do I need to do something different for images?

EDIT: I have changed it to reflect the current code, and also, after doing a dump of the raw bytes on both ends, it looks like for some reason the bytes are getting rearranged when they come over the wire. Is there any way to ensure that the bytes come across in the original order?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:36:42+00:00Added an answer on May 26, 2026 at 9:36 pm

    In ReadMessage you’re writing bytes.Length bytes to the stream, regardless of the number of bytes that were actually read. Try:

    private static byte[] ReadMessage(SslStream sslStream)
    {
        byte[] buffer = new byte[2048];
        MemoryStream stream = new MemoryStream();
        int bytes = -1;
        while (bytes != 0)
        {
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            // Use "bytes" instead of "buffer.Length" here
            stream.Write(buffer, 0, bytes);
        }
        return stream.ToArray();
    }
    

    Based on your follow-up, you’re also taking the file data from the wrong point in the buffer, and so you’re losing the first byte of the file.

    Your code should be:

    var fileBytes = messageData.Splice(145); // File data starts at 145, not 146
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.