コード例 #1
0
        private void CheckEncryptionResultBytes(byte[] encrypted, byte[] expected, IDictionary <string, string> context = null)
        {
            byte[] key        = DummyDataKeyProvider.ProduceContextualKey(context);
            var    iv         = new byte[EnvelopeCryptoProvider.IVBytes];
            var    cipherText = new byte[encrypted.Length - EnvelopeCryptoProvider.IVBytes];

            Array.Copy(encrypted, iv, EnvelopeCryptoProvider.IVBytes);
            Array.Copy(encrypted, EnvelopeCryptoProvider.IVBytes, cipherText, 0, cipherText.Length);

            using (var aes = new AesCryptoServiceProvider())
            {
                aes.KeySize = EnvelopeCryptoProvider.KeyBits;
                aes.Key     = key;
                aes.IV      = iv;
                aes.Mode    = EnvelopeCryptoProvider.Mode;
                aes.Padding = EnvelopeCryptoProvider.Padding;
                using (var inputStream = new MemoryStream(cipherText))
                {
                    using (ICryptoTransform decryptor = aes.CreateDecryptor())
                    {
                        using (var outputStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read))
                        {
                            byte[] decrypted = ReadAllBytes(outputStream);
                            decrypted.Should().Equal(expected);
                        }
                    }
                }
            }
        }
コード例 #2
0
        private void CheckEncryptionResultString(string encryptedString, string expected, IDictionary <string, string> context = null)
        {
            byte[] key     = DummyDataKeyProvider.ProduceContextualKey(context);
            var    pattern = new Regex(@"^\{\w+-\d+-\w+\}");
            Match  match   = pattern.Match(encryptedString);

            match.Should().NotBeNull();
            match.Value.Should().Be("{AES-256-CBC}");

            string content = encryptedString.Substring(match.Length);

            byte[] encrypted = Convert.FromBase64String(content);

            var iv         = new byte[EnvelopeCryptoProvider.IVBytes];
            var cipherText = new byte[encrypted.Length - EnvelopeCryptoProvider.IVBytes];

            Array.Copy(encrypted, iv, EnvelopeCryptoProvider.IVBytes);
            Array.Copy(encrypted, EnvelopeCryptoProvider.IVBytes, cipherText, 0, cipherText.Length);

            using (var aes = new AesCryptoServiceProvider())
            {
                aes.KeySize = EnvelopeCryptoProvider.KeyBits;
                aes.Key     = key;
                aes.IV      = iv;
                aes.Mode    = EnvelopeCryptoProvider.Mode;
                aes.Padding = EnvelopeCryptoProvider.Padding;
                using (var inputStream = new MemoryStream(cipherText))
                {
                    using (ICryptoTransform decryptor = aes.CreateDecryptor())
                    {
                        using (var outputStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read))
                        {
                            byte[] decrypted = ReadAllBytes(outputStream);
                            Encoding.UTF8.GetString(decrypted).Should().Be(expected);
                        }
                    }
                }
            }
        }