LINQ-to-Entities with 8-bit ASCII data
===

I have an interesting issue handling 8-bit "ASCII" characters in LINQ-to-Entities and am hoping someone can give me a tip.

I have inherited a SQL Server 2000 database that has some pseudo-encrypted columns where they just XOR'd the string with 0xFF.  Don't know why and I know it's lame but that's where we are now.

These columns are of SQL datatype char(7) and char(14).  When you XOR using 0xFF you get the 8th bit set in every case, so you end up with non-ASCII (by Microsoft's definition anyway) characters.  UTF-8 seems to be indicated here but the decoding gets messed up.

I am able to read and decode these strings as follows:

1) Get the field using LINQ as a String.
2) Get a byte[] using System.Text.Encoding.GetEncoding(1252).GetBytes()
3) Decode by XORing each byte with 0xFF
4) Return the decoded string with System.Text.Encoding.GetEncoding(1252).GetString()

This works perfectly.

The problem I'm having is that I can't seem to put an ENCODED string back to SQL Server using LINQ.

I am basically following the reverse process and am doing:

1) Get the bytes using ASCIIEncoding.GetBytes().  (No need for CodePage 1252 here as this is a straight string.)
2) Encode the bytes with 0xFF.
3) Return the encoded string with GetEncoding(1252).GetString().

If I look at my string, it's exactly what I'd expect.  But if I stuff that in my entity and do a SaveChanges().  The resultant value in SQL Server is always "?????" of some length.

I'm sure I'm missing something here but I've tried everything I can think of and can't get it.  For now I just fell back to the old-fashioned way of using a SqlCommand and doing an UPDATE with the encoded strings as SqlParameters.  No problem there, works every time.

Thanks in advance for any assistance.