Example #1
0
        public void SetKeys(TanksCommon.Encryption.EncryptioinKeys key)
        {
            var aes = new AesCryptoServiceProvider();
            var iv  = aes.IV;

            // Encrypt the session key
            RSAOAEPKeyExchangeFormatter keyFormatter = new RSAOAEPKeyExchangeFormatter(key.RsaKey);
            var encryptedSessionKey = keyFormatter.CreateKeyExchange(aes.Key, typeof(Aes));

            key.SetIvAndSessionKey(iv, encryptedSessionKey);

            this._cryptoTransform = aes.CreateEncryptor();
        }
Example #2
0
        public void EncryptDecryptTest()
        {
            byte[] messageBytes = null;
            using (var stream = new System.IO.MemoryStream())
            {
                var theObject = new TanksCommon.SharedObjects.GameMove()
                {
                    MessageId = 8
                };
                var messageStream = TanksCommon.MessageEncoder.EncodeMessage(stream, theObject);
                messageStream.Seek(0, System.IO.SeekOrigin.Begin);
                messageBytes = messageStream.ToArray();
            }

            //can only encrypt messages, only has public key
            TanksCommon.Encryption.EncryptioinKeys clientKey = new TanksCommon.Encryption.EncryptioinKeys();
            //can decrypt messages, has public and private key
            TanksCommon.Encryption.EncryptioinKeys serverKey = new TanksCommon.Encryption.EncryptioinKeys();

            //server provides public RSA keys and client imports them
            clientKey.ImportPublicKey(serverKey.ExportPublicKey());

            //client can encrypt AES keys with the provided RSA keys
            var clientEncryptor = new GameCom.Encrypt(clientKey);

            //server sets public AES keys
            serverKey.SetIvAndSessionKey(clientKey.Iv, clientKey.SessionKey);

            var e1 = clientEncryptor.EncryptBytes(messageBytes);

            var d1 = GameCom.Encrypt.DecryptBytes(serverKey, e1);

            Assert.IsFalse(messageBytes.SequenceEqual(e1));

            Assert.IsTrue(messageBytes.SequenceEqual(d1));

            //serverKey.SetIvAndSessionKey(clientKey.Iv, clientKey.SessionKey);

            /*var serverEncrypter = new GameCom.Encrypt(serverKey);
             *
             * var e2 = serverEncrypter.EncryptBytes(messageBytes);
             *
             * var d2 = GameCom.Encrypt.DecryptBytes(clientKey, e2);
             *
             * Assert.IsTrue(d2.SequenceEqual(messageBytes));*/
        }
Example #3
0
        private void AcknowledgeMessage(byte[] messageBytes)
        {
            var stream = new System.IO.MemoryStream(messageBytes);
            //var message = TanksCommon.MessageDecoder.DecodeMessage<TanksCommon.SharedObjects.DataReceived>(stream);
            short messageType = TanksCommon.MessageDecoder.DecodeMessageType(stream);

            _log.Debug("Received Message Type: " + messageType);
            CallReceivedDataLog("Received Message Type: " + messageType);
            if (messageType == 500)
            {
                _log.Debug("Recieved RSA Public Key");
                CallReceivedDataLog("Recieved RSA Public Key");
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                SendObjectToTcpClient(new TanksCommon.SharedObjects.DataReceived()
                {
                    MessageId = message.MessageId, Id = 99
                });

                var rsaPublicKeys = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.Encryption.RsaPublicKey>(stream);
                encryptioinKeys.ImportPublicKey(rsaPublicKeys.Key);

                aesEncrypter = new Encrypt(encryptioinKeys);
                _log.Debug("Sending AES Public Keys");
                CallReceivedDataLog("Sending AES Public Keys");
                SendObjectToTcpClient(new TanksCommon.Encryption.AesPublicKey()
                {
                    Iv = encryptioinKeys.Iv, SessionKey = encryptioinKeys.SessionKey
                });
            }
            else if (messageType == 501)
            {
                _log.Debug("Recieved AES Public Keys");
                CallReceivedDataLog("Recieved AES Public Keys");
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                SendObjectToTcpClient(new TanksCommon.SharedObjects.DataReceived()
                {
                    MessageId = message.MessageId, Id = 99
                });

                var aesPublicKeys = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.Encryption.AesPublicKey>(stream);
                encryptioinKeys.SetIvAndSessionKey(aesPublicKeys.Iv, aesPublicKeys.SessionKey);
            }
            else if (messageType == 900)
            {
                //resend message
                HandleDebugRecievedMessage?.Invoke(messageBytes);
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                if (_messageHistory.ContainsKey(message.MessageId))
                {
                    SendObjectToTcpClient(_messageHistory[message.MessageId]);
                }
                else
                {
                    _log.Error("Peer is requesting Message that is not in message history");
                    CallReceivedDataLog("Peer is requesting Message that is not in message history");
                }
            }
            else if (messageType != 99)
            {
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                SendObjectToTcpClient(new TanksCommon.SharedObjects.DataReceived()
                {
                    MessageId = message.MessageId, Id = 99
                });
            }
            else
            {
                //remove message from queue, marking it complete
                var message = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.DataReceived>(stream);
                var top     = new MessageInfo <TanksCommon.SharedObjects.IMessage>();
                if (_messageQueue.TryPeek(out top))
                {
                    if (top.OriginalMessage.MessageId == message.MessageId)
                    {
                        //success remove from queue
                        _messageQueue.TryDequeue(out top);
                    }
                }
                else
                {
                    _log.Error("Message not found in queue");
                }
            }
        }