/// <summary>Reads a short value from the stream</summary>
 /// <returns>A short value</returns>
 public ushort ReadShort()
 {
     byte[] shortArray = new byte[2];
     this.Read(shortArray, 0, 2);
     return(Converters.BigEndianToUInt16(shortArray));
 }
 /// <summary>Reads a long value from the stream</summary>
 /// <returns>A long value</returns>
 public ulong ReadLong()
 {
     byte[] longArray = new byte[8];
     this.Read(longArray, 0, 8);
     return(Converters.BigEndianToUInt64(longArray));
 }
 /// <summary>Reads an int value from the stream</summary>
 /// <returns>A int value</returns>
 public uint ReadInt()
 {
     byte[] intArray = new byte[4];
     this.Read(intArray, 0, 4);
     return(Converters.BigEndianToUInt32(intArray));
 }
        /// <summary>Encrypts password using IBM's flavor of DES algorithm as defined in RFC2877</summary>
        /// <param name="userName">User name in ASCII</param>
        /// <param name="password">Password in ASCII</param>
        /// <param name="serverSeed">Server's seed</param>
        /// <param name="clientSeed">Client's seed</param>
        /// <returns>Encrypted password as EBCDIC byte stream</returns>
        public static byte[] EncryptPasswordDES(string userName, string password, ulong serverSeed, ulong clientSeed)
        {
            byte[] passwordToken = new byte[8];
            if (password.Length > 8)
            {
                byte[] passwordTokenA = GenerateToken(userName, password.Substring(0, 8));
                byte[] passwordTokenB = GenerateToken(userName, password.Substring(8));
                passwordToken = Converters.UInt64ToBigEndian(Converters.BigEndianToUInt64(passwordTokenA) ^ Converters.BigEndianToUInt64(passwordTokenB));
            }
            else
            {
                passwordToken = GenerateToken(userName, password);
            }

            byte[] usernameEBCDIC_A;
            byte[] usernameEBCDIC_B;

            if (userName.Length <= 8)
            {
                usernameEBCDIC_A = Converters.AsciiToEbcdic(userName.ToUpper().PadRight(8));
                usernameEBCDIC_B = Converters.UInt64ToBigEndian(0x4040404040404040);
            }
            else
            {
                usernameEBCDIC_A = Converters.AsciiToEbcdic(userName.Substring(0, 8).ToUpper().PadRight(8));
                usernameEBCDIC_B = Converters.AsciiToEbcdic(userName.Substring(8).ToUpper().PadRight(8));
            }

            byte[] firstEncryptionRound  = EncryptDES(Converters.UInt64ToBigEndian(serverSeed + 1), passwordToken);
            byte[] secondEncryptionRound = EncryptDES(Converters.UInt64ToBigEndian(Converters.BigEndianToUInt64(firstEncryptionRound) ^ clientSeed), passwordToken);
            byte[] thirdEncryptionRound  = EncryptDES(Converters.UInt64ToBigEndian(Converters.BigEndianToUInt64(usernameEBCDIC_A) ^ (serverSeed + 1) ^ Converters.BigEndianToUInt64(secondEncryptionRound)), passwordToken);
            byte[] fourthEncryptionRound = EncryptDES(Converters.UInt64ToBigEndian(Converters.BigEndianToUInt64(usernameEBCDIC_B) ^ (serverSeed + 1) ^ Converters.BigEndianToUInt64(thirdEncryptionRound)), passwordToken);
            return(EncryptDES(Converters.UInt64ToBigEndian(Converters.BigEndianToUInt64(fourthEncryptionRound) ^ 0x0000000000000001), passwordToken));
        }