public ServerAuthResponse AuthenticateClient(string username, BigInteger A)
        {
            if (A == 0)
            {
                throw new AuthenticationFailedException();
            }

            this.A = A;
            var        random = new Random();
            BigInteger b      = random.Next(int.MaxValue / 2, int.MaxValue);

            B = factors.k * v + BigInteger.ModPow(factors.g, b, factors.N);

            var u = ShaHashing.GenerateSha512Hash(A + B.ToString());

            if (u == 0)
            {
                throw new ConnectionInterruptedException();
            }

            S = BigInteger.ModPow(
                A * BigInteger.ModPow(v, u, factors.N),
                b,
                factors.N);

            K = ShaHashing.GenerateSha512Hash(S.ToString());

            SRPManager.DisplayAuthenticationOnServerSide(b, B, u, S, K);

            return(new ServerAuthResponse(s, B));
        }
        public void Registration()
        {
            generateS();
            x = ShaHashing.GenerateSha512Hash(s + password);
            v = BigInteger.ModPow(factors.g, x, factors.N);

            server.RegisterClient(username, s, v);

            SRPManager.DisplayRegistration(s, x, v);
        }
        public void GenerateConfirmation()
        {
            var M = ShaHashing.GenerateSha512Hash(
                XOR(
                    ShaHashing.GenerateSha512Hash(factors.N.ToString()).ToByteArray(),
                    ShaHashing.GenerateSha512Hash(factors.g.ToString()).ToByteArray())
                + ShaHashing.GenerateSha512Hash(username)
                + S + A.ToString() + B.ToString() + factors.k);

            var serverR = server.ConfirmClientAccess(M);

            var clientR = ShaHashing.GenerateSha512Hash(
                A.ToString() + M.ToString() + K.ToString());

            if (clientR != serverR)
            {
                throw new ConfirmationFailedException();
            }

            SRPManager.DisplayConfirmationOnClientSide(clientR);
        }
        public BigInteger ConfirmClientAccess(BigInteger clientM)
        {
            var serverM = ShaHashing.GenerateSha512Hash(
                XOR(
                    ShaHashing.GenerateSha512Hash(factors.N.ToString()).ToByteArray(),
                    ShaHashing.GenerateSha512Hash(factors.g.ToString()).ToByteArray())
                + ShaHashing.GenerateSha512Hash(username)
                + S + A.ToString() + B.ToString() + factors.k);

            if (serverM != clientM)
            {
                throw new ConfirmationFailedException();
            }

            var R = ShaHashing.GenerateSha512Hash(
                A.ToString() + serverM.ToString() + K.ToString());

            SRPManager.DisplayConfirmationOnServerSide(clientM, serverM, R);

            return(R);
        }
        public void Authentication()
        {
            var        random = new Random();
            BigInteger a      = random.Next(int.MaxValue / 2, int.MaxValue);

            A = BigInteger.ModPow(factors.g, a, factors.N);

            SRPManager.DisplayAuthenticationBeforeSending(a, A);

            var response = server.AuthenticateClient(username, A);

            B = response.B;

            if (B == 0)
            {
                throw new AuthenticationFailedException();
            }

            var u = ShaHashing.GenerateSha512Hash(A + B.ToString());

            if (u == 0)
            {
                throw new ConnectionInterruptedException();
            }

            S = BigInteger.ModPow(
                B - factors.k * BigInteger.ModPow(factors.g, x, factors.N),
                (a + BigInteger.Multiply(u, x)),
                factors.N);

            K = ShaHashing.GenerateSha512Hash(S.ToString());

            SRPManager.DisplayAuthenticationAfterSending(u, S, K);

            GenerateConfirmation();
        }