I wrote one up for testing earlier, so I'll post it below.  It's a 
bit long, and definitely not well structured, but it does work.

As C# uses Rijndael, you'll need to make sure to restrict it to 
something that will work with the actionscript version.  
Specifically, the block size must be 128.  The key size can be 128, 
192, or 256.

It actually doesn't look like they (c# and Hurlant's crypto lib) 
have matching padding schemes, but that's easy to fix.  Here's some 
ActionScript code that implements (I hope -- untested) the zero's 
padding scheme, which is probably the least secure of them, but is 
the easiest implement:

package com.hurlant.crypto.symmetric
{
    import flash.utils.ByteArray;
    
    public class ZeroPad implements IPad
    {
        private var blockSize:uint;
        
        public function PKCS5(blockSize:uint=0) {
            this.blockSize = blockSize;
        }
        
        public function pad(a:ByteArray):void {
            var c:uint = blockSize-a.length%blockSize;
            for (var i:uint=0;i<c;i++){
                a[a.length] = 0;
            }
        }
        public function unpad(a:ByteArray):void {
            var c:uint = a.length%blockSize;
            if (c!=0) throw new Error("ZeroPad::unpad: 
ByteArray.length isn't a multiple of the blockSize");
            c = a.length-1;
            while (a[c] == 0) {
               a.length--;
               c--;
            }
        }

        public function setBlockSize(bs:uint):void {
            blockSize = bs;
        }

    }
}

That's about it for the actionscript part.  If the above items 
match, everything should work great for you.

Details about the padding scheme's are here:

http://technet.microsoft.com/en-us/library/system.security.cryptography.paddingmode(VS.80).aspx

I would recommend implementing the ISO10126 padding scheme as it's 
going to be the most secure.

And here's the more offtopic piece:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace AesTest
{
    class Program
    {
        static string getStringRep(byte[] arr)
        {
            string result = "";
            for (int i = 0; i < arr.Length; i++)
            {
                result += arr[i].ToString("x2");
            }
            return result;
        }

        static void Main(string[] args)
        {
            RijndaelManaged aes = new RijndaelManaged();

            byte[] key = { 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 
0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 
0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 
0x34, 0x12, 0x34 };
            byte[] IV = { 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 
0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34 };

            byte[] ciphertext;
            byte[] plaintext_output;
            byte[] plaintext = { 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 
0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34, 0x12, 0x34 };

            // set the various parameters
            // 1) make sure it's compatible with AES which requires 
128 bit block sizes
            // 2) key size can be 128, 192, or 256 (automatically 
detected when setting the key)
            // 3) Set the padding mode to something useful 
(ANSIX923, ISO10126, None, PKCS7, Zeros)
            aes.BlockSize = 128;
            aes.KeySize = 256;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.ANSIX923;
            aes.Key = key;
            aes.IV = IV;

            // Create an encryptor object
            ICryptoTransform encryptor = aes.CreateEncryptor(key, 
IV);

            // Encrypt the data
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
encryptor, CryptoStreamMode.Write);

            // Write all data to the crypto stream and flush it.
            csEncrypt.Write(plaintext, 0, plaintext.Length);
            csEncrypt.FlushFinalBlock();

            // Get ciphertext array of bytes.
            ciphertext = msEncrypt.ToArray();

            // purposefully change the padding mode so that I can 
see what actually
            // the actual padding bits were
            aes.Padding = PaddingMode.None;

            // create a decryption stream
            CryptoStream csDecrypt = new CryptoStream(
                new MemoryStream(ciphertext),
                aes.CreateDecryptor(),
                CryptoStreamMode.Read);

            plaintext_output = new byte[ciphertext.Length];

            // Read the data out of the crypto stream.
            csDecrypt.Read(plaintext_output, 0, 
plaintext_output.Length);

            // Display the original data and the decrypted data.
            Console.WriteLine("Original:   {0}", 
getStringRep(plaintext));
            Console.WriteLine("Ciphertext: {0}", 
getStringRep(ciphertext));
            Console.WriteLine("Round Trip: {0}", 
getStringRep(plaintext_output));
        }
    }
}

I hope that helps.

--Kaleb



--- In flexcoders@yahoogroups.com, "bhaq1972" <[EMAIL PROTECTED]> wrote:
>
> Hi Kaleb (or anyone else)
> 
> This is slightly off topic but can you point me to a c# example 
that 
> works with Metal Hurlat. 
> 
> thanks
> Bod


Reply via email to