Пример #1
0
        public NetBuffer Decrypt(NetBuffer inBuf)
        {
            NetworkLog.Debug(NetworkLogFlags.Encryption, "Decrypting message using RSA");
            //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes));

            byte[] encrypted = inBuf.Data.SubArray(1, inBuf.LengthBytes - 1);
            byte[] decrypted;

            try
            {
                decrypted = _rsa.Decrypt(encrypted, false);
            }
            catch (Exception ex)
            {
                NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to decrypt message using RSA: ", ex);
                return(null);
            }

            var outBuf = new NetBuffer();

            outBuf.Data        = decrypted;
            outBuf.LengthBytes = decrypted.Length;

            //Log.Debug(NetworkLogFlags.Encryption, "Decrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes));
            return(outBuf);
        }
Пример #2
0
        public NetBuffer Encrypt(NetBuffer inBuf)
        {
            NetworkLog.Debug(NetworkLogFlags.Encryption, "Encrypting message using RSA");
            //Log.Debug(NetworkLogFlags.Encryption, "PlainData: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes));

            byte[] unencrypted = inBuf.Data.SubArray(0, inBuf.LengthBytes);
            byte[] encrypted;

            try
            {
                encrypted = _rsa.Encrypt(unencrypted, false);
            }
            catch (Exception ex)
            {
                NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to encrypt message using RSA: ", ex);
                return(null);
            }

            ArrayUtility.Push(ref encrypted, NetworkMessage.ENCRYPTED_SIGNATURE);

            var outBuf = new NetBuffer();

            outBuf.Data        = encrypted;
            outBuf.LengthBytes = encrypted.Length;

            //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes));
            return(outBuf);
        }
        public static string GetHostName()
        {
#if UNITY_BUILD
            if (UnityEngine.Application.webSecurityEnabled)
            {
                NetworkLog.Debug(NetworkLogFlags.Utility, "Can't get local host name when running as a webplayer");
                return("localhost");
            }
#endif
            return(Dns.GetHostName());
        }
        public static IPAddress TryGetLocalIP()
        {
            IPAddress ip = null;

            try
            {
                ip = NetUtility.Resolve(GetHostName());
            }
            catch (Exception ex)
            {
                NetworkLog.Debug(NetworkLogFlags.Utility, "Failed to get local ip address:", ex);
            }

            return(ip ?? IPAddress.Loopback);
        }
        public static IPAddress Resolve(string host)
        {
            IPAddress ip = null;

            try
            {
                ip = NetUtility.Resolve(host);
            }
            catch (Exception ex)
            {
                NetworkLog.Debug(NetworkLogFlags.Utility, "Failed to resolve host '", host, "':", ex);
            }

            Assert(ip != null, "Unable to resolve host");
            return(ip);
        }
Пример #6
0
        public NetBuffer Encrypt(NetBuffer inBuf)
        {
            NetworkLog.Debug(NetworkLogFlags.Encryption, "Encrypting message using AES");
            //Log.Debug(NetworkLogFlags.Encryption, "PlainData: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes));

            MemoryStream ms = null;
            CryptoStream cs = null;

            try
            {
                ms = new MemoryStream();
                ms.WriteByte(NetworkMessage.ENCRYPTED_SIGNATURE);

                var iv = new byte[IV_SIZE];
                NetRandom.Instance.NextBytes(iv);
                ms.Write(iv, 0, IV_SIZE);

                var encryptor = _aes.CreateEncryptor(_aes.Key, iv);
                cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);

                cs.Write(inBuf.Data, 0, inBuf.LengthBytes);
                cs.FlushFinalBlock();
            }
            catch (Exception ex)
            {
                NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to encrypt message using AES: ", ex);
                return(null);
            }
            finally
            {
                if (cs != null)
                {
                    cs.Close();
                }
                if (ms != null)
                {
                    ms.Close();
                }
            }

            var encrypted = ms.ToArray();
            var outBuf    = new NetBuffer(encrypted, 0, encrypted.Length * 8);

            //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes));
            return(outBuf);
        }
Пример #7
0
        public NetBuffer Decrypt(NetBuffer inBuf)
        {
            NetworkLog.Debug(NetworkLogFlags.Encryption, "Decrypting message using AES");
            //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes));

            var          ms = new MemoryStream(inBuf.Data, 1, inBuf.LengthBytes - 1);
            CryptoStream cs = null;

            byte[] decrypted;
            int    decryptedCount;

            try
            {
                var iv = new byte[IV_SIZE];
                ms.Read(iv, 0, IV_SIZE);

                var decryptor = _aes.CreateDecryptor(_aes.Key, iv);
                cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

                decrypted      = new byte[inBuf.LengthBytes];
                decryptedCount = cs.Read(decrypted, 0, decrypted.Length);
            }
            catch (Exception ex)
            {
                NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to decrypt message using AES: ", ex);
                return(null);
            }
            finally
            {
                if (cs != null)
                {
                    cs.Close();
                }
                ms.Close();
            }

            var outBuf = new NetBuffer(decrypted, 0, decryptedCount * 8);

            //Log.Debug(NetworkLogFlags.Encryption, "Decrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes));
            return(outBuf);
        }