public async Task <SecureChatUser> RegisterNewUser(SecureChatCreateUserRequest userRequest, RSAKeySetIdentity keySetIdentity) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(HostNameURL); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.PostAsJsonAsync("api/securechat/users", userRequest); if (response.IsSuccessStatusCode) { // Get the URI of the created resource. string responceMessage = await response.Content.ReadAsStringAsync(); StringReader sr = new StringReader(responceMessage); JsonReader jsonReader = new JsonTextReader(sr); CreateNewUser createNewUserServerResponce = _jsonSerializer.Deserialize <CreateNewUser>(jsonReader); RSA_AsymetricEncryption rsaAsymetricEncryption = new RSA_AsymetricEncryption(); RSAParameters rsaParameters = rsaAsymetricEncryption.ParseRSAPublicKeyOnlyInfo(keySetIdentity); string aesKey = rsaAsymetricEncryption.DecryptObjectUsingRSA(createNewUserServerResponce.EncodedAESKey, rsaParameters); // Todo decrypt incoming data using AES SecureChatUser secureChatUser = new SecureChatUser(); //register public key //PublicKeyData publicKeyData = new PublicKeyData(); //publicKeyData.GUID = userKeySet.RSA_GUID; //publicKeyData.PublicKey = userKeySet.RSA_PublicKey; //response = await client.PostAsJsonAsync("api/securechat/publickeys", publicKeyData); if (response.IsSuccessStatusCode) { return(secureChatUser); } } } return(null); }
private async void btnCreateUser_Click(object sender, EventArgs e) { txtPassword.Focus(); txtPasswordConfirm.Focus(); if (!_passwordsConfirmed) { MessageBox.Show(this, "Invalid password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!_usernameValidated) { MessageBox.Show(this, "Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (keySetIdentity == null) { lblServerStatus.Text = "RSA keys for server comunication has not yet been completed, please try again in 5 seconds"; return; } try { SecureChatCreateUserRequest userRequest = new SecureChatCreateUserRequest { Nickname = this.txtUserName.Text, RSA_PublicKey = this.keySetIdentity.RSA_PublicKey }; chatUserProfileData = await ConnectionService.Instance.RegisterNewUser(userRequest, this.keySetIdentity); if (chatUserProfileData != null) { saveFileDialog1.FileName = chatUserProfileData.NickName + ".scp"; saveFileDialog1.Filter = "Secure chat profiles|*.scp"; if (saveFileDialog1.ShowDialog(this) == DialogResult.OK) { StorageManager.SerializeObjectToFile(chatUserProfileData, saveFileDialog1.FileName, txtPassword.Text); } } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.DialogResult = DialogResult.OK; this.Close(); }
public static SecureChatUser CreateChatUser(string nickname, string guid) { SecureChatUser user = new SecureChatUser { GUID = guid, NickName = nickname, IsVisibleToSearch = false }; user.UserInfo = new SecureChatUserInfo(); RSA_AsymetricEncryption rsaAsymetricEncryption = new RSA_AsymetricEncryption(); RSAKeySetIdentity rsaKeySet = rsaAsymetricEncryption.GenerateRSAKeyPair(RSA_AsymetricEncryption.RSAKeySize.b4096); user.User_Certificate = rsaKeySet.RSA_PublicKey; user.User_PrivateKey = rsaKeySet.RSA_PrivateKey; user.Server_Certificate = ServerKeyManager.KeyManager.ServerPublicKey; return(user); }