Beispiel #1
0
        public void DecryptData()
        {
            var decrypted = Aes.DecryptByteArray(EncryptedMessage, Key);

            Assert.NotNull(decrypted);
            Assert.Equal(DecryptedMessage.Length, decrypted.Length);
            Assert.Equal(DecryptedMessage, decrypted);
        }
Beispiel #2
0
        public void EncryptDecryptData()
        {
            var encrypted = Aes.EncryptByteArray(Encoding.Default.GetBytes(DecryptedMessage), Key);
            var decrypted = Aes.DecryptByteArray(encrypted, Key);

            Assert.NotNull(encrypted);
            Assert.NotNull(decrypted);
            Assert.Equal(DecryptedMessage, decrypted);
        }
Beispiel #3
0
        public void EncryptDecryptByteArrayWithGeneratedKey(int keySize)
        {
            byte[] decryptedMessageBytes = Encoding.UTF8.GetBytes(DecryptedMessage);
            byte[] generatedKey          = Convert.FromBase64String(Aes.GenerateKey(keySize));
            byte[] encryptedData         = Aes.EncryptByteArray(decryptedMessageBytes, generatedKey);

            Assert.NotNull(encryptedData);

            string decryptedData = Aes.DecryptByteArray(encryptedData, generatedKey);

            Assert.NotNull(decryptedData);
            Assert.Equal(DecryptedMessage, decryptedData);
        }
Beispiel #4
0
        public void Deserialize(INetPacketStream packet)
        {
            BuildVersion = packet.Read <string>();
            Username     = packet.Read <string>();
            Password     = null;

            if (_configuration.PasswordEncryption)
            {
                byte[] encryptedPassword = packet.Read <byte>(16 * 42);
                byte[] encryptionKey     = Aes.BuildEncryptionKeyFromString(_configuration.EncryptionKey, 16);

                Password = Aes.DecryptByteArray(encryptedPassword, encryptionKey);
            }
            else
            {
                Password = packet.Read <string>();
            }
        }
Beispiel #5
0
        public static void OnLogin(LoginClient client, INetPacketStream packet)
        {
            var    loginServer   = DependencyContainer.Instance.Resolve <ILoginServer>();
            var    configuration = DependencyContainer.Instance.Resolve <LoginConfiguration>();
            var    certifyPacket = new CertifyPacket(packet, configuration.PasswordEncryption);
            string password      = null;

            if (certifyPacket.BuildVersion != configuration.BuildVersion)
            {
                AuthenticationFailed(client, ErrorType.CERT_GENERAL, "bad client build version");
                return;
            }

            if (configuration.PasswordEncryption)
            {
                byte[] encryptionKey = Aes.BuildEncryptionKeyFromString(configuration.EncryptionKey, 16);

                password = Aes.DecryptByteArray(certifyPacket.EncryptedPassword, encryptionKey);
            }
            else
            {
                password = packet.Read <string>();
            }

            var authenticationService = DependencyContainer.Instance.Resolve <IAuthenticationService>();
            var authenticationResult  = authenticationService.Authenticate(certifyPacket.Username, password);

            switch (authenticationResult)
            {
            case AuthenticationResult.BadUsername:
                AuthenticationFailed(client, ErrorType.FLYFF_ACCOUNT, "bad username");
                break;

            case AuthenticationResult.BadPassword:
                AuthenticationFailed(client, ErrorType.FLYFF_PASSWORD, "bad password");
                break;

            case AuthenticationResult.AccountSuspended:
                // TODO
                break;

            case AuthenticationResult.AccountTemporarySuspended:
                // TODO
                break;

            case AuthenticationResult.AccountDeleted:
                AuthenticationFailed(client, ErrorType.ILLEGAL_ACCESS, "logged in with deleted account");
                break;

            case AuthenticationResult.Success:
                if (loginServer.IsClientConnected(certifyPacket.Username))
                {
                    AuthenticationFailed(client, ErrorType.DUPLICATE_ACCOUNT, "client already connected", disconnectClient: false);
                    return;
                }

                using (var database = DependencyContainer.Instance.Resolve <IDatabase>())
                {
                    var user = database.Users.Get(x => x.Username.Equals(certifyPacket.Username, StringComparison.OrdinalIgnoreCase));

                    if (user == null)
                    {
                        AuthenticationFailed(client, ErrorType.ILLEGAL_ACCESS, "Cannot find user in database");
                        Logger.LogCritical($"Cannot find user '{certifyPacket.Username}' in database to update last connection time.");
                        return;
                    }

                    user.LastConnectionTime = DateTime.UtcNow;
                    database.Users.Update(user);
                    database.Complete();
                }

                LoginPacketFactory.SendServerList(client, certifyPacket.Username, loginServer.GetConnectedClusters());
                client.SetClientUsername(certifyPacket.Username);
                Logger.LogInformation($"User '{client.Username}' logged succesfully from {client.RemoteEndPoint}.");
                break;

            default:
                break;
            }
        }