views:

1231

answers:

1

Is there a simple ASP.NET (.VB) available for AES-encrypting?

here is a link to c# one but its complicate one having pretty much parameters, for example salt. [http://www.gutgames.com/post/AES-Encryption-in-C.aspx]

I need a simple one that works together with GOOGLEAS3 Class that is called easily like this:

var key:ByteArray = Hex.toArray("1234567890abcdef");
var pt:ByteArray = Hex.toArray( Hex.fromString("aaaaaaaaaaaaaaaaaaaa"));
var aes:AESKey = new AESKey(key);
aes.encrypt(pt);
var out:String = Hex.fromArray(pt).toUpperCase();
A: 

AES is built into the framework, as the Aes class in System.Security.Cryptography. There are two concrete implementations, one managed, one using the Windows Crypto Service Provider (faster, but not portable to other platforms). There's also an implementation of Rijndael, from which AES derives.

Remember you need an initialization vector, as well as the key to encrypt/decrypt, as it's a block cipher. If you encrypt without setting one a random one will be used, but you will need to store and retrieve it for decryption.

Example Code: (lifted from chapter 6 of my forthcoming book grin)

static byte[] Encrypt(byte[] clearText, byte[] key, byte[] iv)
{
    // Create an instance of our encyrption algorithm.
    RijndaelManaged rijndael = new RijndaelManaged();

    // Create an encryptor using our key and IV
    ICryptoTransform transform = rijndael.CreateEncryptor(key, iv);

    // Create the streams for input and output
    MemoryStream outputStream = new MemoryStream();
    CryptoStream inputStream = new CryptoStream(
        outputStream,
        transform,
        CryptoStreamMode.Write);

    // Feed our data into the crypto stream.
    inputStream.Write(clearText, 0, clearText.Length);

    // Flush the crypto stream.
    inputStream.FlushFinalBlock();

    // And finally return our encrypted data.
    return outputStream.ToArray();
}

and to decrypt

static byte[] Decyrpt(byte[] clearText, byte[] key, byte[] iv)
{
    // Create an instance of our encryption algorithm.
    RijndaelManaged rijndael = new RijndaelManaged();

    // Create an decryptor using our key and IV
    ICryptoTransform transform = rijndael.CreateDecryptor(key, iv);

    // Create the streams for input and output
    MemoryStream outputStream = new MemoryStream();
    CryptoStream inputStream = new CryptoStream(
        outputStream,
        transform,
        CryptoStreamMode.Write);

    // Feed our data into the crypto stream.
    inputStream.Write(clearText, 0, clearText.Length);

    // Flush the crypto stream.
    inputStream.FlushFinalBlock();

    // And finally return our decrypted data.
    return outputStream.ToArray();
}

replace the RijndaelManaged class with one of the AES classes and a suitable key.

blowdart
I'm trying to do this, and I keep getting a key size violation. Here's my key:string verySecureKey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";When I do:var algo = new System.Security.Cryptography.RijndaelManaged();keyArray = UTF8Encoding.UTF8.GetBytes(verySecureKey);algo.Key = keyArray;I get a key violation on the last line (64 bit instead of 32) - but this works fine in AS3Crypto. Thoughts?
aronchick