Ejemplo n.º 1
0
        public void TestConstructorWithBadArguments()
        {
            SymmetricAlgorithm algorithm;
            ICryptoTransform   transform = null;

            try
            {
                algorithm      = New <Aes>();
                algorithm.Mode = CipherMode.CBC;
                Assert.Throws <ArgumentException>(() => transform = new CounterModeCryptoTransform(New <Aes>(), 0, 0));

                algorithm         = New <Aes>();
                algorithm.Mode    = CipherMode.ECB;
                algorithm.Padding = PaddingMode.PKCS7;
                Assert.Throws <ArgumentException>(() => transform = new CounterModeCryptoTransform(algorithm, 0, 0));

                algorithm         = New <Aes>();
                algorithm.Mode    = CipherMode.ECB;
                algorithm.Padding = PaddingMode.None;
                Assert.DoesNotThrow(() => transform = new CounterModeCryptoTransform(algorithm, 0, 0));
            }
            finally
            {
                if (transform != null)
                {
                    transform.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        public void TestCanReuseTransform()
        {
            SymmetricAlgorithm algorithm = New <Aes>();

            algorithm.Mode    = CipherMode.ECB;
            algorithm.Padding = PaddingMode.None;
            using (ICryptoTransform transform = new CounterModeCryptoTransform(algorithm, 0, 0))
            {
                Assert.That(transform.CanReuseTransform);
            }
        }
        /// <summary>
        /// Sets the encryption keys and starts to encrypt the communication
        /// with the module.
        /// </summary>
        /// <param name="key">Session key.</param>
        /// <param name="txNonce">TX nonce used as prefix of the counter block.
        /// </param>
        /// <param name="rxNonce">RX nonce used as prefix of the counter block.
        /// </param>
        public void SetEncryptionKeys(byte[] key, byte[] txNonce, byte[] rxNonce)
        {
            Aes128CounterMode aesEncryption = new Aes128CounterMode(GetCounter(txNonce, 1));

            encryptor = (CounterModeCryptoTransform)aesEncryption.CreateEncryptor(key, null);

            Aes128CounterMode aesDecryption = new Aes128CounterMode(GetCounter(rxNonce, 1));

            decryptor = (CounterModeCryptoTransform)aesDecryption.CreateEncryptor(key, null);

            encrypt = true;
        }
Ejemplo n.º 4
0
        public void TestTransformBlockWithBadArgument()
        {
            SymmetricAlgorithm algorithm = New <Aes>();

            algorithm.Mode    = CipherMode.ECB;
            algorithm.Padding = PaddingMode.None;
            using (ICryptoTransform transform = new CounterModeCryptoTransform(algorithm, 0, 0))
            {
                Assert.Throws <ArgumentException>(() => transform.TransformBlock(new byte[transform.InputBlockSize + 1], 0, transform.InputBlockSize + 1, new byte[transform.InputBlockSize + 1], 0));
                Assert.DoesNotThrow(() => transform.TransformBlock(new byte[transform.InputBlockSize], 0, transform.InputBlockSize, new byte[transform.InputBlockSize], 0));
            }
        }