Bit manipulation from a ByteArray
listed in answer
ANSWER:
Your first problem is that you have an array of 32 bytes that you’re converting to a BitArray. You’ll find that the value of the source.Count property is 256. target.Count, on the other hand, is only 32. So your Encrypt method only changes the first 32 bits of the bit array, which corresponds four bytes — eight of your hexadecimal characters. The rest of your array will be nulls.
You can verify this by changing your BitArrayToByteArray method to fill the destination with 0xFF before doing the copy:
public static byte[] BitArrayToByteArray(BitArray bits)
byte[] bytes = new byte[bits.Length / 8];
// Fill the array with 0xFF to illustrate.
for (int i = 0; i < bytes.Length; ++i)
bytes[i] = 0xFF;
bits.CopyTo(bytes, 0);
return bytes;
}
I think you'll find that the result is "50120000FFFFFF...."
It's hard to tell exactly what you're trying to do. If you want to scramble the bytes in your string, there's no need to use a BitArray. If you really want to scramble the bits, then you need to scramble ALL the bits. Without more information about what you're trying to do, I don't have a suggestion. Except that perhaps you should use one of the many existing encryption algorithms rather than trying to roll your own.
by Jim Mischel from http://stackoverflow.com/questions/10443004

New Comments