Re: a problem with encryption

From: Tonci Jukic (nytrogen_at_email.htnet.hr)
Date: 08/10/04

  • Next message: TabletTC: "Set Caption In Print Dialog"
    Date: Tue, 10 Aug 2004 13:32:19 -0700
    
    

    >The problem is that you're assuming the decrypted size will be the same
    as the encrypted size - it's not. You're only actually reading 15 bytes
    (and moreover, you're assuming they'll all be read in one go, which is a
    bad idea) but passing the encoding a buffer 16 bytes long.
    >*Always* use the return value of Stream.Read.

    Well, the problem was not in network operations and data send. It was in
    decrypted data.
    The problem was I did always get n to 16 bytes filled with zeros.

    So I've just swapped line:

    roundtrip = textConverter.GetString(fromEncrypt);

    with:

    roundtrip =
    textConverter.GetString(fromEncrypt).TrimEnd(Convert.ToChar(0));

    That way I always get the original data I've encrypted.

    Thanks btw.

    I have another question:
    When I'm sending this data trough network stream from client to server,
    I always create byte type array big enough to accept possible data from
    the client application.
    Do I have to always create it big enough to support any possible data
    size, or I can read everything in blocks and then merge it to a single
    byte array for example.

    This is how it is done by now:

    client code:
    (this is a connection thread code cut from the main code:)

    try
    {
    this.hostName = this.textBox2.Text;
    TcpClient client = new TcpClient(hostName, portNum);

    NetworkStream ns = client.GetStream();

    //size of response buffer
    byte[] bytes = new byte[1024];

    //using custom encryption class to encrypt given data
    bit256_RijndaelEnCryptorC enkripted = new bit256_RijndaelEnCryptorC();

    //encrypt string from the textbox
    encrypted.EnCrypt(this.textBox1.Text);

    //create data-to-be-sent buffer
    byte[] byteTime = new byte[encrypted.ReleaseEnCrypted).Length];

    //fill it
    byteTime = encrypted.ReleaseEnCrypted();

    //write it trough stream
    ns.Write(byteTime, 0, byteTime.Length);

    //receive a response
    int bytesRead = ns.Read(bytes, 0, bytes.Length);

    client.Close();

    }

    server code:

    TcpClient client = listener.AcceptTcpClient();

    NetworkStream ns = client.GetStream();

    //buffer for incoming data
    byte[] bytes = new byte[4096];

    //read data from the ns
    int bytesRead = ns.Read(bytes, 0, bytes.Length);

    //input data in a work buffer
    byte[] returned = new byte[bytesRead];

    for (int u=0; u<bytesRead; u++)
    {
        returned[u]=bytes[u];
    }

    //create data variables to be used in encryption process
    byte[] key = new byte[32];
    byte[] IV = new byte[16];
    byte[] encrypted = new byte[returned.Length-48];

    //strip usable data from the incoming stream
    for (int i=0; i<32; i++)
    {
        key[i]=returned[i];
    }
    for (int i=32; i<48; i++)
    {
        IV[i-32]=returned[i];
    }
    for (int i=48; i<returned.Length; i++)
    {
        encrypted[i-48]=returned[i];
    }

    //create a custom encryption (this time decryption) class
    bit256_RijndaelEnCryptorC dekripted = new
    bit256_RijndaelEnCryptorC(key,IV,encrypted);
    encrypted.DeCrypt();

    string result = encrypted.ReleaseDeCrypted();

    byte[] byteTime = Encoding.ASCII.GetBytes("server performed
    operations!");

    try
    {
        ns.Write(byteTime, 0, byteTime.Length);
        ns.Close();
    }
    client.Close();

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!


  • Next message: TabletTC: "Set Caption In Print Dialog"

    Relevant Pages

    • RE: Cannot decrypt files encrypted using Crypto API on a different
      ... previous message which uses the recipien't public key.) ... KEK (key encryption key) to protect the session key. ... embedded into your client app and server code). ... but what is the point to encrypt the data if ANYBODY can decrypt it (since ...
      (microsoft.public.platformsdk.security)
    • Re: SQL 7 Encryption Using Multiprotocol Network Library
      ... The client was configured via the Client Network ... Utility with Multiprotocol enabled. ... encryption" enabled and disabled without success. ... Did you run the Client Network Utility on the Client ...
      (microsoft.public.sqlserver.security)
    • Re: username and Password sent as clear text strings
      ... encryption of the traffic. ... SSL is used. ... client, it would seem like too much hassle for a low possibility hack. ... This is how all web applications on the planet work today by design. ...
      (Pen-Test)
    • Re: username and Password sent as clear text strings
      ... encryption of the traffic. ... SSL is used. ... client, it would seem like too much hassle for a low possibility hack. ... This is how all web applications on the planet work today by design. ...
      (Pen-Test)
    • Re: XP wireless questions ...setting encryption
      ... I never use the Linksys software for drivers. ... 802.1x authentication is only used with WPA encryption. ... wireless network. ... The manufacturers client program ...
      (alt.internet.wireless)