public void ToBuffer_ShouldBeReversible()
        {
            using (var randomnessProvider = RandomNumberGenerator.Create())
            {
                // Arrange.
                var processor       = new SymmetricStringProcessor(randomnessProvider);
                var plaintextObject = "䆟`ಮ䷆ʘ‣⦸⏹ⰄͶa✰ṁ亡Zᨖ0༂⽔9㗰";

                using (var target = SecureSymmetricKey.New())
                {
                    // Arrange.
                    var ciphertext = processor.Encrypt(plaintextObject, target);

                    // Act.
                    using (var buffer = target.ToBuffer())
                    {
                        using (var parityKey = SecureSymmetricKey.FromBuffer(buffer))
                        {
                            // Arrange.
                            var result = processor.Decrypt(ciphertext, parityKey);

                            // Assert.
                            result.Should().Be(plaintextObject);
                        }
                    }
                }
            }
        }
        private static void Encrypt_ShouldBeReversible_UsingSecureSymmetricKey(SymmetricAlgorithmSpecification algorithm)
        {
            using (var randomnessProvider = RandomNumberGenerator.Create())
            {
                // Arrange.
                var target          = new SymmetricStringProcessor(randomnessProvider);
                var plaintextObject = "䆟`ಮ䷆ʘ‣⦸⏹ⰄͶa✰ṁ亡Zᨖ0༂⽔9㗰";

                using (var key = SecureSymmetricKey.New(algorithm))
                {
                    // Act.
                    var ciphertext = target.Encrypt(plaintextObject, key);

                    // Assert.
                    ciphertext.Should().NotBeNullOrEmpty();
                    ciphertext.Count(value => value == 0x00).Should().NotBe(ciphertext.Length);

                    // Act.
                    var plaintext = target.Decrypt(ciphertext, key);

                    // Assert.
                    plaintext.Should().NotBeNullOrEmpty();
                    plaintext.Should().Be(plaintextObject);
                }
            }
        }
Esempio n. 3
0
        private static void Encrypt_ShouldBeReversible_UsingSecureSymmetricKey(SymmetricAlgorithmSpecification algorithm, SecureSymmetricKeyDerivationMode derivationMode)
        {
            using (var randomnessProvider = RandomNumberGenerator.Create())
            {
                // Arrange.
                var binarySerializer = new BinaryPassThroughSerializer();
                var target           = new SymmetricProcessor <Byte[]>(randomnessProvider, binarySerializer);
                var plaintextObject  = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };

                using (var key = SecureSymmetricKey.New(algorithm, derivationMode))
                {
                    // Act.
                    var ciphertextResult = target.Encrypt(plaintextObject, key);

                    // Assert.
                    ciphertextResult.Should().NotBeNullOrEmpty();
                    ciphertextResult.Count(value => value == 0x00).Should().NotBe(ciphertextResult.Length);

                    // Act.
                    var plaintextResult = target.Decrypt(ciphertextResult, key);

                    // Assert.
                    plaintextResult.Should().NotBeNullOrEmpty();
                    plaintextResult.Length.Should().Be(plaintextObject.Length);
                    plaintextResult.Count(value => value == 0x00).Should().NotBe(plaintextResult.Length);

                    for (var i = 0; i < plaintextResult.Length; i++)
                    {
                        // Assert.
                        plaintextResult[i].Should().Be(plaintextObject[i]);
                    }
                }
            }
        }
        public void EncryptToBase64String_ShouldBeReversible()
        {
            using (var randomnessProvider = RandomNumberGenerator.Create())
            {
                // Arrange.
                var target          = new SymmetricStringProcessor(randomnessProvider);
                var algorithm       = SymmetricAlgorithmSpecification.Aes128Cbc;
                var plaintextObject = "aZ09`ಮ";

                using (var key = SecureSymmetricKey.New(algorithm))
                {
                    // Act.
                    var ciphertext = target.EncryptToBase64String(plaintextObject, key);

                    // Assert.
                    ciphertext.Should().NotBeNullOrEmpty();

                    // Act.
                    var result = target.DecryptFromBase64String(ciphertext, key);

                    // Assert.
                    result.Should().NotBeNullOrEmpty();
                    result.Should().Be(plaintextObject);
                }
            }
        }
 public void New_ShouldReturnValidKey_ForValidArguments()
 {
     // Arrange.
     using (var target = SecureSymmetricKey.New())
     {
         // Assert.
         target.Should().NotBeNull();
     }
 }
        public void New_ShouldRaiseArgumentOutOfRangeException_ForUnspecifiedDerivationMode()
        {
            // Arrange.
            var algorithm      = SymmetricAlgorithmSpecification.Aes256Cbc;
            var derivationMode = SecureSymmetricKeyDerivationMode.Unspecified;

            // Act.
            var action = new Action(() =>
            {
                using (var target = SecureSymmetricKey.New(algorithm, derivationMode))
                {
                    return;
                }
            });

            // Assert.
            action.Should().Throw <ArgumentOutOfRangeException>();
        }
        public void ToBuffer_ShouldReturnValidResult()
        {
            // Arrange.
            var secureKeyLengthInBytes = 416;

            using (var target = SecureSymmetricKey.New())
            {
                // Act.
                using (var buffer = target.ToBuffer())
                {
                    buffer.Access(plaintext =>
                    {
                        // Assert.
                        plaintext.Should().NotBeNullOrEmpty();
                        plaintext.Length.Should().Be(secureKeyLengthInBytes);
                        plaintext.Count(value => value == 0x00).Should().NotBe(plaintext.Length);
                    });
                }
            }
        }
        public void New_ShouldRaiseArgumentOutOfRangeException_ForInvalidLengthKeySource()
        {
            // Arrange.
            var algorithm      = SymmetricAlgorithmSpecification.Aes256Cbc;
            var derivationMode = SecureSymmetricKeyDerivationMode.Truncation;
            var keyLength      = 3;

            using (var keySource = new PinnedBuffer(keyLength))
            {
                // Act.
                var action = new Action(() =>
                {
                    using (var target = SecureSymmetricKey.New(algorithm, derivationMode, keySource))
                    {
                        return;
                    }
                });

                // Assert.
                action.Should().Throw <ArgumentException>();
            }
        }