public MessageModel GetServerMessage([FromBody] MessageModel _clientMessage) { if (_clientMessage.IsValid()) { try { using (SqlProvider sqlOp = new SqlProvider()) using (AESOperations aesOp = new AESOperations()) { KeyModel sessionParameters = sqlOp.GetSessionKeys(new Guid(_clientMessage.SessionId)); if (sessionParameters != null && !string.IsNullOrEmpty(sessionParameters.PrivateKey) && !string.IsNullOrEmpty(sessionParameters.EncKey) && !string.IsNullOrEmpty(sessionParameters.IVKey)) { string clientMessage = aesOp.Decrypt(_clientMessage.Message, Encoding.ASCII.GetBytes(sessionParameters.EncKey), Encoding.ASCII.GetBytes(sessionParameters.IVKey)); if (clientMessage.Equals("MESSAGE FROM CLIENT")) { return(new MessageModel() { Message = aesOp.Encrypt("LEGIT CLIENT", Encoding.ASCII.GetBytes(sessionParameters.EncKey), Encoding.ASCII.GetBytes(sessionParameters.IVKey)) }); } } } } catch { } } return(null); }
public ResponseModel ShareSessionKeys([FromBody] SessionKeyRequestModel _clientInfo) { if (_clientInfo.IsValid()) { using (SqlProvider sqlOp = new SqlProvider()) using (RSAOperations keyOp = new RSAOperations()) { KeyModel sessionParameters = sqlOp.GetSessionKeys(new Guid(_clientInfo.SessionId)); if (sessionParameters != null && !string.IsNullOrEmpty(sessionParameters.PrivateKey) && !string.IsNullOrEmpty(sessionParameters.EncKey) && !string.IsNullOrEmpty(sessionParameters.IVKey)) { string clientPublicKey = string.Empty; foreach (string chunk in _clientInfo.ClientPublic.Split('≡')) { clientPublicKey += keyOp.Decrypt(sessionParameters.PrivateKey, chunk); } if (!string.IsNullOrEmpty(clientPublicKey)) { string encryptedEKey = keyOp.Encrypt(clientPublicKey, sessionParameters.EncKey); string encryptedIKey = keyOp.Encrypt(clientPublicKey, sessionParameters.IVKey); return(new ResponseModel() { EKey = encryptedEKey, IKey = encryptedIKey }); } } } } return(null); }