Microsoft.SqlServer.TDS.TDSUtilities.WritePasswordString C# (CSharp) Method

WritePasswordString() static private method

Write password string encrypted into the packet
static private WritePasswordString ( Stream destination, string value ) : void
destination Stream
value string
return void
        internal static void WritePasswordString(Stream destination, string value)
        {
            // Check if value is null
            if (string.IsNullOrEmpty(value))
            {
                // There's nothing to write
                return;
            }

            // Convert
            byte[] byteString = Encoding.Unicode.GetBytes(value);

            // Perform password decryption
            for (int i = 0; i < byteString.Length; i++)
            {
                // Swap 4 high bits with 4 low bits
                byteString[i] = (byte)(((byteString[i] & 0xf0) >> 4) | ((byteString[i] & 0xf) << 4));

                // XOR
                byteString[i] ^= 0xA5;
            }

            // Write into a the stream
            destination.Write(byteString, 0, byteString.Length);
        }