Example #1
0
        public void GetDecryptionIV_TripleDesUri()
        {
            EncryptedXml  ex            = new EncryptedXml();
            EncryptedData encryptedData = new EncryptedData();

            encryptedData.CipherData = new CipherData(new byte[16]);
            Assert.Equal(8, ex.GetDecryptionIV(encryptedData, EncryptedXml.XmlEncTripleDESUrl).Length);
        }
Example #2
0
        public void GetDecryptionIV_StringNullWithoutEncryptionMethod()
        {
            EncryptedXml  ex            = new EncryptedXml();
            EncryptedData encryptedData = new EncryptedData();

            encryptedData.CipherData = new CipherData(new byte[16]);
            Assert.Throws <CryptographicException>(() => ex.GetDecryptionIV(encryptedData, null));
        }
Example #3
0
        public void GetDecryptionIV_InvalidAlgorithmUri()
        {
            EncryptedXml  ex            = new EncryptedXml();
            EncryptedData encryptedData = new EncryptedData();

            encryptedData.CipherData = new CipherData(new byte[16]);
            Assert.Throws <CryptographicException>(() => ex.GetDecryptionIV(encryptedData, "invalid"));
        }
Example #4
0
        public void GetDecryptionIV_StringNull()
        {
            EncryptedXml  ex            = new EncryptedXml();
            EncryptedData encryptedData = new EncryptedData();

            encryptedData.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
            encryptedData.CipherData       = new CipherData(new byte[16]);
            Assert.Equal(new byte[16], ex.GetDecryptionIV(encryptedData, null));
        }
Example #5
0
        public void GetDecryptionIV_StringNull()
        {
            // Added EncryptionMethod and CipherData to avoid a CryptographicException

            EncryptedXml  ex            = new EncryptedXml();
            EncryptedData encryptedData = new EncryptedData();

            encryptedData.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
            encryptedData.CipherData       = new CipherData(new byte[16]);
            Assert.Null(ex.GetDecryptionIV(encryptedData, null));
        }
Example #6
0
        public void GetDecryptionIV_StringNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.IsNull(ex.GetDecryptionIV(new EncryptedData(), null));
        }
Example #7
0
        public void GetDecryptionIV_EncryptedDataNull()
        {
            EncryptedXml ex = new EncryptedXml();

            ex.GetDecryptionIV(null, EncryptedXml.XmlEncAES128Url);
        }
Example #8
0
        public void GetDecryptionIV_EncryptedDataNull()
        {
            EncryptedXml ex = new EncryptedXml();

            Assert.Throws <ArgumentNullException>(() => ex.GetDecryptionIV(null, EncryptedXml.XmlEncAES128Url));
        }
Example #9
0
        private bool TryDecrypt(EncryptedType encryptedType, out byte[] plainText)
        {
            var xml       = new EncryptedXml();
            var keys      = GetSecurityKeys(encryptedType);
            var algorithm = encryptedType.EncryptionMethod.KeyAlgorithm;

            foreach (var key in keys)
            {
                var crypto = GetCrypto(key);
                if (!crypto.IsSupportedAlgorithm(algorithm, key))
                {
                    continue;
                }
                var symmetric = null as SymmetricAlgorithm;
                try
                {
                    if (encryptedType is EncryptedData encryptedData)
                    {
                        if (!(key is SymmetricSecurityKey symmetricKey))
                        {
                            continue;
                        }
                        symmetric    = crypto.CreateSymmetricAlgorithm(symmetricKey, algorithm);
                        symmetric.IV = xml.GetDecryptionIV(encryptedData, algorithm);
                        var pt = xml.DecryptData(encryptedData, symmetric);
                        plainText = pt;
                        return(true);
                    }

                    if (encryptedType is EncryptedKey encryptedKey)
                    {
                        var pt = null as byte[];
                        var keyWrapAlgorithm = encryptedKey.EncryptionMethod.KeyAlgorithm;
                        if (crypto.IsSupportedAlgorithm(algorithm, key))
                        {
                            var keyWrap = crypto.CreateKeyWrapProviderForUnwrap(key, encryptedKey.EncryptionMethod.KeyAlgorithm);
                            try
                            {
                                pt        = keyWrap.UnwrapKey(encryptedKey.CipherData.CipherValue);
                                plainText = pt;
                                return(true);
                            }
                            finally
                            {
                                crypto.ReleaseKeyWrapProvider(keyWrap);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (symmetric != null)
                    {
                        crypto.ReleaseSymmetricAlgorithm(symmetric);
                        symmetric = null;
                    }
                }
            }
            return(Out.False(out plainText));
        }