byte[] plainTextBytes;
RijndaelManaged aesAlgorithm;
ICryptoTransform encryptor;
MemoryStream memStream;
CryptoStream cryptoStream;
byte[] cipherTextBytes;

// Convert the plainText string into a byte array
plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);

// Prepare the AES algorithm with the correct settings
aesAlgorithm = new RijndaelManaged(); // Rijndael are the authors of AES and Microsoft use this as their AES class
aesAlgorithm.Mode = CipherMode.CBC; // Sendible uses CBC mode
aesAlgorithm.Padding = PaddingMode.PKCS7; // Sendible uses PKCS7 padding

// Create an aes encryptor with the key and iv
encryptor = aesAlgorithm.CreateEncryptor(this.sharedKeyBytes, this.sharedIVBytes);

// Create a memory stream so that the encrypted cipher text can be read out to memory
memStream = new MemoryStream();

// Create a crypto stream to aes encrypt the plain text through to the memory stream
cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write);

// Write the plain text to the crypto stream and ensure the final block is flushed
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

// Read the encrypted cipher text from the memory stream to an array of bytes
// and close both the memory and crypto streams. Also explicitly nullify all encryption
// objects
cipherTextBytes = memStream.ToArray();
cryptoStream = null;
memStream = null;
encryptor = null;
aesAlgorithm = null;