private void loginButton_Click(object sender, EventArgs e) { loginButton.Enabled = false; Task.Factory.StartNew(() => { try { Program.Session = Session.DoLogin(usernameTextBox.Text, passwordTextBox.Text); // Login successful if (rememberMeCheckBox.Checked) { var lastLogin = new LastLogin(); lastLogin.Username = usernameTextBox.Text; lastLogin.Password = passwordTextBox.Text; LastLogin.SetLastLogin(lastLogin); } else { if (File.Exists(LastLogin.LastLoginFile)) File.Delete(LastLogin.LastLoginFile); } if (this.InvokeRequired) this.Invoke(new Action(Close)); else this.Close(); } catch (UnauthorizedAccessException ex) { if (MessageBox.Show(ex.Message + Environment.NewLine + "Would you like to use offline mode?", "Login Error", MessageBoxButtons.YesNo) == DialogResult.Yes) { Program.Session = new Session(usernameTextBox.Text); if (this.InvokeRequired) this.Invoke(new Action(Close)); else this.Close(); } else { if (loginButton.InvokeRequired) loginButton.Invoke(new Action(() => loginButton.Enabled = true)); else loginButton.Enabled = true; } } catch (Exception ex) { } }); }
public static void SetLastLogin(LastLogin login) { byte[] decrypted = BitConverter.GetBytes(IPAddress.NetworkToHostOrder((short)login.Username.Length)) .Concat(System.Text.Encoding.UTF8.GetBytes(login.Username)) .Concat(BitConverter.GetBytes(IPAddress.NetworkToHostOrder((short)login.Password.Length))) .Concat(System.Text.Encoding.UTF8.GetBytes(login.Password)).ToArray(); PKCSKeyGenerator crypto = new PKCSKeyGenerator(LastLoginPassword, LastLoginSalt, 5, 1); ICryptoTransform cryptoTransform = crypto.Encryptor; byte[] encrypted = cryptoTransform.TransformFinalBlock(decrypted, 0, decrypted.Length); if (File.Exists(LastLoginFile)) File.Delete(LastLoginFile); using (Stream stream = File.Create(LastLoginFile)) stream.Write(encrypted, 0, encrypted.Length); }
public static void SetLastLogin(LastLogin login) { byte[] decrypted = BitConverter.GetBytes(IPAddress.NetworkToHostOrder((short)login.Username.Length)) .Concat(System.Text.Encoding.UTF8.GetBytes(login.Username)) .Concat(BitConverter.GetBytes(IPAddress.NetworkToHostOrder((short)login.Password.Length))) .Concat(System.Text.Encoding.UTF8.GetBytes(login.Password)).ToArray(); PKCSKeyGenerator crypto = new PKCSKeyGenerator(LastLoginPassword, LastLoginSalt, 5, 1); ICryptoTransform cryptoTransform = crypto.Encryptor; byte[] encrypted = cryptoTransform.TransformFinalBlock(decrypted, 0, decrypted.Length); if (File.Exists(LastLoginFile)) { File.Delete(LastLoginFile); } using (Stream stream = File.Create(LastLoginFile)) stream.Write(encrypted, 0, encrypted.Length); }
public static LastLogin GetLastLogin(string lastLoginFile) { try { byte[] encryptedLogin = File.ReadAllBytes(lastLoginFile); PKCSKeyGenerator crypto = new PKCSKeyGenerator(LastLoginPassword, LastLoginSalt, 5, 1); ICryptoTransform cryptoTransform = crypto.Decryptor; byte[] decrypted = cryptoTransform.TransformFinalBlock(encryptedLogin, 0, encryptedLogin.Length); short userLength = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(decrypted, 0)); byte[] user = decrypted.Skip(2).Take(userLength).ToArray(); byte[] password = decrypted.Skip(4 + userLength).ToArray(); LastLogin result = new LastLogin(); result.Username = System.Text.Encoding.UTF8.GetString(user); result.Password = System.Text.Encoding.UTF8.GetString(password); return(result); } catch { return(null); } }
public static LastLogin GetLastLogin(string lastLoginFile) { try { byte[] encryptedLogin = File.ReadAllBytes(lastLoginFile); PKCSKeyGenerator crypto = new PKCSKeyGenerator(LastLoginPassword, LastLoginSalt, 5, 1); ICryptoTransform cryptoTransform = crypto.Decryptor; byte[] decrypted = cryptoTransform.TransformFinalBlock(encryptedLogin, 0, encryptedLogin.Length); short userLength = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(decrypted, 0)); byte[] user = decrypted.Skip(2).Take(userLength).ToArray(); byte[] password = decrypted.Skip(4 + userLength).ToArray(); LastLogin result = new LastLogin(); result.Username = System.Text.Encoding.UTF8.GetString(user); result.Password = System.Text.Encoding.UTF8.GetString(password); return result; } catch { return null; } }