Esempio n. 1
0
        public void TestAesHmac()
        {
            byte[] hmac = AesStatic.GenerateKey();
            byte[] key  = AesStatic.GenerateKey();
            byte[] iv   = AesStatic.GenerateIV();

            Random random = new Random();

            byte[] plaintext = new byte[69854];
            random.NextBytes(plaintext);

            PacketBuffer ciphertext = PacketBuffer.CreateDynamic();

            AesStatic.EncryptWithHmac(plaintext, ciphertext, false, hmac, key);
            ciphertext.Position = 0;
            byte[] result = AesStatic.DecryptWithHmac(ciphertext, -1, hmac, key);

            CollectionAssert.AreEqual(plaintext, result);
        }
Esempio n. 2
0
        internal AesShaStream(Stream stream, byte[] key, CryptoStreamMode mode, CryptographicOperation operation) : base(stream, mode)
        {
            this.key = key ?? throw new ArgumentNullException(nameof(key));
            if (key.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(key), key.Length, "The AES key must have a length of 256 bit.");
            }
            if (operation == CryptographicOperation.Encrypt)
            {
                iv = AesStatic.GenerateIV();
            }
            else if (operation != CryptographicOperation.Decrypt)
            {
                throw new NotSupportedException("This stream does not support cryptographic operations other than encrypt and decrypt.");
            }

            this.operation = operation;
            csp            = Aes.Create();
            sha            = SHA256.Create();
        }
Esempio n. 3
0
        public void TestAes()
        {
            byte[] key = AesStatic.GenerateKey();
            Assert.IsNotNull(key);
            byte[] iv = AesStatic.GenerateIV();
            Assert.IsNotNull(iv);

            Random random = new Random();

            byte[] plaintext = new byte[45674];
            random.NextBytes(plaintext);

            byte[] ciphertext = AesStatic.Encrypt(plaintext, key, iv);
            Assert.IsNotNull(ciphertext);
            Assert.AreEqual(ciphertext.Length, Util.GetTotalSize(plaintext.Length, 16));

            byte[] decrypted = AesStatic.Decrypt(ciphertext, key, iv);
            Assert.IsNotNull(decrypted);
            CollectionAssert.AreEqual(plaintext, decrypted);
        }
Esempio n. 4
0
 private void btnAesGenerateIV_Click(object sender, EventArgs e)
 => tbAesIV.Text = Util.ToHexString(AesStatic.GenerateIV());