Example #1
0
        public static int Establish(ref byte[] EncryptionKeyServer, ref byte[] Salt, HenkTcpClient Client, string Password, string UserName, RSAKey RSAKey)
        {
            try
            {
                EncryptionKeyServer = Encryption.RSA.Decrypt(Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 1 }, RSAKey.PublicKey), TimeSpan.FromSeconds(TIMESPAN)).Data, RSAKey.PrivateKey);
                Client.SetEncryption(Aes.Create(), EncryptionKeyServer);
                Salt = Client.WriteAndGetReply(new byte[] { 42, 2 }, TimeSpan.FromSeconds(1)).DecryptedData;

                Rfc2898DeriveBytes HashedPassword = new Rfc2898DeriveBytes(Password, Salt, 250000);
                byte ValidPassword = Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 3 }, HenkTcp.Encryption.Encrypt(Aes.Create(), HashedPassword.GetBytes(20), EncryptionKeyServer)), TimeSpan.FromSeconds(TIMESPAN)).Data[0];

                if (ValidPassword.Equals(1))
                {
                    byte ValidUserName = Client.WriteAndGetReply(CombineBytes(new byte[] { 42, 4 }, AES256.encrypt(UserName, Password, Salt)), TimeSpan.FromSeconds(TIMESPAN)).Data[0];
                    if (ValidUserName.Equals(1))
                    {
                        return(3);                        //evrything ok
                    }
                    else
                    {
                        Client.Disconnect(); return(2);
                    }                                      //UserName already taken
                }
                else
                {
                    Client.Disconnect(); return(1);
                }                                      //Wrong password
            }
            catch { return(0); }//server did not reply on a message
        }