Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Async Sockets: Send data base64 coded?

Tags:

c#

base64

sockets

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);
    }
like image 976
user1558672 Avatar asked Dec 29 '25 04:12

user1558672


1 Answers

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).

like image 117
Stefan Nobis Avatar answered Dec 30 '25 19:12

Stefan Nobis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!