Beispiel #1
0
        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;
            }
        }
Beispiel #2
0
        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)
            {
            }
        }