public class MyClass
{
public static void Main()
{
string strLock = "Hello World";
int len = strLock.Length;
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] bytLock = ascii.GetBytes(strLock);
byte[] key = new byte[len];
for (int i = 1; i < len; i++)
key[i] =(byte) (bytLock[i] ^ bytLock[i-1]);
// note this leave key[0] unassigned. Are you sure you want that?
// key is not guaranteed to contain codes for good ASCII characters,
// so the following probably won't work right.
// You probably really want the byte array "key"
string strKey = ascii.GetString(key);
Console.WriteLine(strKey);
}
}
--
Truth,
James Curran
Home:
www.noveltheory.com Work:
www.njtheater.com
Blog:
www.honestillusion.com Day Job:
www.partsearch.com
(note new day job!)
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:uX**************@TK2MSFTNGP14.phx.gbl...
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...
perhaps: key[i] = (int)lock[i] ^ (int)lock[i-1]; ????
"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP**********************@msnews.microsoft.com ... Dirk Reske <_F*******@gmx.net> wrote: i have following source, to be translatet into C#
for (i = 1; i < len; i++)
key[i] = lock[i] xor lock[i-1];
key and lock are strings
in C# xor = ^
but in c# i cannot apply the ^ operator on characters.
How can I solve this?
Well, XOR isn't really a logical operation on characters. While you
*can* do a bitwise XOR on the code points, there's no guarantee that
you'll get back a character which will be printable or anything like
that. It's generally a bad idea to treat character data as if it was
binary data.
What is the above used for?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too