I’m trying to encrypt my bytes using XOR, based on a Java code:
public static void encXORPass(byte[] raw, final int offset, final int size, int key)
{
int stop = size - 8;
int pos = 4 + offset;
int edx;
int ecx = key; // Initial xor key
while (pos < stop)
{
edx = raw[pos] & 0xFF;
edx |= (raw[pos + 1] & 0xFF) << 8;
edx |= (raw[pos + 2] & 0xFF) << 16;
edx |= (raw[pos + 3] & 0xFF) << 24;
ecx += edx;
edx ^= ecx;
raw[pos++] = (byte) (edx & 0xFF);
raw[pos++] = (byte) (edx >> 8 & 0xFF);
raw[pos++] = (byte) (edx >> 16 & 0xFF);
raw[pos++] = (byte) (edx >> 24 & 0xFF);
}
raw[pos++] = (byte) (ecx & 0xFF);
raw[pos++] = (byte) (ecx >> 8 & 0xFF);
raw[pos++] = (byte) (ecx >> 16 & 0xFF);
raw[pos] = (byte) (ecx >> 24 & 0xFF);
}
Packet before encrypting, in the Java code:
[0, 0, 0, 61, -9, -35, -57, 33, -58, 0, 0, 64, -119, -120, -62, -8, -100, 52, -37, -31, 74, -12, -13, -60, 27, -2, 62, 100, 0, 64, -64, 74, -58, -90, -77, -7, -125, -62, -7, -99, -39, 91, 55, -78, -67, 52, 14, 56, -41, -92, 60, -35, 109, 10, 83, -96, -14, -121, 53, -19, 112, -78, -4, 22, -56, -26, -53, -118, 37, -82, 80, 108, -21, 32, 59, 4, 110, -73, -85, -43, 74, 102, 83, 119, -78, -111, -116, -93, -92, 41, -41, -44, 19, -92, 25, 67, 10, -110, 122, 92, -22, -44, 41, -112, 84, -48, 101, -114, 80, -46, 118, 49, 100, -126, -50, -13, -77, 61, -23, -42, -44, 43, 51, 83, 74, -127, -1, 100, -70, -57, 6, 115, 125, 84, -36, -11, 17, -97, -14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -45, 97, -81, -115, -76, 69, -29, 56, -11, -32, 86, -116, -49, -123, 35, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I used the key: 1572995469
My code in Golang:
func encXORPass(raw []byte, offset, size, key int) []byte {
stop := size - 8
pos := 4 + offset
ecx := key
for pos < stop {
edx := int(raw[pos]) & 0xFF
edx |= (int(raw[pos+1]) & 0xFF) << 8
edx |= (int(raw[pos+2]) & 0xFF) << 16
edx |= (int(raw[pos+3]) & 0xFF) << 24
ecx += edx
edx ^= ecx
raw[pos] = byte(edx & 0xFF)
raw[pos+1] = byte(edx >> 8 & 0xFF)
raw[pos+2] = byte(edx >> 16 & 0xFF)
raw[pos+3] = byte(edx >> 24 & 0xFF)
pos += 4
}
raw[pos] = byte(ecx & 0xFF)
raw[pos+1]
= byte(ecx >> 8 & 0xFF)
raw[pos+2] = byte(ecx >> 16 & 0xFF)
raw[pos+3] = byte(ecx >> 24 & 0xFF)
return raw
}
I created the test:
func TestXor(t *testing.T) {
data := []byte{
0, 0, 0, 61, 247, 221, 199, 33, 198, 0, 0, 64, 137, 136, 194, 248, 156, 52, 219, 225, 74, 244, 243, 196, 27, 254, 62, 100, 0, 64, 192, 74, 198, 166, 179, 249, 131, 194, 249, 157, 217, 91, 55, 178, 189, 52, 14, 56, 215, 164, 60, 221, 109, 10, 83, 160, 242, 135, 53, 237, 112, 178, 252, 22, 200, 230, 203, 138, 37, 174, 80, 108, 235, 32, 59, 4, 110, 183, 171, 213, 74, 102, 83, 119, 178, 145, 140, 163, 164, 41, 215, 212, 19, 164, 25, 67, 10, 146, 122, 92, 234, 212, 41, 144, 84, 208, 101, 142, 80, 210, 118, 49, 100, 130, 206, 243, 179, 61, 233, 214, 212, 43, 51, 83, 74, 129, 255, 100, 186, 199, 6, 115, 125, 84, 220, 245, 17, 159, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 211, 97, 175, 141, 180, 69, 227, 56, 245, 224, 86, 140, 207, 133, 35, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
want := []byte{0, 0, 0, 61, 247, 221, 147, 2, 78, 94, 84, 35, 152, 111, 212, 164, 50, 47, 42, 220, 179, 251, 23, 198, 14, 240, 28, 3, 21, 14, 34, 251, 29, 82, 38, 82, 220, 117, 119, 212, 224, 72, 242, 73, 75, 115, 221, 11, 25, 72, 51, 204, 81, 253, 49, 17, 220, 248, 162, 115, 239, 133, 111, 163, 175, 254, 149, 202, 168, 104, 254, 192, 147, 199, 210, 180, 136, 41, 63, 83, 123, 99, 180, 138, 81, 7, 255, 2, 44, 233, 157, 162, 143, 192, 122, 250, 172, 100, 167, 73, 123, 31, 47, 54, 177, 75, 14, 186, 102, 188, 151, 84, 254, 114, 97, 170, 253, 19, 113, 230, 247, 113, 248, 208, 39, 90, 53, 140, 157, 100, 214, 40, 216, 163, 112, 164, 166, 9, 108, 82, 183, 150, 158, 82, 183, 150, 158, 82, 183, 150, 158, 82, 183, 150, 158, 246, 120, 233, 166, 110, 27, 202, 91, 58, 223, 214, 99, 81, 64, 128, 51, 159, 197, 163, 93, 159, 197, 163, 93, 159, 197, 163, 0, 0}
got := encXORPass(data, 2, len(data), 1572995469)
require.Equal(t, want, got)
}
But in Golang it’s giving a divergence in the test
The problem seems to be in the line: edx := int(raw[pos]) & 0xFF, the divergence occurs at position 147 of the array, where the expected value is 147 (0x93) and the value obtained is 131 (0x83).
But I can’t find the reason for this difference, could someone help me?
I need to add XOR to the bytes from position 6 to 176, and add the XOR key, but I don’t know why the difference.