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

  • Home
  • SEARCH
  • 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 8325301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:23:09+00:00 2026-06-09T00:23:09+00:00

I have the problem that I am trying to send data as a base64

  • 0

I have the problem that I am trying to send data as a base64 format. The send from client works great. But if im trying to decompile base64 on server, im get still always an error, invalid base64 charecters.

Im search for a way, to encrypt data from client to server and decrypt it on server, for secure transfer.

My code shows atm like this:
Client:

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.BeginConnect(ipAddress, ipAddressPort, new AsyncCallback(testA), null);

protected static void testA (IAsyncResult ar) {
      socket.EndConnect(ar);
      string str = EncodeTo64("Hello world!");
      socket.BeginSend(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length, SocketFlags.None, new AsyncCallback(testB), socket);
    }
    protected static void testB (IAsyncResult ar) {
      socket.EndSend(ar);
      socket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(testC), socket);
    }
    protected static void testC (IAsyncResult ar) {
      socket.EndReceive(ar);
      MessageBox.Show(System.Text.Encoding.UTF8.GetString(bytes));
    }
    static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }
    static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }

Server:

serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 4444);

serverSocket.Bind(ipEndPoint);
serverSocket.Listen(4);


serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);

 protected static byte[] byteData = new byte[1024];
private void OnAccept(IAsyncResult ar)
    {
      Socket clientSocket = serverSocket.EndAccept(ar);

      //Start listening for more clients
      serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);

      //Once the client connects then start receiving the commands from her
      // Create the state object.
      clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
           new AsyncCallback(OnReceive), clientSocket);
    }

private void OnReceive(IAsyncResult ar) {
      Socket clientSocket = (Socket)ar.AsyncState;
      clientSocket.EndReceive(ar);
MessageBox.Show(DecodeFrom64(System.Text.Encoding.UTF8.GetString(byteData)));
}
    static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }
    static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }
    public void OnSend(IAsyncResult ar) {
      Socket client = (Socket)ar.AsyncState;
      client.EndSend(ar);
    }
  • 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-06-09T00:23:12+00:00Added an answer on June 9, 2026 at 12:23 am

    As Marc Gravell wrote, you unnecessarily complicate your network protocol. Network transmissions are complicated by nature, so avoid any unnecessary complication!

    And he is also absolutly right when he says that you have to ensure, that you really read all sent data. Please have a look at the documentation and the examples there (especially Socket class and Socket.EndReceive; note that just calling EndReceive is not enough to ensure that everything has been read). The primary point is: You need a loop on the server side that ensures that all send data has been read. If the data to send is simple and the network is quite reliable (read: local Ethernet via cable, no WiFi; Internet and especially mobile Internet is not reliable), maybe just a loop until no more data arrived may be enough; in any more complex scenarios it would be wiser to use a framing protocol as Marc suggested, that would provide a more reliable way to decide when to end the reading loop.

    You can reproduce your error easily with the following code:

    var data = Encoding.ASCII.GetBytes("Hello World!");
    var base64 = Convert.ToBase64String(data);
    var bytesToSend = Encoding.UTF8.GetBytes(base64);
    var stringRecieved = Encoding.UTF8.GetString(bytesToSend).Substring(0, 5);
    var decoded = Convert.FromBase64String(stringRecieved);
    var result = Encoding.ASCII.GetString(decoded);
    Console.WriteLine("{0} -> {1} -> {2}", base64, stringRecieved, result);
    

    The .Substring(0, 5) simulates a network stream that has not been read completely. If you remove the .Substring then everything works fine. To support Marcs point, the following would be enough to send data:

    var bytesToSend = Encoding.UTF8.GetBytes("Hello World!");
    var stringRecieved = Encoding.UTF8.GetString(bytesToSend);
    

    Another important note: BASE64 has absolutely nothing to do with encryption. It’s just an encoding, like UTF8 or ASCII. If you need a secure communication channel, BASE64 is not the solution! If an eavesdropper reads your BASE64 encoded message, he can easily identify the encoding and then its trivial to decode it.

    If you need a secure channel, either use strong encryption standards like AES or use a secure network protocol like TLS (for example via HTTPS; using HTTPS may also solve the ‘framing’ problem mentioned by Marc Gravell for more complex data exchanges).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send data from client-side to server-side (asp.net c#), if you really
I'm trying to send some data from a C++ server to a C# client.
I'm trying to send data from an AJAX call to database tables that are
I have some troubles trying to move data from SQL Server 2000 (SP4) to
I have a desktop client that sends data to a web server and I
So we have this problem that we are trying to figure out. Heres what
I have been Googling a problem that I have with trying to integrate the
I have a problem in a site that I am trying to develop Sometimes
The problem: I have a WCF service that I'm trying to performance test with
I'm trying to solve the following problem in Redis. I have a list that

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.