DecodeUInt32BE() public static method

Decodes a uint from a byte-array, in big-endian encoding.
public static DecodeUInt32BE ( byte buffer, int offset ) : uint
buffer byte /// A array which contains the . ///
offset int /// The index in at which the starts. ///
return uint
Beispiel #1
0
 /// <summary>
 /// Reads a <see cref="uint"/> in big-endian encoding from the stream and advances the
 /// position within the stream by two bytes.
 /// </summary>
 /// <returns>
 /// The <see cref="uint"/> which was read from the stream.
 /// </returns>
 public uint ReceiveUInt32BE()
 {
     return(VncUtility.DecodeUInt32BE(this.Receive(4), 0));
 }
        void NegotiateSecurity()
        {
            int count = ReceiveByte();

            if (count == 0)
            {
                string message = ReceiveString().Trim('\0');
                Require(false, message, VncFailureReason.ServerOfferedNoAuthenticationMethods);
            }

            var types = new List <VncSecurityType>();

            for (int i = 0; i < count; i++)
            {
                types.Add((VncSecurityType)ReceiveByte());
            }

            if (types.Contains(VncSecurityType.None))
            {
                Send(new[] { (byte)VncSecurityType.None });
            }
            else if (types.Contains(VncSecurityType.Vnc))
            {
                if (_options.Password == null)
                {
                    OnPasswordRequired(new PasswordRequiredEventArgs(_options));
                    Require(_options.Password != null, "Password required.",
                            VncFailureReason.PasswordRequired);
                }

                Send(new[] { (byte)VncSecurityType.Vnc });

                var challenge = Receive(16);
                var response  = new byte[16];
                var password  = _options.Password;

                var key   = new byte[8];
                var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(password);

                try
                {
                    Array.Copy(bytes, 0, key, 0, Math.Min(bytes.Length, key.Length));
                    for (int i = 0; i < key.Length; i++)
                    {
                        key[i] = ReverseBits(key[i]);
                    }

                    using (var des = new DESCryptoServiceProvider()
                    {
                        Key = key, Mode = CipherMode.ECB
                    })
                        using (var encryptor = des.CreateEncryptor())
                        {
                            encryptor.TransformBlock(challenge, 0, 16, response, 0);
                            Send(response);
                        }
                }
                finally
                {
                    Array.Clear(bytes, 0, bytes.Length);
                    Array.Clear(key, 0, key.Length);
                    Array.Clear(response, 0, response.Length);
                }
            }
            else
            {
                Require(false, "No supported authentication methods.",
                        VncFailureReason.NoSupportedAuthenticationMethods);
            }

            uint status = VncUtility.DecodeUInt32BE(Receive(4), 0);

            if (status != 0)
            {
                string message = ReceiveString().Trim('\0');
                Require(false, message, VncFailureReason.AuthenticationFailed);
            }
        }