// Coerce key and iv into legal format for specificed algorithm then encrypt private static byte[] LegalKeyEncrypt(SymmetricAlgorithm algorithm, byte[] data, int startIndex, int length, byte[] key, byte[] iv) { byte[] rgbKey = algorithm.GetLegalKey(key); byte[] rgbIV = algorithm.GetLegalIV(iv); return algorithm.Encrypt(data, startIndex, length, rgbKey, rgbIV); }