コード例 #1
0
        public void RequiresPlaintext()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => EncryptOptions.Rsa15Options(null));

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

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

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

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

            Assert.DoesNotThrow(() => EncryptOptions.A128GcmOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A192GcmOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A256GcmOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A128CbcOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A192CbcOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A256CbcOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A128CbcPadOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A192CbcPadOptions(Array.Empty <byte>(), null));

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

            Assert.DoesNotThrow(() => EncryptOptions.A256CbcPadOptions(Array.Empty <byte>(), null));
        }
コード例 #2
0
        public void EncryptAfterValidDate()
        {
            using Aes aes = Aes.Create();

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

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

            byte[]         iv      = { 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30, 0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41 };
            EncryptOptions options = EncryptOptions.A128CbcOptions(Encoding.UTF8.GetBytes("Single block msg"), iv);

            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => provider.Encrypt(options, default));

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