public FtpTicketResponse RequstSessionKeyForFtpConnection(FtpKeyRequst ftpKeyRequst) { if (!users_list.ContainsKey(ftpKeyRequst.UserName)) { return(null); } // get users from the data base KdcFtpKey retKeyFromDB = m_FtpDBservice.getKdcFtpKey("KDC"); // check validity if (retKeyFromDB == null) { Console.Write("no key exist in DB"); return(null); } // genrate new session key for CLIENT - FTP byte[] sessiomKey = CAes.NewKey(); FtpTicketResponse ftpTicketResponse = new FtpTicketResponse(); ftpTicketResponse.SessionKeyClientFTPEncryptedForFTP = CAes.SimpleEncryptWithPassword(sessiomKey, retKeyFromDB.PassWord); ftpTicketResponse.UserNameencryptedForFtpWithFtpKey = CAes.SimpleEncryptWithPassword(ftpKeyRequst.UserName, retKeyFromDB.PassWord); ftpTicketResponse.SessionKeyClientFTPEncryptedForClient = CAes.SimpleEncrypt(sessiomKey, users_list[ftpKeyRequst.UserName].SessionKey, users_list[ftpKeyRequst.UserName].SessionKey); return(ftpTicketResponse); }
// requst to connect to Ftp service private void getSessionKeyForFtpWorker_DoWork(object sender, DoWorkEventArgs e) { clientPrivateData clientData = ClientAllData.Instance.getMyClient(); //send requst for Auth FtpKeyRequst ftpKeyRequst = new FtpKeyRequst(clientData.username); FtpTicketResponse ftpTicketResponse = kdcProxy.RequstSessionKeyForFtpConnection(ftpKeyRequst); // blocking if (ftpTicketResponse == null) { return; } byte[] sessionKey = CAes.SimpleDecrypt(ftpTicketResponse.SessionKeyClientFTPEncryptedForClient, clientData.m_kdcAsSessionKey, clientData.m_kdcAsSessionKey); ClientAllData.Instance.getMyClient().m_ftpSessionKey = sessionKey; FtpTicketRequst ftpTicketRequst = new FtpTicketRequst(); ftpTicketRequst.UserNameencryptedForFtpWithFtpKey = ftpTicketResponse.UserNameencryptedForFtpWithFtpKey; ftpTicketRequst.SessionKeyClientFTPEncryptedForFTP = ftpTicketResponse.SessionKeyClientFTPEncryptedForFTP; ftpTicketRequst.UserNameencryptedForFtpWithSessionKey = CAes.SimpleEncrypt(clientData.username, sessionKey, sessionKey); ftpProxy.requstForConnectionWithSessionKey(ftpTicketRequst); // non blocking //TODO: should set timer for time out }
public void startSessionChatRemote() { byte[] readBuffer = new byte[1024]; bool sessionOk = false; try { readBuffer = udpClient.Receive(ref ipPartnerEndPointRcv); } catch (Exception e) { Console.WriteLine(e.ToString()); } byte[] sessionKey = null; if (readBuffer != null) { clientPrivateData userData = ClientAllData.Instance.getMyClient(); sessionKey = CAes.SimpleDecrypt(readBuffer, userData.m_kdcAsSessionKey, userData.m_kdcAsSessionKey); string challenge = Path.GetRandomFileName(); messageCreateChatRemoteToMaster sendMsgFromMaster = new messageCreateChatRemoteToMaster();; sendMsgFromMaster.challenge = CAes.SimpleEncrypt(challenge, sessionKey, sessionKey); sendMsgFromMaster.sessionKey = CAes.SimpleEncrypt(sessionKey, sessionKey, sessionKey); m_binryFormatter.Serialize(m_memStreamer, sendMsgFromMaster); byte[] buffer = m_memStreamer.ToArray(); udpClient.Send(buffer, buffer.Length, ipPartnerEndPointSend); byte[] readBuffer2 = new byte[1024]; try { readBuffer2 = udpClient.Receive(ref ipPartnerEndPointRcv); byte[] clientData = new byte[1024]; MemoryStream readSteam = new MemoryStream(readBuffer2); messageCreateChatRemoteToMaster retMsgFromRemote = (messageCreateChatRemoteToMaster)m_binryFormatter.Deserialize(readSteam); byte[] sessionKey2 = CAes.SimpleDecrypt(retMsgFromRemote.sessionKey, sessionKey, sessionKey); string challenge2 = CAes.SimpleDecrypt(retMsgFromRemote.challenge, sessionKey, sessionKey); if (StructuralComparisons.StructuralEqualityComparer.Equals(sessionKey2, sessionKey) && challenge2 == challenge) { sessionOk = true; } } catch (Exception e) { //set session key Console.WriteLine(e.ToString()); } } if (sessionOk) { m_sessionKey = sessionKey; } }
private void sendMessageBtn(object sender, RoutedEventArgs e) { string sendString = m_myUseruser.username + ": " + enterMessageTxtBox.Text + "\n"; byte[] stringBytes = Encoding.ASCII.GetBytes(sendString); byte[] encryptedMsg = CAes.SimpleEncrypt(stringBytes, m_sessionKey, m_sessionKey); udpClient.Send(encryptedMsg, encryptedMsg.Length, ipPartnerEndPointSend); AllChatTxtBox.AppendText(sendString); }
private void downLoadFileButton_Click(object sender, RoutedEventArgs e) { //send requst to the server if (ftpFilesComboBox.IsEnabled == true) { if (ftpFilesComboBox.Text != null) { string encrypedFile = CAes.SimpleEncrypt(ftpFilesComboBox.Text, ClientAllData.Instance.getMyClient().m_ftpSessionKey, ClientAllData.Instance.getMyClient().m_ftpSessionKey); ftpProxy.requstForDownloadFile(encrypedFile, ClientAllData.Instance.getMyUsername()); } } }
public CSessionKeyResponse GetSessionKeyForChatConnection(CSessionParams sessionParams) { // get users from the data base User retUser1FromDB = m_DBservice.getUserByName(sessionParams.client1UserName); User retUser2FromDB = m_DBservice.getUserByName(sessionParams.client2UserName); UserServiceData localUser = users_list[sessionParams.client1UserName]; UserServiceData partnerUser = users_list[sessionParams.client2UserName]; // check validity if (retUser1FromDB == null || retUser2FromDB == null || localUser == null || partnerUser == null) { if (retUser1FromDB == null) { Console.Write(sessionParams.client1UserName + "not exist in DB"); } if (retUser2FromDB == null) { Console.Write(sessionParams.client2UserName + "not exist in DB"); } if (localUser == null) { Console.Write(sessionParams.client1UserName + "not logged in"); } if (partnerUser == null) { Console.Write(sessionParams.client2UserName + "not logged in"); } return(null); } // genrate new session key byte[] sessionKey = CAes.NewKey(); //encrypt Eka [ Ks || || Kb[Ks] ] byte[] encryptedDataForClientB = CAes.SimpleEncrypt(sessionKey, partnerUser.SessionKey, partnerUser.SessionKey); //Kb[Ks] byte[] keyA = CAes.SimpleEncrypt(sessionKey, localUser.SessionKey, localUser.SessionKey); //Ka[Ks] byte[] keyAB = CAes.SimpleEncrypt(encryptedDataForClientB, localUser.SessionKey, localUser.SessionKey); //Ka[clientB data] // set return value CSessionKeyResponse retVal = new CSessionKeyResponse(); retVal.m_sessionKeyA = keyA; retVal.m_sessionKeyB = keyAB; users_list[sessionParams.client1UserName].clientKdcCallBack.startChatSession(retUser2FromDB.ID + 1100, retUser2FromDB.Name, true); users_list[sessionParams.client2UserName].clientKdcCallBack.startChatSession(retUser1FromDB.ID + 1100, retUser1FromDB.Name, false); return(retVal); }
public void startChatSessionMaster() { bool sessionOk = false; try { udpClient.Send(m_remoteKey, m_remoteKey.Length, ipPartnerEndPointSend); } catch (Exception e) { Console.WriteLine(e.ToString()); } byte[] readBuffer = null; try { readBuffer = udpClient.Receive(ref ipPartnerEndPointRcv); byte[] clientData = new byte[1024]; MemoryStream readSteam = new MemoryStream(readBuffer); messageCreateChatRemoteToMaster retMsgFromMaster = (messageCreateChatRemoteToMaster)m_binryFormatter.Deserialize(readSteam); byte[] sessionKey = CAes.SimpleDecrypt(retMsgFromMaster.sessionKey, m_sessionKey, m_sessionKey); if (StructuralComparisons.StructuralEqualityComparer.Equals(sessionKey, m_sessionKey)) { string challenge = CAes.SimpleDecrypt(retMsgFromMaster.challenge, m_sessionKey, m_sessionKey); string retChallengeEncryted = CAes.SimpleEncrypt(challenge, m_sessionKey, m_sessionKey); byte[] encryptedSession = CAes.SimpleEncrypt(m_sessionKey, m_sessionKey, m_sessionKey); messageCreateChatRemoteToMaster retMsgToMaster = new messageCreateChatRemoteToMaster(); retMsgToMaster.challenge = retChallengeEncryted; retMsgToMaster.sessionKey = encryptedSession; m_binryFormatter.Serialize(m_memStreamer, retMsgToMaster); byte[] buffer = m_memStreamer.ToArray(); udpClient.Send(buffer, buffer.Length, ipPartnerEndPointSend); sessionOk = true; } else { //error return; } } catch (Exception e) { Console.WriteLine(e.ToString()); } if (sessionOk) { } }
public void requstForDownloadFile(string fileName, string clientName) { if (!users_list.ContainsKey(clientName)) { return; } string decrryptedFileName = CAes.SimpleDecrypt(fileName, users_list[clientName].SessionKey, users_list[clientName].SessionKey); string[] filesPaths = Directory.GetFiles("..\\..\\files\\", decrryptedFileName, SearchOption.AllDirectories); byte[] fileStream = File.ReadAllBytes(filesPaths[0]); byte[] fileStreamEncrypted = CAes.SimpleEncrypt(fileStream, users_list[clientName].SessionKey, users_list[clientName].SessionKey); users_list[clientName].clientFtpCallBack.finishRequstForDownloadFile(fileStreamEncrypted, fileName); }
public static Dictionary <string, UserFtpServiceData> users_list = new Dictionary <string, UserFtpServiceData>(); // user, key_S public void requstForConnectionWithSessionKey(FtpTicketRequst ftpTicketRequst) { IClientFtpCallBack iClientFtpCallBack = OperationContext.Current.GetCallbackChannel <IClientFtpCallBack>(); KdcFtpKey retKeyFromDB = m_FtpDBservice.getKdcFtpKey("KDC"); if (retKeyFromDB == null) { iClientFtpCallBack.finishRequstConnectionProcess(null); return; } byte[] sessionKey = CAes.SimpleDecryptWithPassword(ftpTicketRequst.SessionKeyClientFTPEncryptedForFTP, retKeyFromDB.PassWord); string usenameDecryptedWithFtpKdcKey = CAes.SimpleDecryptWithPassword(ftpTicketRequst.UserNameencryptedForFtpWithFtpKey, retKeyFromDB.PassWord); string usenameDecryptedWithSessionKey = CAes.SimpleDecrypt(ftpTicketRequst.UserNameencryptedForFtpWithSessionKey, sessionKey, sessionKey); if (usenameDecryptedWithFtpKdcKey == usenameDecryptedWithSessionKey) // OK { UserFtpServiceData userData = new UserFtpServiceData(sessionKey, iClientFtpCallBack); users_list.Add(usenameDecryptedWithFtpKdcKey, userData); string fileNameResult; string[] filesPaths = Directory.GetFiles("..\\..\\files\\", "*.*", SearchOption.AllDirectories); List <string> newFilesNames = new List <string>(); // Display all the files. foreach (string path in filesPaths) { fileNameResult = Path.GetFileName(path); string fileNameResultString = CAes.SimpleEncrypt(fileNameResult, sessionKey, sessionKey); newFilesNames.Add(fileNameResultString); } iClientFtpCallBack.finishRequstConnectionProcess(newFilesNames); return; } iClientFtpCallBack.finishRequstConnectionProcess(null); }
private void login_worker_DoWork(object sender, DoWorkEventArgs e) { bool looginSuccess = false; IKdcService proxy = KdcProxy.Instance.GetProxy(); string usernameInvoked = string.Empty; string passwordInvoked = string.Empty; Dispatcher.BeginInvoke(new Action(delegate { usernameInvoked = txtUserName.Text; passwordInvoked = txtPassWord.Password.ToString(); })); Thread.Sleep(100); // try to log in to the server ClientAllData.Instance.setMyUsername(usernameInvoked); CKdcToClientLogInData logginData = proxy.LogInApp(usernameInvoked); // parse the result if (logginData != null) { string passWordHash = string.Empty; using (MD5 md5Hash = MD5.Create()) { passWordHash = CMd5hash.GetMd5Hash(md5Hash, passwordInvoked); } string retDecUserName = CAes.SimpleDecryptWithPassword(logginData.m_username, passWordHash); byte[] retDecPassword = CAes.SimpleDecryptWithPassword(logginData.m_kdcAsSessionKey, passWordHash); byte[] localPortByte = CAes.SimpleDecryptWithPassword(logginData.m_localPort, passWordHash); int localPort = BitConverter.ToInt32(localPortByte, 0); if (retDecUserName == usernameInvoked) { string retChallenge = CAes.SimpleDecryptWithPassword(logginData.m_challenge, passWordHash); CLogInStatus logginStatus = new CLogInStatus(); logginStatus.m_username = retDecUserName; logginStatus.m_logInFail = false; logginStatus.m_challenge = CAes.SimpleEncrypt(retChallenge + retChallenge, retDecPassword, retDecPassword); if (proxy.SetLoginStatus(logginStatus)) { clientPrivateData myClient = ClientAllData.Instance.getMyClient(); myClient.username = retDecUserName; myClient.m_kdcAsSessionKey = retDecPassword; myClient.m_loginSucccess = true; myClient.m_localPort = localPort; looginSuccess = true; } } } if (!looginSuccess) { CLogInStatus logginStatus = new CLogInStatus(); logginStatus.m_username = usernameInvoked; logginStatus.m_logInFail = true; proxy.SetLoginStatus(logginStatus); } }
private void remoteHandshake() { byte[] readBuffer = new byte[1024]; bool sessionOk = false; try { readBuffer = udpClient.Receive(ref ipPartnerEndPointRcv); } catch (Exception e) { Console.WriteLine(e.ToString()); } byte[] sessionKey = null; if (readBuffer != null) { sessionKey = CAes.SimpleDecrypt(readBuffer, m_myUseruser.m_kdcAsSessionKey, m_myUseruser.m_kdcAsSessionKey); m_sessionKey = sessionKey; string challenge = System.IO.Path.GetRandomFileName(); messageCreateChatRemoteToMaster sendMsgFromMaster = new messageCreateChatRemoteToMaster();; sendMsgFromMaster.challenge = CAes.SimpleEncrypt(challenge, sessionKey, sessionKey); sendMsgFromMaster.sessionKey = CAes.SimpleEncrypt(sessionKey, sessionKey, sessionKey); m_binryFormatter.Serialize(m_memStreamer, sendMsgFromMaster); byte[] buffer = m_memStreamer.ToArray(); udpClient.Send(buffer, buffer.Length, ipPartnerEndPointSend); byte[] readBuffer2 = new byte[1024]; try { readBuffer2 = udpClient.Receive(ref ipPartnerEndPointRcv); byte[] clientData = new byte[1024]; MemoryStream readSteam = new MemoryStream(readBuffer2); messageCreateChatRemoteToMaster retMsgFromRemote = (messageCreateChatRemoteToMaster)m_binryFormatter.Deserialize(readSteam); byte[] sessionKey2 = CAes.SimpleDecrypt(retMsgFromRemote.sessionKey, sessionKey, sessionKey); string challenge2 = CAes.SimpleDecrypt(retMsgFromRemote.challenge, sessionKey, sessionKey); if (StructuralComparisons.StructuralEqualityComparer.Equals(sessionKey2, sessionKey) && challenge2 == challenge + challenge) { sessionOk = true; } } catch (Exception e) { //set session key Console.WriteLine(e.ToString()); } } if (sessionOk) { MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("confirm chat with " + m_userPort.userName, "Confirmation", System.Windows.MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.Yes) { string confirmStr = "confirm"; byte[] confirmStringBytes = Encoding.ASCII.GetBytes(confirmStr); byte[] encryptedMsg = CAes.SimpleEncrypt(confirmStringBytes, m_sessionKey, m_sessionKey); udpClient.Send(encryptedMsg, encryptedMsg.Length, ipPartnerEndPointSend); var evt = setShowChatWindowEvnt; if (evt != null) { evt(this, ""); } readChatMsg(); } else { return; } } }
private void masterHandshake() { while (m_sessionRespons == null) { Thread.Sleep(100); } m_sessionKey = CAes.SimpleDecrypt(m_sessionRespons.m_sessionKeyA, m_myUseruser.m_kdcAsSessionKey, m_myUseruser.m_kdcAsSessionKey); m_remoteKey = CAes.SimpleDecrypt(m_sessionRespons.m_sessionKeyB, m_myUseruser.m_kdcAsSessionKey, m_myUseruser.m_kdcAsSessionKey); bool sessionOk = false; try { udpClient.Send(m_remoteKey, m_remoteKey.Length, ipPartnerEndPointSend); } catch (Exception e) { Console.WriteLine(e.ToString()); } byte[] readBuffer = null; try { readBuffer = udpClient.Receive(ref ipPartnerEndPointRcv); byte[] clientData = new byte[1024]; MemoryStream readSteam = new MemoryStream(readBuffer); messageCreateChatRemoteToMaster retMsgFromMaster = (messageCreateChatRemoteToMaster)m_binryFormatter.Deserialize(readSteam); byte[] sessionKey = CAes.SimpleDecrypt(retMsgFromMaster.sessionKey, m_sessionKey, m_sessionKey); if (StructuralComparisons.StructuralEqualityComparer.Equals(sessionKey, m_sessionKey)) { string challenge = CAes.SimpleDecrypt(retMsgFromMaster.challenge, m_sessionKey, m_sessionKey); string retChallengeEncryted = CAes.SimpleEncrypt(challenge + challenge, m_sessionKey, m_sessionKey); byte[] encryptedSession = CAes.SimpleEncrypt(m_sessionKey, m_sessionKey, m_sessionKey); messageCreateChatRemoteToMaster retMsgToMaster = new messageCreateChatRemoteToMaster(); retMsgToMaster.challenge = retChallengeEncryted; retMsgToMaster.sessionKey = encryptedSession; m_binryFormatter.Serialize(m_memStreamer, retMsgToMaster); byte[] buffer = m_memStreamer.ToArray(); udpClient.Send(buffer, buffer.Length, ipPartnerEndPointSend); sessionOk = true; } else { //error return; } } catch (Exception e) { Console.WriteLine(e.ToString()); } if (sessionOk) { byte[] readBufferConfirm = new byte[1024]; readBufferConfirm = udpClient.Receive(ref ipPartnerEndPointRcv); byte[] readBufferConfirmBytes = CAes.SimpleDecrypt(readBufferConfirm, m_sessionKey, m_sessionKey); string retStringConfirm = Encoding.ASCII.GetString(readBufferConfirmBytes); if (retStringConfirm == "confirm") { var evt = setShowChatWindowEvnt; if (evt != null) { evt(this, ""); } readChatMsg(); } else { ; } { return; } } }