Esempio n. 1
0
        public string Decrypt(string input)
        {
            byte[] plainText;

            try
            {
                var inputBytes = Convert.FromBase64String(input);

                var hashLength = inputBytes[0];
                var hash       = new byte[hashLength - 1 + 1];
                Buffer.BlockCopy(inputBytes, 1, hash, 0, hashLength);
                var cipherText = new byte[inputBytes.Length - hashLength - 2 + 1];
                Buffer.BlockCopy(inputBytes, hashLength + 1, cipherText, 0, cipherText.Length);
                var compareHash = _mHashProvider.Hash(CombineBytes(_key, cipherText));

                if (!CommonUtil.CompareBytes(hash, compareHash))
                {
                    throw new ApplicationException("input value was improperly signed or tampered with");
                }

                plainText = _mSymmetricCryptoProvider.Decrypt(cipherText);
            }
            catch (Exception)
            {
                throw new ApplicationException("decrypt method failed");
            }

            return(Encoding.Unicode.GetString(plainText));
        }
Esempio n. 2
0
        public string Decrypt(string data)
        {
            var dataByte  = data.ConvertToByteArray();
            var decrypted = crypto.Decrypt(dataByte);

            return(decrypted.ConvertToString());
        }
Esempio n. 3
0
        private string DecryptAndVerify(string input)
        {
            byte[] plainText = null;

            try
            {
                byte[] inputBytes = Convert.FromBase64String(input);

                byte   hashLength = inputBytes[0];
                byte[] hash       = new byte[hashLength];
                Buffer.BlockCopy(inputBytes, 1, hash, 0, hashLength);
                byte[] cipherText = new byte[inputBytes.Length - hashLength - 1];
                Buffer.BlockCopy(inputBytes, hashLength + 1, cipherText, 0, cipherText.Length);
                byte[] compareHash = hashProvider.Hash(CombineBytes(key, cipherText));

                if (!CommonUtil.CompareBytes(hash, compareHash))
                {
                    throw new InvalidQueryStringException("Query string was improperly signed or tampered with");
                }

                plainText = symmetricCryptoProvider.Decrypt(cipherText);
            }
            catch (Exception)
            {
                throw new InvalidQueryStringException();
            }

            return(Encoding.Unicode.GetString(plainText));
        }
Esempio n. 4
0
        internal static byte[] DecryptSymmetric(string symmetricInstance, byte[] ciphertext, ConfigurationContext context)
        {
            ArgumentValidation.CheckForNullReference(symmetricInstance, "symmetricInstance");
            ArgumentValidation.CheckForEmptyString(symmetricInstance, "symmetricInstance");

            SymmetricCryptoProviderFactory factory           = new SymmetricCryptoProviderFactory(context);
            ISymmetricCryptoProvider       symmetricProvider = factory.CreateSymmetricCryptoProvider(symmetricInstance);

            return(symmetricProvider.Decrypt(ciphertext));
        }
Esempio n. 5
0
        private static string Decrypt(string cipherText)
        {
            var cipherBytes = Convert.FromBase64String(cipherText);
            var src         = new MemoryStream(cipherBytes);
            var dst         = new MemoryStream();

            Aes.Decrypt(dst.AsUnclosable(), src, (int)src.Length);
            var plainText = Encoding.UTF8.GetString(dst.GetBuffer(), 0, (int)dst.Length);

            return(plainText);
        }
Esempio n. 6
0
        internal static string DecryptSymmetric(ISymmetricCryptoProvider provider, string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(ciphertextBase64))
            {
                throw new ArgumentException("The value can not be a null or empty string.", "ciphertextBase64");
            }
            byte[] ciphertext = Convert.FromBase64String(ciphertextBase64);
            byte[] bytes      = provider.Decrypt(ciphertext);
            string str        = Encoding.Unicode.GetString(bytes);

            CryptographyUtility.GetRandomBytes(bytes);
            return(str);
        }
 public IMessage Extract(MessageId id, ISymmetricCryptoProvider cryptoProvider, ProtocolId protocol)
 {
     using (var dst = new MemoryStream())
     {
         var dstWrap = dst.AsUnclosable();
         cdataWrap.Seek(0, SeekOrigin.Begin);
         cryptoProvider.IV = iv;
         cryptoProvider.Decrypt(dstWrap, cdataWrap, (int)cdata.Length);
         dstWrap.Seek(0, SeekOrigin.Begin);
         var msg = MessageFactory.CreateMessage(id);
         msg.Load(dstWrap, protocol);
         return(msg);
     }
 }
Esempio n. 8
0
        internal static string DecryptSymmetric(ISymmetricCryptoProvider provider, string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(ciphertextBase64))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "ciphertextBase64");
            }

            byte[] cipherTextBytes = Convert.FromBase64String(ciphertextBase64);
            byte[] decryptedBytes  = provider.Decrypt(cipherTextBytes);
            string decryptedString = UnicodeEncoding.Unicode.GetString(decryptedBytes);

            CryptographyUtility.GetRandomBytes(decryptedBytes);

            return(decryptedString);
        }
Esempio n. 9
0
        /// <summary>
        /// Decrypts a cipher text using a specified symmetric cryptography provider.
        /// </summary>
        /// <param name="symmetricInstance">A symmetric instance from configuration.</param>
        /// <param name="ciphertext">The cipher text for which you want to decrypt.</param>
        /// <returns>The resulting plain text.</returns>
        public static byte[] DecryptSymmetric(string symmetricInstance, byte[] ciphertext)
        {
            if (string.IsNullOrEmpty(symmetricInstance))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "symmetricInstance");
            }

            try
            {
                SymmetricCryptoProviderFactory factory           = new SymmetricCryptoProviderFactory(ConfigurationSourceFactory.Create());
                ISymmetricCryptoProvider       symmetricProvider = factory.Create(symmetricInstance);

                return(symmetricProvider.Decrypt(ciphertext));
            }
            catch (ConfigurationErrorsException configurationException)
            {
                TryLogSymmetricConfigurationError(configurationException, symmetricInstance);

                throw;
            }
        }
 /// <summary>
 /// Decrypts the data passed to this method according to the correct symmetric cryptographic
 /// algoritm as defined in configuration
 /// </summary>
 /// <param name="ciphertext">Encrypted data to be decrypted</param>
 /// <returns>Decrypted data</returns>
 public byte[] Decrypt(byte[] ciphertext)
 {
     return(symmetricCrytoProvider.Decrypt(ciphertext));
 }
Esempio n. 11
0
        /// <summary>
        /// Decrypts the data passed to this method according to the correct symmetric cryptographic
        /// algoritm as defined in configuration
        /// </summary>
        /// <param name="ciphertext">Encrypted data to be decrypted</param>
        /// <returns>Decrypted data</returns>
        public byte[] Decrypt(byte[] ciphertext)
        {
            ISymmetricCryptoProvider provider = CreateProvider();

            return(provider.Decrypt(ciphertext));
        }
        /// <summary>
        /// Decrypts a cipher text using a specified symmetric cryptography provider.
        /// </summary>
        /// <param name="symmetricInstance">A symmetric instance from configuration.</param>
        /// <param name="ciphertext">The cipher text for which you want to decrypt.</param>
        /// <returns>The resulting plain text.</returns>
        public override byte[] DecryptSymmetric(string symmetricInstance, byte[] ciphertext)
        {
            ISymmetricCryptoProvider symmetricProvider = GetSymmetricCryptoProvider(symmetricInstance);

            return(symmetricProvider.Decrypt(ciphertext));
        }
        internal static string DecryptSymmetric(ISymmetricCryptoProvider provider, string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(ciphertextBase64)) throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "ciphertextBase64");

            byte[] cipherTextBytes = Convert.FromBase64String(ciphertextBase64);
            byte[] decryptedBytes = provider.Decrypt(cipherTextBytes);
            string decryptedString = UnicodeEncoding.Unicode.GetString(decryptedBytes);
            CryptographyUtility.GetRandomBytes(decryptedBytes);

            return decryptedString;
        }
Esempio n. 14
0
 internal static string DecryptSymmetric(ISymmetricCryptoProvider provider, string ciphertextBase64)
 {
     if (string.IsNullOrEmpty(ciphertextBase64))
     {
         throw new ArgumentException("The value can not be a null or empty string.", "ciphertextBase64");
     }
     byte[] ciphertext = Convert.FromBase64String(ciphertextBase64);
     byte[] bytes = provider.Decrypt(ciphertext);
     string str = Encoding.Unicode.GetString(bytes);
     CryptographyUtility.GetRandomBytes(bytes);
     return str;
 }