Link to home
Start Free TrialLog in
Avatar of pointeman
pointemanFlag for United States of America

asked on

TextBox base64 Encoding and Padding?

Go figure, for what ever reason, loading the received data into a textbox converts the data into base64 and either corrects or preserve the padding.

Q. How can I avoid using a TextBox and give the Decrypt the encoding and padding it requires?

public  void DataRcvd(IAsyncResult aResult)
{
    string dataEncrypted = Encoding.ASCII.GetString(skt.byteBuffer, 0, skt.byteBuffer.Length);                                
   
     textBox_ReceivedMsg.Text = dataEncrypted;

    string dataDECRYTPED = myCrypto.Decrypt(txtReceivedMsg.Text, "password123");

     textBox_Decrypted.Text = dataDECRYTPED;
}
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Firstly, using ASCII encoding will cause issues as it is a 7-bit encoding only, meaning anything above 127 will be lost. This could in turn mean that the decrypted date is invalid.

What type is the myCrypto object? It's obviously a wrapper around a crypto transform. But if you're able to pass a string to myCrypto.Decrypt, is there some reason why you can't pass the dataEncrypted string to the method?
string s = System.Text.Encoding.ASCII.GetString(new byte[]{
127, 128, 129, 130, 131, 132});
byte[] buff = System.Text.Encoding.ASCII.GetBytes(s);
// buff = {127, 63, 63, 63, 63, 63}

Open in new window

Avatar of pointeman

ASKER

Yes, I have since change the code to UTF8 encoding, but still same problem.
I'm using the following TCP Sockets Server/Client example: http://www.codeguru.com/Csharp/Csharp/cs_network/sockets/article.php/c8781/
The server is the problem because it has a fixed buffer of [1024], whereas the cypherTextBytes needs to vary to match the client byte count.
http://www.obviex.com/samples/Encryption.aspx
I accidentially discovered dumping the encrypted string into a TextBox1.Text will come-out converted to base64 and the correct byte.length of 400(per test string) to satisfy the cypherTextBytes like so:

public void OnDataReceived(IAsyncResult asyn)
{      
     string message = Encoding.UTF8.GetString(socketData.dataBuffer, 0, socketData.dataBuffer.Length);

    TextBox1.Text = message;
}
private void ButtonDecrypt_Click(object sender, EventArgs e)
{
     string data = myCrypto.Decrypt(TextBox1.Text, "password123");

     TextBox2.Text = data; });
}
 
Obviously I want to eliminate the TextBox1...
ASKER CERTIFIED SOLUTION
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see you've worked with sockets before, great job and good eye. I'm new to TCP sockets, always used TcpClient and Listen, so Sockets is a real adventure. More work, but well worth the extended benefits...
Concerning the TextBox and base64 encoding: I kept receiving base64 and padding errors and discoved by accident a Textbox solved all my problems. It didn't make any sense to me either...
Thanks for all your help, you'll probably see my here again concerning TCP Sockets....
Thanks again...
Glad you've got it working.