Convert HEX to XOR

I am trying to convert a hex string to XOR, but not getting a good result

This is the python code to convert utf-8 to XOR

def encodePayload(string, b64=True, shift=True, shiftAmount=50):

    if (shift):

        encoded = ''

        for i in range(len(string)):

            encoded += chr(ord(string[i]) ^ shiftAmount)

        if (b64):

            return encoded

    else:

        if (b64):

            return base64.b64encode(string.encode('utf-8'))

        else:

            return string        

a = encodePayload(PAYLOAD)

Does anyone know how to do this for hex to XOR?

Do you need this in C# or python? As in, are you planning to use this inside OB2 or on a separate project?

1 Like

Ofcz I am doing this in ob2 dude, if possible I want it in c# as ob2 supports it very good. In case it’s not possible in c# I am okay with using python

Please give me a sample input string and a sample output string so that I can verify that the code works, then I will write you the function

Here, if the string is: {"name":"Jordan"}

BLOCK:ConstantString
LABEL:PAYLOAD
  value = "{\"name\":\"Jordan\"}"
  => VAR @PAYLOAD
ENDBLOCK

BLOCK:Script
INTERPRETER:IronPython
INPUT PAYLOAD
BEGIN SCRIPT
def encodePayload(string, b64=True, shift=True, shiftAmount=50):
	if (shift):
	    encoded = ''
	    for i in range(len(string)):
	        encoded += chr(ord(string[i]) ^ shiftAmount)
	    if (b64):
			return encoded
	else:
	    if (b64):
	        return base64.b64encode(string.encode('utf-8'))
	    else:
	        return string        
a = encodePayload(PAYLOAD)
END SCRIPT
OUTPUT String @a
ENDBLOCK

BLOCK:UTF8ToBase64
  input = @a
  => VAR @payload
ENDBLOCK

This is the loli code if the text is a utf-8 string
This should be the output = SRBcU19XEAgQeF1AVlNcEE8=

But what i need is that that the input text is in hex like this 7b226e616d65223a224a6f7264616e227d (In my original case the conversion from hex to utf-8 won’t work as its not in utf-8, so i need it to convert directly to xor encoding from hex)

you can use this tool for testing

image

The site is down for me, it’s a 404

EDIT: Nevermind I found it via google

1 Like

I hope this piece of code clears all your doubts

BLOCK:ConstantString
  value = "7b226e616d65223a224a6f7264616e227d"
  => VAR @hex
ENDBLOCK

BLOCK:HexStringToByteArray
  hexString = @hex
  => VAR @bytes
ENDBLOCK

BLOCK:BytesToString
  input = @bytes
  encoding = UTF8
  => VAR @str
ENDBLOCK

// This function will XOR each value of the byte array with the value 50
byte[] EncodePayload(byte[] bytes, int shiftAmount)
{
  var encoded = new byte[bytes.Length];

  for (var i = 0; i < bytes.Length; i++)
  {
    encoded[i] = (byte)(bytes[i] ^ shiftAmount);
  }

  return encoded;
}

// Here we call the function and get the output as a byte array
byte[] encoded = EncodePayload(bytes, 50);

BLOCK:ByteArrayToBase64String
  bytes = @encoded
  urlEncoded = False
  => VAR @outputBase64
ENDBLOCK

The 3rd block is just to show that you can still convert it to a UTF8 string ^^

1 Like

I see thanks as usual dude you really help people to the best :grin:. Ima try it out

Bonus: if you want here’s the LINQ version of the same function, much shorter and maybe easier to understand :sweat_smile:

byte[] EncodePayload(byte[] bytes, int shiftAmount)
  => bytes.Select(b => (byte)(b ^ shiftAmount)).ToArray();

Yeah thanks for that too! :kissing_heart::yum: