#1 2011-09-07 17:31:34

Julian
Member
Registered: 2011-03-06
Posts: 3

.NET compatible AES encryption/decryption

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"

Last edited by Julian (2011-09-07 19:33:24)

Offline

#2 2011-09-08 07:14:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: .NET compatible AES encryption/decryption

SynCrypto uses standard AES in ECB mode.
So it should be compatible with your source code.

Don't use AESFull* functions (this is a proprietary format for the remaining bytes), but the TAES object, with raw access to the data. Don't forget to pad your block with zeros.

Offline

Board footer

Powered by FluxBB