Exemplo n.º 1
0
        /// <summary>Create a new AES encryptor with the provided key.</summary>
        /// <param name="key">Encryption key to use.</param>
        /// <param name="allocator">Allocator to use for allocating keys.</param>
        public CryptoAES(byte[] key, Allocator allocator)
        {
            // Validate
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key), "Key is null");
            }
            else if (allocator == null)
            {
                throw new ArgumentNullException(nameof(allocator), "No allocator");
            }
            else if (key.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(key), string.Format(
                                                          "Key length {0} is not 32", key.Length
                                                          ));
            }

            // Initialize
            Allocator = allocator;
            Key       = key;
            IV        = Allocator.CreateIV(16);
            Disposed  = false;

            // Create AES
            Aes         = Aes.Create();
            Aes.Padding = PaddingMode.PKCS7;
            Aes.Mode    = CipherMode.CBC;
            //Aes.FeedbackSize = 0;
            Aes.BlockSize = 128;
            Aes.KeySize   = 256;
            Aes.Key       = Key;
            Aes.IV        = IV;
        }