#1 mORMot 1 » .NET compatible AES encryption/decryption » 2011-09-07 17:31:34

Julian
Replies: 1

Hello,
I'm trying to make my client communicate with a .NET service. For this to work, i have to send some encrypted data across the wire. Currently i'm using a third party lib to encrpyt/decrypth data on my Delphi side and i'm trying to replace it with SynCrypto. However, i'm but lost... The .NET code looks similar to this simplified example:

private void button1_Click(object sender, EventArgs e)
        {
            // define the key
            const string keyString = "12345678901234561234567890123456";
            ASCIIEncoding encoding = new ASCIIEncoding();
            Byte[] key = encoding.GetBytes(keyString);

            //  define the input
            const string inputString = "1234567890123456";
            Byte[] input = encoding.GetBytes(inputString);

            //  define the IV
            const string ivString = "0000000000000000";
            //System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Byte[] iv = encoding.GetBytes(ivString);

            //  TODO: introduce Using statement
            RijndaelManaged myAES = new RijndaelManaged
            {
                KeySize = 128,
                BlockSize = 128,
                Key = key,
                //IV = iv,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.Zeros
            };

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = myAES.CreateEncryptor(myAES.Key, myAES.IV);

            // Define memory stream which will be used to hold encrypted data.
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // Define cryptographic stream (always use Write mode for encryption).
                CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);

                // Start encrypting.
                cryptoStream.Write(input, 0, input.Length);

                // Finish encrypting.
                cryptoStream.FlushFinalBlock();

                // Convert our encrypted data from a memory stream into a byte array.
                byte[] cipherTextBytes = memoryStream.ToArray();

                // Close both streams.
                memoryStream.Close();
                cryptoStream.Close();

                // Convert encrypted data into a readable format.
                //string cipherText = Convert.ToBase64String(cipherTextBytes);
                string cipherText = BitConverter.ToString(cipherTextBytes);

                // Return encrypted string.
                textBox1.Text = cipherText;
            }
        }

As you can see, it is AES algorythm in ECB mode. You can ignore the IV as it is not used.

The question is: How, if possible, to make the AES implementation in SynCrypto to produce the same result as .NETs RijndaelManaged? I've checked with PHPs crypt and it worked OK, however all my tries with SynCrypto failed sad

Edit: the RijndaelManaged class supports PaddingMode of "None", "Zeros", "ANSIX923", "ISO10126" and "PKCS7"

#2 Re: Source Code repository » Fast JSON (un)serialization » 2011-03-11 17:40:18

Yeah, moving the JSON serializing in a stand alone unit is not a big deal - thanks to the smart-linking.

I'm planing to play with the ideas over the weekend, but first i'm planing to play a bit with the ObjectToJSON and JSONToObject functions.
Right now, ObjectToJSON can serialize TStrings, but it is ignoring the attached objects (if any). I was thinking to use the AddObject() of TStrings to mimic a dictionary or something like this.

#3 Re: Source Code repository » Fast JSON (un)serialization » 2011-03-06 21:33:40

This looks great!

As JSON becoming more and more popular (and used) for so much things nowadays, it possible to move the JSON parsing/serialization code to a dedicated unit?

For example, I'm using JSON to store large number of objects into a SynBigTable. Some kind of key/value database, storing JSON documents. All works great, but i had to use an external JSON library.

And while we're on the topic, why not evolving SynBigTable to some kind of a document DB, similar to RavenDB (http://ravendb.net/).

Thank you for the great bits of code,
Julian

Board footer

Powered by FluxBB