I don't know C # either, so this is probably seriously non-idiomatic.
static string EncodeDecode(string str) { byte[] hash = new byte[63] { 94, 37, 49, 50, 104, 68, 86, 106, 69, 68, 49, 126, 126, 35, 50, 57, 97, 102, 100, 109, 83, 68, 96, 54, 90, 118, 85, 89, 64, 104, 98, 107, 68, 66, 67, 51, 102, 110, 55, 89, 55, 101, 117, 70, 124, 82, 55, 57, 51, 52, 48, 57, 51, 42, 55, 97, 45, 124, 45, 32, 32, 81, 96 }; Encoding ANSI = Encoding.GetEncoding(1252); byte[] input = ANSI.GetBytes(str); byte[] output = new byte[input.Length]; for (int i = 0; i < input.Length; i++) output[i] = (byte)(input[i] ^ ~hash[(i + 1) % hash.Length]); return ANSI.GetString(output); }
I assumed that your ANSI strings were encoded using Windows 1252, but you must have encoded your legacy data using a different code page, it is pretty obvious how to change it.
Since C # does not have the equivalent of 8-bit Delphi string types, it would be very difficult for me personally to use byte[] rather than string .
Done looks like this:
static byte[] EncodeDecode(byte[] input) { byte[] hash = new byte[63] { 94, 37, 49, 50, 104, 68, 86, 106, 69, 68, 49, 126, 126, 35, 50, 57, 97, 102, 100, 109, 83, 68, 96, 54, 90, 118, 85, 89, 64, 104, 98, 107, 68, 66, 67, 51, 102, 110, 55, 89, 55, 101, 117, 70, 124, 82, 55, 57, 51, 52, 48, 57, 51, 42, 55, 97, 45, 124, 45, 32, 32, 81, 96 }; byte[] output = new byte[input.Length]; for (int i = 0; i < input.Length; i++) output[i] = (byte)(input[i] ^ ~hash[(i + 1) % hash.Length]); return output; }
@Groo makes an excellent point at which a hash can be initialized with a cleaner listing of this:
byte[] hash = ANSI.GetBytes(@"^%12hDVjED1~~#29afdmSD`6ZvUY@hbkDBC3fn7Y7euF|R7934093*7a-|- Q`");