Beispiel #1
0
        public void RequiresPlaintext()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.Rsa15Parameters(null));

            Assert.AreEqual("plaintext", ex.ParamName);

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.RsaOaepParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.RsaOaep256Parameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128GcmParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A128GcmParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A192GcmParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A192GcmParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A256GcmParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A256GcmParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128CbcParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A128CbcParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128CbcParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A192CbcParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128CbcParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A256CbcParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128CbcPadParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A128CbcPadParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128CbcPadParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A192CbcPadParameters(Array.Empty <byte>(), null));

            ex = Assert.Throws <ArgumentNullException>(() => EncryptParameters.A128CbcPadParameters(null));
            Assert.AreEqual("plaintext", ex.ParamName);

            Assert.DoesNotThrow(() => EncryptParameters.A256CbcPadParameters(Array.Empty <byte>(), null));
        }
Beispiel #2
0
        public void EncryptAfterValidDate()
        {
            using RSA rsa = RSA.Create();

            KeyVaultKey key = new KeyVaultKey("test")
            {
                Key        = new JsonWebKey(rsa),
                Properties =
                {
                    ExpiresOn = DateTimeOffset.Now.AddDays(-1),
                },
            };

            RsaCryptographyProvider provider = new RsaCryptographyProvider(key.Key, key.Properties);

            byte[] plaintext             = Encoding.UTF8.GetBytes("test");
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => provider.Encrypt(EncryptParameters.RsaOaep256Parameters(plaintext), default));

            Assert.AreEqual($"The key \"test\" is not valid after {key.Properties.ExpiresOn.Value:r}.", ex.Message);
        }