Example #1
0
        internal static string DecryptValue(string value)
        {
            if (String.IsNullOrWhiteSpace(value))
            {
                return(String.Empty);
            }

            using (var symmetricProvider = new Symmetric(SymmetricProvider.Rijndael, false))
            {
                string result;

                try
                {
                    symmetricProvider.IntializationVector = IV;
                    result = symmetricProvider.Decrypt(new Data(value.FromBase64()), SymmetricKey).ToString();
                }
                catch (FormatException)
                {
                    // return the value as is
                    result = value;
                }
                catch (CryptographicException)
                {
                    // return the value as is
                    result = value;
                }

                return(result);
            }
        }
Example #2
0
        private void Decrypt(IEnumerable <ClientSettings> value)
        {
            var symetricProvider = new Symmetric(SymmetricProvider.Rijndael, false)
            {
                IntializationVector = _iv
            };

            foreach (var settings in value)
            {
                if (settings.Password.Length == 0)
                {
                    continue;
                }

                try
                {
                    settings.Password = symetricProvider.Decrypt(new Data(settings.Password.FromBase64()), _symmetricKey).ToString();
                }
                catch (FormatException)
                {
                    Logger.WarnFormat(Constants.ClientNameFormat, settings.Name, "Failed to decrypt password... loading clear value.");
                }
                catch (CryptographicException)
                {
                    Logger.WarnFormat(Constants.ClientNameFormat, settings.Name, "Failed to decrypt password... loading clear value.");
                }
            }
        }
Example #3
0
        public void Decrypt_WithBase64CipherAndIV()
        {
            var symmetric     = new Symmetric(key);
            var cipher        = symmetric.Encrypt(plainText, iv);
            var decryptedText = symmetric.Decrypt(Convert.ToBase64String(cipher.EncryptedContent), iv);

            Assert.AreEqual(plainText, decryptedText);
        }
Example #4
0
        public void EncryptDecryptString_AppendIV()
        {
            var symmetric     = new Symmetric(key);
            var cipher        = symmetric.Encrypt(plainText, iv);
            var decryptedText = symmetric.Decrypt(cipher);

            Assert.AreEqual(plainText, Symmetric.Deserialize <string>(decryptedText));
        }
Example #5
0
        public void Decrypt_WithNullSecret_ReturnsEmptyString()
        {
            byte[] secret = null;

            Symmetric symmetricHelper = new Symmetric();
            string    actual          = symmetricHelper.Decrypt(secret);

            Assert.IsNullOrEmpty(actual);
        }
Example #6
0
        public void BasicCanDecryptTest()
        {
            const string PLAINTEXT = "SmartEncryption Sym Test";
            var          key       = Symmetric.GenerateKey();
            var          cipher    = Symmetric.Encrypt(Encoding.UTF8.GetBytes(PLAINTEXT), key);
            var          plain     = Symmetric.Decrypt(cipher, key);

            Assert.AreEqual(PLAINTEXT, Encoding.UTF8.GetString(plain));
        }
Example #7
0
 public static string Decrypt(String strEncText, string strKey24)
 {
     Symmetric sym = new Symmetric(Symmetric.Provider.TripleDES, true);
     EncryptionClassLibrary.Encryption.Data key = new EncryptionClassLibrary.Encryption.Data(strKey24);
     EncryptionClassLibrary.Encryption.Data encryptedData = new EncryptionClassLibrary.Encryption.Data();
     encryptedData.Base64 = strEncText;
     EncryptionClassLibrary.Encryption.Data decryptedData = new EncryptionClassLibrary.Encryption.Data();
     decryptedData = sym.Decrypt(encryptedData, key);
     return decryptedData.ToString();
 }
        public void DecryptSuccess()
        {
            var encryptedText = "VVBXjy/jCQsBjv8hlyl0lrhcRuyaWzorLcLp86Zkqx8=";
            var plainText     = "this is my plain text";
            var passPhrase    = "this is my pass phrase";

            var decrypted = Symmetric.Decrypt(encryptedText, passPhrase);

            Assert.AreEqual(plainText, decrypted);
        }
 public static string Decrypt(String strEncText, string strKey24)
 {
     var sym = new Symmetric(Symmetric.Provider.TripleDES, true);
     var key = new Data(strKey24);
     var encryptedData = new Data();
     encryptedData.Base64 = strEncText;
     var decryptedData = new Data();
     decryptedData = sym.Decrypt(encryptedData, key);
     return decryptedData.ToString();
 }
Example #10
0
        public static string Decrypt(string value)
        {
            Symmetric sym = new Symmetric();

            CPM.Business.Global.Account.Encryption.Data encryptedData = new CPM.Business.Global.Account.Encryption.Data();
            encryptedData.Base64 = value;

            CPM.Business.Global.Account.Encryption.Data decryptedData = default(CPM.Business.Global.Account.Encryption.Data);
            decryptedData = sym.Decrypt(encryptedData);

            return(decryptedData.Text);
        }
        public bool LogIn(string password1, string password2)
        {
            //var encrypted = Symmetric.Encrypt<AesManaged>(decrypted, password1, password2);
            var encrypted = "4Y390/+dcY9jvQUrfSwKdncyF/Ue0yOgfBH9/wPmfhFNJ7SJqV42Gl+Ykz4xlyR6JbYAaLPNbG2M8HJ1fCaF2XsRMthdI7UfazyngX5/Ax/TOGBRzWoMfhBWTK4VJF7Xb3mtLNu3ODU3//XNyJhmhZE/kpGgtIBUndyJUZT9dfpfA1HhoCwJVZsqu0kphtVgtw8UjXopg1URcybCG/wflg==";

            connectionStr = Symmetric.Decrypt <AesManaged>(encrypted, password1, password2);

            if (string.IsNullOrEmpty(connectionStr))
            {
                return(false);
            }

            return(true);
        }
Example #12
0
        public IActionResult Decrypt(string ciphertext)
        {
            string key = _configuration["Symmetric:Key"];
            string iv  = _configuration["Symmetric:IV"];

            Symmetric s = new Symmetric(key, iv);

            byte[] decryptedBytes = s.Decrypt(ciphertext);

            ViewBag.Ciphertext = ciphertext;
            ViewBag.Plaintext  = Encoding.UTF8.GetString(decryptedBytes);

            return(View());
        }
        public bool LogIn(string password1, string password2)
        {
            //var encrypted = Symmetric.Encrypt<AesManaged>(decrypted, password1, password2);
            var encrypted = "MpellEPjKmPk0tzWRY6hSu9y+rnlGzB1jAdiQ+g3oksQ3tOtp5/fu90/YmRsOtFXIDhkHCEobDBDytKnGgumKkxE2Lr2Fv8PgCUuswKK9tp52rdH13EuNvsIZNkYjdchtjnzC3oNsZesneWq4zgO9hzOkSmqku4nNM3sHbhHq8TJmV1P1G8TE2TH/Us49vEcqRtRaOF0oU2vzCU32I4nBilKY2oVEvd9/6xTXfpdF88+X+Vi2D0E0wEf/oe3NAAnwedV2KF2nhgweWWtIENL/Q==";

            connectionStr = Symmetric.Decrypt <AesManaged>(encrypted, password1, password2);

            if (string.IsNullOrEmpty(connectionStr))
            {
                return(false);
            }

            return(true);
        }
 public string decrypt(string code)
 {
     try
     {
         var sym = new Symmetric(_p);
         sym.Key.Text = _k;
         var d = new Encryption.Data();
         d.Hex = code;
         return(sym.Decrypt(d).Text);
     }
     catch (Exception)
     {
         return(string.Empty);
     }
 }
Example #15
0
        public void Decrypt_WithEmptyString_ReturnsEmptyString(
            [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode)
        {
            string secret = String.Empty;

            Symmetric symmetricHelper = new Symmetric(mode);

            symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT;
            symmetricHelper.IV   = SymmetricTestsHelper.GetDefaultIV(mode);
            symmetricHelper.Key  = SymmetricTestsHelper.GetDefaultKey(mode);

            string actual = symmetricHelper.Decrypt(secret);

            Assert.IsNullOrEmpty(actual);
        }
Example #16
0
 /// <summary>
 ///  Decrypt encrypted text via Rijndael symmetric-key algorithm. Returns the plain text
 /// </summary>
 /// <param name="encryptedText">Base64 string representation of the encrypted text</param>
 /// <returns></returns>
 public string Decrypt(string encryptedText)
 {
     try
     {
         string eKey = this._key;
         var sym = new Symmetric(Symmetric.Provider.Rijndael);
         var key = new Data(eKey);
         var encryptedData = new Data {Base64 = encryptedText};
         return sym.Decrypt(encryptedData, key).ToString();
     }
     catch(Exception)
     {
         return null;
     }
 }
        public void EncryptTest()
        {
            var message  = "Attack at dawn!";
            var password = "******";

            // Encrypt the message using our password to get a cipher text
            var cipher = Symmetric.Encrypt(message, password);

            // Decode the cipher text using the same password to get the original message
            Assert.AreEqual(message, Symmetric.Decrypt(cipher, password));

            // CONDENSED EXAMPLE WITH BYTES
            byte[] messageBytes  = { 0x25, 0x9F, 0xB3 };
            byte[] passwordBytes = { 0x12, 0x34, 0x56, 0x78 };
            Assert.AreEqual(messageBytes.ToBase64(), Symmetric.Decrypt(Symmetric.Encrypt(messageBytes, passwordBytes), passwordBytes).ToBase64());
        }
Example #18
0
 /// <summary>
 ///  Decrypt encrypted text via Rijndael symmetric-key algorithm. Returns the plain text
 /// </summary>
 /// <param name="encryptedText">Base64 string representation of the encrypted text</param>
 /// <returns></returns>
 public string Decrypt(string encryptedText)
 {
     try
     {
         string eKey          = this.Key;
         var    sym           = new Symmetric(Symmetric.Provider.Rijndael);
         var    key           = new Data(eKey);
         var    encryptedData = new Data();
         encryptedData.Base64 = encryptedText;
         return(sym.Decrypt(encryptedData, key).ToString());
     }
     catch (Exception)
     {
         return(null);
     }
 }
Example #19
0
        public void Decrypt_WithDefaultSecretString_ReturnsEmptyString(
            [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode)
        {
            string secret   = SymmetricTestsHelper.GetDefaultSecretString(mode);
            string expected = SymmetricTestsHelper.DEFAULT_MESSAGE;

            Symmetric symmetricHelper = new Symmetric(mode);

            symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT;
            symmetricHelper.IV   = SymmetricTestsHelper.GetDefaultIV(mode);
            symmetricHelper.Key  = SymmetricTestsHelper.GetDefaultKey(mode);

            string actual = symmetricHelper.Decrypt(secret);

            Assert.AreEqual(expected, actual);
        }
Example #20
0
 public void Decrypt_NoCipherIV()
 {
     try
     {
         var symmetric = new Symmetric(key);
         var cipher    = symmetric.Encrypt(plainText, iv);
         cipher.IV = null;
         Assert.IsFalse(cipher.HasIV);
         var decryptedText = symmetric.Decrypt(cipher);
         Assert.AreEqual(plainText, decryptedText);
     }
     catch (Exception ex)
     {
         Assert.AreEqual("Initialization vector must be supplied", ex.Message);
         throw;
     }
 }
Example #21
0
        public async Task GetApiKeys(string password)
        {
            //mIWrBwmCu+/ZvZfwS2//R5YKYokBuIo7BRixpa1dpzY9EY6nV/7FMbR8aVC7D5okOU38a2QPYxLxK/Qsf6DqplVbU2irUtYEcBuxJt9wymu8KqiQLQIkd+lHUTxJxzmEGKnmI9JcVscWF5mkj9XANx5263PP7xh47d4NbDOSwn8Jud85ZZtvlWroemb9U2JI2uf/5I2hzhX7Op/shEVFDQbnM9YkJ3coIGRJm+cD9x2h7cDjYnyR1dNr1fJwB6Dx
            string decrypted = Symmetric.Decrypt <AesManaged>("mIWrBwmCu+/ZvZfwS2//R5YKYokBuIo7BRixpa1dpzY9EY6nV/7FMbR8aVC7D5okOU38a2QPYxLxK/Qsf6DqplVbU2irUtYEcBuxJt9wymu8KqiQLQIkd+lHUTxJxzmEGKnmI9JcVscWF5mkj9XANx5263PP7xh47d4NbDOSwn8Jud85ZZtvlWroemb9U2JI2uf/5I2hzhX7Op/shEVFDQbnM9YkJ3coIGRJm+cD9x2h7cDjYnyR1dNr1fJwB6Dx"
                                                              , password, "apikeystore");

            var account = new CloudStorageAccount(
                new StorageCredentials("apikeystore", decrypted), true);
            var tableClient = account.CreateCloudTableClient();
            var table       = tableClient.GetTableReference("ApiKeys");

            TableQuery <ApiKeyEntity> query = new TableQuery <ApiKeyEntity>().Where(
                TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Dev"), TableOperators.And
                                          , TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "d666bdc6d2d2643ea61a6d5df444f6ef84e084b269565a2b0b5bf805fe36702b")));

            var result = await table.ExecuteQuerySegmentedAsync <ApiKeyEntity>(query, new TableContinuationToken());

            if (result.Results.Count > 0)
            {
                CoinMarketCapApiKey = result.Results[0].ApiKey;
            }

            query = new TableQuery <ApiKeyEntity>().Where(
                TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Dev"), TableOperators.And
                                          , TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "7f0889c0a7ec97519dff1cf6e1ed52a99422928f5412d53e01e5d546398da472")));

            result = await table.ExecuteQuerySegmentedAsync <ApiKeyEntity>(query, new TableContinuationToken());

            if (result.Results.Count > 0)
            {
                NomicsApiKey = result.Results[0].ApiKey;
            }

            query = new TableQuery <ApiKeyEntity>().Where(
                TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Dev"), TableOperators.And
                                          , TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "103ed9f43eb647a0986e751faa8c5745")));

            result = await table.ExecuteQuerySegmentedAsync <ApiKeyEntity>(query, new TableContinuationToken());

            if (result.Results.Count > 0)
            {
                InfuraMainnetKey = result.Results[0].ApiKey;
            }
        }
Example #22
0
        public void TestEncryptDecrypt()
        {
            var key = "eda8506c1fb0bbcc3f62626fef074bbf2d09a8c7c608f3fa1482c9a625d00f75".ToByteArray();

            var plaintexs = new[]
            {
                "", "a", "ab", "abc", "abcd", "short", "hello, how are you?", "this is very short",
                "this is very long though, like, very very long, should we test very very long things here?",
                "Domestic cats are similar in size to the other members of the genus Felis, typically weighing "
                + "between 4 and 5 kg (9 and 10 lb).[36] Some breeds, such as the Maine Coon, can occasionally "
                + "exceed 11 kg (24 lb). Conversely, very small cats, less than 2 kg (4 lb), have been reported.[59] "
                + "The world record for the largest cat is 21 kg(50 lb).[60][self - published source] "
                + "The smallest adult cat ever officially recorded weighed around 1 kg(2 lb).[60] "
                + "Feral cats tend to be lighter, as they have more limited access to food than house cats."
                + "The Boston Cat Hospital weighed trapped feral cats, and found the average feral adult "
                + "male to weigh 4 kg(9 lb), and average adult female 3 kg(7 lb).[61] Cats average about "
                + "23–25 cm(9–10 in) in height and 46 cm(18 in) in head / body length(males being larger than females), "
                + "with tails averaging 30 cm(12 in) in length;[62] feral cats may be smaller on average.ats have seven"
                + " cervical vertebrae, as do almost all mammals; 13 thoracic vertebrae(humans have 12); seven lumbar"
                + " vertebrae(humans have five); three sacral vertebrae like most mammals(humans have five);"
                + " and a variable number of caudal vertebrae in the tail(humans have only vestigial caudal"
                + " vertebrae, fused into an internal coccyx).[63]:11 The extra lumbar and thoracic vertebrae"
                + " account for the cat's spinal mobility and flexibility. Attached to the spine are 13 ribs,"
                + " the shoulder, and the pelvis.[63] :16 Unlike human arms, cat forelimbs are attached to the"
                + " shoulder by free-floating clavicle bones which allow them to pass their body through any"
                + " space into which they can fit their head.[64]",
            };

            foreach (var plaintextString in plaintexs)
            {
                var plaintex   = Encoding.ASCII.GetBytes(plaintextString);
                var ciphertext = Symmetric.Encrypt(key, plaintex);
                var decrypted  = Symmetric.Decrypt(key, ciphertext);

                if (!plaintex.SequenceEqual(decrypted))
                {
                    throw new Exception("Decrypt did not work");
                }
            }
        }
Example #23
0
        public static bool VerifyPasswordMatch(string passwordToTest, byte[] encryptedCorrectPassword, bool passwordIsHashed = false)
        {
            string decryptedCorrectPassword = Symmetric.Decrypt(encryptedCorrectPassword);

            return(VerifyPasswordMatch(passwordToTest, decryptedCorrectPassword, passwordIsHashed));
        }
Example #24
0
        public void Decrypt_WithDefaultSecretString_ReturnsEmptyString(
            [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode)
        {
            string secret = SymmetricTestsHelper.GetDefaultSecretString(mode);
            string expected = SymmetricTestsHelper.DEFAULT_MESSAGE;

            Symmetric symmetricHelper = new Symmetric(mode);
            symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT;
            symmetricHelper.IV = SymmetricTestsHelper.GetDefaultIV(mode);
            symmetricHelper.Key = SymmetricTestsHelper.GetDefaultKey(mode);

            string actual = symmetricHelper.Decrypt(secret);

            Assert.AreEqual(expected, actual);
        }
Example #25
0
        public void Decrypt_WithNullSecret_ReturnsEmptyString()
        {
            byte[] secret = null;

            Symmetric symmetricHelper = new Symmetric();
            string actual = symmetricHelper.Decrypt(secret);

            Assert.IsNullOrEmpty(actual);
        }
        private ClientSettings Deserialize(XmlNode xmlData)
        {
            var settings = new ClientSettings(ClientType.Legacy);

            // ReSharper disable PossibleNullReferenceException

            settings.LegacyClientSubType = GetLegacyClientSubType(xmlData.SelectSingleNode(xmlPropType).InnerText);
            settings.Name = xmlData.Attributes[xmlAttrName].ChildNodes[0].Value;
            settings.Path = xmlData.SelectSingleNode(xmlPropPath).InnerText;

            try
            {
                bool external = Convert.ToBoolean(xmlData.SelectSingleNode(xmlNodeExternal).InnerText);
                if (external)
                {
                    Logger.Warn("Cannot migrate legacy external client settings. Skipping these client settings and continuing.");
                    return(null);
                }
            }
            catch (NullReferenceException)
            {
                // do nothing, we don't care about legacy external settings
            }
            catch (FormatException)
            {
                Logger.Warn("Cannot load external instance flag. Skipping these client settings and continuing.");
                return(null);
            }

            try
            {
                settings.FahLogFileName = xmlData.SelectSingleNode(xmlNodeFAHLog).InnerText;
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load FAHlog.txt file name.");
                settings.FahLogFileName = Constants.FahLogFileName;
            }

            try
            {
                settings.UnitInfoFileName = xmlData.SelectSingleNode(xmlNodeUnitInfo).InnerText;
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load unitinfo.txt file name.");
                settings.UnitInfoFileName = Constants.UnitInfoFileName;
            }

            try
            {
                settings.QueueFileName = xmlData.SelectSingleNode(xmlNodeQueue).InnerText;
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load queue.dat file name.");
                settings.QueueFileName = Constants.QueueFileName;
            }

            try
            {
                settings.Server = xmlData.SelectSingleNode(xmlPropServ).InnerText;
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load client server.");
                settings.Server = String.Empty;
            }

            try
            {
                settings.Username = xmlData.SelectSingleNode(xmlPropUser).InnerText;
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load server username.");
                settings.Username = String.Empty;
            }

            var symetricProvider = new Symmetric(SymmetricProvider.Rijndael, false);

            try
            {
                settings.Password = String.Empty;
                if (xmlData.SelectSingleNode(xmlPropPass).InnerText.Length > 0)
                {
                    try
                    {
                        symetricProvider.IntializationVector = _iv;
                        settings.Password = symetricProvider.Decrypt(new Data(xmlData.SelectSingleNode(xmlPropPass).InnerText.FromBase64()), _symmetricKey).ToString();
                    }
                    catch (FormatException)
                    {
                        Logger.Warn("Cannot decrypt password... loading clear value.");
                        settings.Password = xmlData.SelectSingleNode(xmlPropPass).InnerText;
                    }
                    catch (CryptographicException)
                    {
                        Logger.Warn("Cannot decrypt password... loading clear value.");
                        settings.Password = xmlData.SelectSingleNode(xmlPropPass).InnerText;
                    }
                }
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load password.");
            }

            try
            {
                settings.FtpMode = GetFtpType(xmlData.SelectSingleNode(xmlPropPassiveFtpMode).InnerText);
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load ftp mode, defaulting to passive.");
                settings.FtpMode = FtpMode.Passive;
            }
            catch (FormatException)
            {
                Logger.Warn("Cannot load ftp mode, defaulting to passive.");
                settings.FtpMode = FtpMode.Passive;
            }

            try
            {
                settings.UtcOffsetIsZero = Convert.ToBoolean(xmlData.SelectSingleNode(xmlNodeClientVM).InnerText);
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load client VM flag, defaulting to false.");
                settings.UtcOffsetIsZero = false;
            }
            catch (FormatException)
            {
                Logger.Warn("Cannot load client VM flag, defaulting to false.");
                settings.UtcOffsetIsZero = false;
            }

            try
            {
                settings.ClientTimeOffset = Int32.Parse(xmlData.SelectSingleNode(xmlNodeClientOffset).InnerText);
            }
            catch (NullReferenceException)
            {
                Logger.Warn("Cannot load client time offset, defaulting to 0.");
                settings.ClientTimeOffset = 0;
            }
            catch (FormatException)
            {
                Logger.Warn("Cannot load client time offset, defaulting to 0.");
                settings.ClientTimeOffset = 0;
            }

            // ReSharper restore PossibleNullReferenceException

            return(settings);
        }
Example #27
0
        public void Decrypt_WithNullString_ReturnsEmptyString(
            [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode)
        {
            string secret = null;

            Symmetric symmetricHelper = new Symmetric(mode);
            symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT;
            symmetricHelper.IV = SymmetricTestsHelper.GetDefaultIV(mode);
            symmetricHelper.Key = SymmetricTestsHelper.GetDefaultKey(mode);

            string actual = symmetricHelper.Decrypt(secret);

            Assert.IsNullOrEmpty(actual);
        }