Serialization and Encryption Problem (Urget and most important)
-
Hello Gurus
I'm facing a problem that is pissing me off for hours. I have a simple serializeable class named Message1.
Here is the code for it.[Serializable]
public class Message1
{
private string chatMessage;
private bool isPrivate;
private string sender;public string ChatMessage { get { return chatMessage; } set { chatMessage = value; } } public bool IsPrivate { get { return isPrivate; } set { isPrivate = value; } } public string Sender { get { return sender; } set { sender = value; } } public Message1() { }
And 1 TcpListener and 1 TcpClient.
I want to encrypt the message field of Message1 Class and relay it on the network after serializing an object of Message1 using BinaryFormatter. It encrypts fine and serialization is also 100% ok. When i receive it on the server end and deserialize it and decrypt the message field. it gives me some wrong string.
Code for Server:listener = new TcpListener(IPAddress.Any, 9000);
listener.Start();
client = listener.AcceptTcpClient();
dataStream = client.GetStream();
msg = new Message1();
formatter = new BinaryFormatter();
msg = (Message1)formatter.Deserialize(dataStream);
string str = Chiper.Decrypt(msg.ChatMessage);
MessageBox.Show("Message : " + str + " Private : " + msg.IsPrivate.ToString() + " Sender : " + msg.Sender);
listener.Stop();Code for the Client:
msg = new Message1(); msg.Sender = "Rizwan Ahmed"; msg.ChatMessage = Chiper.Encrypt("Hello how are you?"); msg.IsPrivate = true; client = new TcpClient(); client.Connect(IPAddress.Parse("192.168.0.28"), 9000); formatter = new BinaryFormatter(); formatter.Serialize(client.GetStream(), msg);
-
Hello Gurus
I'm facing a problem that is pissing me off for hours. I have a simple serializeable class named Message1.
Here is the code for it.[Serializable]
public class Message1
{
private string chatMessage;
private bool isPrivate;
private string sender;public string ChatMessage { get { return chatMessage; } set { chatMessage = value; } } public bool IsPrivate { get { return isPrivate; } set { isPrivate = value; } } public string Sender { get { return sender; } set { sender = value; } } public Message1() { }
And 1 TcpListener and 1 TcpClient.
I want to encrypt the message field of Message1 Class and relay it on the network after serializing an object of Message1 using BinaryFormatter. It encrypts fine and serialization is also 100% ok. When i receive it on the server end and deserialize it and decrypt the message field. it gives me some wrong string.
Code for Server:listener = new TcpListener(IPAddress.Any, 9000);
listener.Start();
client = listener.AcceptTcpClient();
dataStream = client.GetStream();
msg = new Message1();
formatter = new BinaryFormatter();
msg = (Message1)formatter.Deserialize(dataStream);
string str = Chiper.Decrypt(msg.ChatMessage);
MessageBox.Show("Message : " + str + " Private : " + msg.IsPrivate.ToString() + " Sender : " + msg.Sender);
listener.Stop();Code for the Client:
msg = new Message1(); msg.Sender = "Rizwan Ahmed"; msg.ChatMessage = Chiper.Encrypt("Hello how are you?"); msg.IsPrivate = true; client = new TcpClient(); client.Connect(IPAddress.Parse("192.168.0.28"), 9000); formatter = new BinaryFormatter(); formatter.Serialize(client.GetStream(), msg);
What exactly is Chiper? Custom class? Are your sure it is doing the right thing? You should check it with something like this:
string msg = "Hello world!";
if (!msg.Equals(Chiper.Decrypt(Chiper.Encrypt(msg))))
MessageBox.Show("Chiper is not working correctly.");Encrypt and Decrypt also seem to be static. Is there any password or something like this which is probably different on client and server side?
-
What exactly is Chiper? Custom class? Are your sure it is doing the right thing? You should check it with something like this:
string msg = "Hello world!";
if (!msg.Equals(Chiper.Decrypt(Chiper.Encrypt(msg))))
MessageBox.Show("Chiper is not working correctly.");Encrypt and Decrypt also seem to be static. Is there any password or something like this which is probably different on client and server side?
Sir Thanks alot for your interest. I have figured out the solution. Yes Chiper is my own class all is working well. I just mistakenly converted the decrypted bytes into Base64 string as i do after Chiper.Encrypt() replacing return Convert.ToBase64String() to return Encoding.ASCII.GetString() in Chiper.Decrypt() method. It's working fine now thanks alot again. Yeh that'sll be my help if you tell me whats the difference between Base64 string and the string one we get from Encoding.ASCII.GetString()??? and why it is needed in in the Encryption classes? Why Encoding.ASCII looses the information?? Thanks alot again Sir. Best Regards, Rizwan Ahmed
-
Sir Thanks alot for your interest. I have figured out the solution. Yes Chiper is my own class all is working well. I just mistakenly converted the decrypted bytes into Base64 string as i do after Chiper.Encrypt() replacing return Convert.ToBase64String() to return Encoding.ASCII.GetString() in Chiper.Decrypt() method. It's working fine now thanks alot again. Yeh that'sll be my help if you tell me whats the difference between Base64 string and the string one we get from Encoding.ASCII.GetString()??? and why it is needed in in the Encryption classes? Why Encoding.ASCII looses the information?? Thanks alot again Sir. Best Regards, Rizwan Ahmed
base64 is a binary encoding scheme for email attachments. The email protocalls are ancient and were written so long ago that the performance gained by using 7 bit bytes instead of 8bit bytes. Base 64 takes a series (7bytes?) of 8bit bytes and encodes them as series of 7 bit bytes (8bytes?) which are represented in modern hardware as 8 bit bytes with the high bit set to zero. It might also contain parity or ECC bits, I'm not overly familiar with it.