コード例 #1
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);
        }
コード例 #2
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);
            }
        }
コード例 #3
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
        }
コード例 #4
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;
            }
        }
コード例 #5
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)
            {
            }
        }
コード例 #6
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);
                }
            }
        }
コード例 #7
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);
        }
コード例 #8
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;
            }
        }
コード例 #9
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);
        }
コード例 #10
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;
                }
            }
        }
コード例 #11
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;
                }
            }
        }