Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Subkey"/> class. Different data is encrypted using
        /// different variants of the master encryption key.
        /// </summary>
        /// <param name="masterKey">The master key.</param>
        /// <param name="headerSubkey">The header subkey.</param>
        public Subkey(AesKey masterKey, HeaderSubkey headerSubkey)
        {
            if (masterKey == null)
            {
                throw new ArgumentNullException("masterKey");
            }

            byte[] block = new byte[16];
            byte subKeyValue;
            switch (headerSubkey)
            {
                case HeaderSubkey.Hmac:
                    subKeyValue = 0;
                    break;
                case HeaderSubkey.Validator:
                    subKeyValue = 1;
                    break;
                case HeaderSubkey.Headers:
                    subKeyValue = 2;
                    break;
                case HeaderSubkey.Data:
                    subKeyValue = 3;
                    break;
                default:
                    throw new InternalErrorException("headerSubkey");
            }

            block[0] = subKeyValue;
            using (AesCrypto aesCrypto = new AesCrypto(masterKey))
            {
                _subKey = new AesKey(aesCrypto.Encrypt(block));
            }
        }
Пример #2
0
        public void AesCryptoToFromEncrypted()
        {
            var expected = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";

            using (var crypto = new AesCrypto("Passphrase", new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 }))
            {
                string encrypted = crypto.AesEncrypt(expected);
                string actual = crypto.AesDecrypt(encrypted, true);

                Assert.AreEqual(expected, actual);
            }
        }
Пример #3
0
        public static void TestDoubleDispose()
        {
            AesKey key = new AesKey();
            AesIV iv = new AesIV();

            AesCrypto crypto = new AesCrypto(key, iv, CipherMode.CBC, PaddingMode.None);
            crypto.Dispose();
            Assert.Throws<ObjectDisposedException>(() =>
            {
                crypto.CreateDecryptingTransform();
            });
            crypto.Dispose();
            Assert.Throws<ObjectDisposedException>(() =>
            {
                crypto.CreateDecryptingTransform();
            });
        }
Пример #4
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (_dataCrypto != null)
                {
                    _dataCrypto.Dispose();
                    _dataCrypto = null;
                }
                if (_reader != null)
                {
                    _reader.Dispose();
                    _reader = null;
                }
            }

            _disposed = true;
        }