1

I am working on a TCP/IP project, I need to send encrypted packages via sockets. I've completed network part, I can send strings but all my packages are objects. So I have to serialize my package class to string and encrypt it, then after client receives deserialize and decrypt it. Cany you help me please?

Package.cs

 public class Package
{
    private string context;
    public string Context
    {
        get { return context; }
        set { context = value; }
    }
    private bool flag;
    public bool Flag
    {
        get { return flag; }
        set { flag = value; }
    }
    private int statusCode;
    public int StatusCode
    {
        get { return statusCode; }
        set { statusCode = value; }
    }

    public Package() { this.context = null; }
}
osumatu
  • 410
  • 1
  • 8
  • 25
  • 2
    Encryption/decryption is quite a massive topic. What kind of encryption do you want to use? For example, will it be enough to just encrypt/decrypt using a key known by both ends, or do you need to do something like full public/private key encryption? – Matthew Watson Aug 02 '17 at 08:17
  • @MatthewWatson if I use key encryption, how will it be transported between systems? – osumatu Aug 02 '17 at 08:25

1 Answers1

1

For serialization you can use JavaScriptSerializer class.

Add reference System.Web.Extensions to your project then;

private string Serialize(object obj){
var serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}

private object Deserialize(string json){
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<object>(json);
}

For encryption/decryption you can basically use Base64 but if you want more spesific answer, you need to tell more details about your requirements.

Sahin
  • 114
  • 7