コード例 #1
0
        private void saveFileWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List <byte[]> genericlist = e.Argument as List <byte[]>;

            byte[] fileStream        = genericlist[0];
            byte[] folderPath        = genericlist[1];
            byte[] fileNameEncrypted = genericlist[2];

            string folderPathToString        = Encoding.UTF8.GetString(folderPath);
            string fileNameEncryptedToString = Encoding.UTF8.GetString(fileNameEncrypted);

            string decrryptedFileName = CAes.SimpleDecrypt(fileNameEncryptedToString, ClientAllData.Instance.getMyClient().m_ftpSessionKey, ClientAllData.Instance.getMyClient().m_ftpSessionKey);


            byte[] fileStreamDecrypt = CAes.SimpleDecrypt(fileStream, ClientAllData.Instance.getMyClient().m_ftpSessionKey, ClientAllData.Instance.getMyClient().m_ftpSessionKey);


            try
            {
                using (var fs = new FileStream(folderPathToString + "\\" + decrryptedFileName, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(fileStreamDecrypt, 0, fileStreamDecrypt.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in process: {0}", ex);
            }
        }
コード例 #2
0
ファイル: KdcService.cs プロジェクト: lioris/KdcChat
        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);
        }
コード例 #3
0
ファイル: KdcService.cs プロジェクト: lioris/KdcChat
        public bool SetLoginStatus(CLogInStatus logginStatus)
        {
            bool retVal = false;

            if (!users_list.ContainsKey(logginStatus.m_username))
            {
                return(retVal);
            }

            UserServiceData userData = users_list[logginStatus.m_username];

            if (userData != null)
            {
                if (!logginStatus.m_logInFail)
                {
                    string logginChalleng = CAes.SimpleDecrypt(logginStatus.m_challenge, userData.SessionKey, userData.SessionKey);
                    if (userData.logginChallenge == logginChalleng)
                    {
                        retVal = true;
                        sendNewUserToAllClients(logginStatus.m_username, 1000);
                    }
                }
                if (!retVal)
                {
                    users_list.Remove(logginStatus.m_username);
                }
            }
            return(retVal);
        }
コード例 #4
0
ファイル: MainWin.xaml.cs プロジェクト: lioris/KdcChat
        // 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
        }
コード例 #5
0
ファイル: clientChat.cs プロジェクト: lioris/KdcChat
        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;
            }
        }
コード例 #6
0
ファイル: ChatWindow.xaml.cs プロジェクト: lioris/KdcChat
        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);
        }
コード例 #7
0
ファイル: MainWin.xaml.cs プロジェクト: lioris/KdcChat
 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());
         }
     }
 }
コード例 #8
0
ファイル: KdcService.cs プロジェクト: lioris/KdcChat
        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);
        }
コード例 #9
0
ファイル: clientChat.cs プロジェクト: lioris/KdcChat
        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)
            {
            }
        }
コード例 #10
0
ファイル: ChatWindow.xaml.cs プロジェクト: lioris/KdcChat
        public void readChatMsg()
        {
            while (true)
            {
                byte[] readBuffer = new byte[1024];
                readBuffer = udpClient.Receive(ref ipPartnerEndPointRcv);

                byte[] readStringBytes = CAes.SimpleDecrypt(readBuffer, m_sessionKey, m_sessionKey);
                string retString       = Encoding.ASCII.GetString(readStringBytes);

                var evt = addLineToChatEvnt;
                if (evt != null)
                {
                    evt(this, retString);
                }
            }
        }
コード例 #11
0
        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);
        }
コード例 #12
0
ファイル: MainWin.xaml.cs プロジェクト: lioris/KdcChat
        private void finishRequstConnectionProcessUI(List <string> finishStatus)
        {
            if (finishStatus != null)
            {
                foreach (string fileName in finishStatus)
                {
                    string fileNameResultString = CAes.SimpleDecrypt(fileName, ClientAllData.Instance.getMyClient().m_ftpSessionKey, ClientAllData.Instance.getMyClient().m_ftpSessionKey);
                    ftpFilesComboBox.Items.Add(fileNameResultString);
                }

                ftpConnectButton.Visibility = Visibility.Hidden;
                ftpConnectButton.IsEnabled  = false;

                downLoadFileButton.Visibility = Visibility.Visible;
                ftpFilesComboBox.Visibility   = Visibility.Visible;

                downLoadFileButton.IsEnabled = true;
                ftpFilesComboBox.IsEnabled   = true;
            }
        }
コード例 #13
0
        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);
        }
コード例 #14
0
ファイル: KdcService.cs プロジェクト: lioris/KdcChat
        public CKdcToClientLogInData LogInApp(string userName)
        {
            if (users_list.ContainsKey(userName))
            {
                Console.WriteLine("you already logged");
                return(null);
            }

            Console.WriteLine("LOGIN: NAME {0} ", userName);

            CKdcToClientLogInData msgKdcToClientLoggin = new CKdcToClientLogInData();

            User retUserFromDB = m_DBservice.getUserByName(userName);

            if (retUserFromDB != null)
            {
                byte[] userSessionKey = CAes.NewKey();
                string challenge      = Path.GetRandomFileName();
                int    port           = 1100 + retUserFromDB.ID;
                byte[] localPortByte  = BitConverter.GetBytes(port);

                msgKdcToClientLoggin.m_username        = CAes.SimpleEncryptWithPassword(userName, retUserFromDB.PassWord);
                msgKdcToClientLoggin.m_kdcAsSessionKey = CAes.SimpleEncryptWithPassword(userSessionKey, retUserFromDB.PassWord);
                msgKdcToClientLoggin.m_challenge       = CAes.SimpleEncryptWithPassword(challenge, retUserFromDB.PassWord);
                msgKdcToClientLoggin.m_localPort       = CAes.SimpleEncryptWithPassword(localPortByte, retUserFromDB.PassWord);

                UserServiceData userServiceData = new UserServiceData(userSessionKey, OperationContext.Current.GetCallbackChannel <IClientKdcCallBack>());
                userServiceData.logginChallenge = challenge + challenge;
                users_list.Add(userName, userServiceData);
            }
            else
            {
                msgKdcToClientLoggin = null;
            }

            return(msgKdcToClientLoggin);
        }
コード例 #15
0
ファイル: ChatWindow.xaml.cs プロジェクト: lioris/KdcChat
        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;
                }
            }
        }
コード例 #16
0
        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);
            }
        }
コード例 #17
0
ファイル: ChatWindow.xaml.cs プロジェクト: lioris/KdcChat
        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;
                }
            }
        }