#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"

Board footer

Powered by FluxBB