C# Async Sockets: Send data base64 coded?
===

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);
        }

