Username: 
Password: 
Restrict session to IP 

[Python] Byte per Byte XORing

Global Rank: 183
Totalscore: 110766
Posts: 88
Thanks: 78
UpVotes: 91
Registered: 9y 196d
occasus`s Avatar



Last Seen: 11d 13m
The User is Offline
[Python] Byte per Byte XORing
Google/translate0Thank You!0Good Post!0Bad Post! link
Hello all... trying is always a good start. I've seen there are many real Coders here... like the great space but many others too. I was wandering if someone would like to help me understanding some Python-related things. (Believe if I tell you that I'm reading documentation, references, examples and video-tutorials... apparently I'm getting old, because I don't understand always everything) The following code seems a lot, but it isn't. Actually it is the same code x4 because there could be 4 different .jpg headers atm. In few hours it will anyway not be solvable with the same code... so... there are no spoilers here, related to anywhere.

Details: there is a .jpg file. This has been xored. The challenge hosted on WeChall has more... hmm... let's say: through the walk into the solution many hints... so very much easier. In my case, about the key there is no hint.

Moreover I do not even understand why I'm getting decimals, when I actually would like to see the hex-bytes... ok... sorry I talk/write way too much... Here we go. Many thanks in advance (even just for reading).

GeSHi`ed Python code for XOR
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
20
jpg1short = 'FF D8 FF DB'.split(' ')                              # ÿØÿÛ         - jpg (jpeg) (raw or in the JFIF or Exif file)
jpg1long  = 'FF D8 FF E0 00 10 4A 46 49 46 00 01'.split(' ')      # ÿØÿà..JFIF.. - jpg (jpeg) (raw or in the JFIF or Exif file)
jpg2short = 'FF D8 FF EE'.split(' ')                              # ÿØÿî         - jpg (jpeg) (raw or in the JFIF or Exif file)
jpg2long  = 'FF D8 FF E1'.split(' ')   # ?? ?? 45 78 69 66 00 00  # ÿØÿá..Exif.. - jpg (jpeg) (raw or in the JFIF or Exif file) here we have 2 unknown bytes, so we leave it short: only the first 4bytes
encrypted = 'B9 14 06 45 71 E0 B5 F7 37 07 CB 85'.split(' ')      # first 12 bytes of encrypted file 
j1s_xored = [ int(j1s, 16) ^ int(enc, 16) for j1s, enc in zip(jpg1short, encrypted) ]
j1l_xored = [ int(j1l, 16) ^ int(enc, 16) for j1l, enc in zip(jpg1long, encrypted) ]
j2s_xored = [ int(j2s, 16) ^ int(enc, 16) for j2s, enc in zip(jpg2short, encrypted) ]
j2l_xored = [ int(j2l, 16) ^ int(enc, 16) for j2l, enc in zip(jpg2long, encrypted) ] 
print(jpg1short)   # ['FF', 'D8', 'FF', 'DB']
print(jpg1long)    # ['FF', 'D8', 'FF', 'E0', '00', '10', '4A', '46', '49', '46', '00', '01']
print(jpg2short)   # ['FF', 'D8', 'FF', 'EE']
print(jpg2long)    # ['FF', 'D8', 'FF', 'E1']print(encrypted)   # ['B9', '14', '06', '45', '71', 'E0', 'B5', 'F7', '37', '07', 'CB', '85']
print(j1s_xored, ''.join( [ chr(i) for i in j1s_xored] ) )   # [70, 204, 249, 158] >as ascii> FÌùž
print(j1l_xored, ''.join( [ chr(i) for i in j1l_xored] ) )   # [70, 204, 249, 165, 113, 240, 255, 177, 126, 65, 203, 132] >as ascii> FÌù¥qðÿ±~A˄
print(j2s_xored, ''.join( [ chr(i) for i in j2s_xored] ) )   # [70, 204, 249, 171] >as ascii> FÌù«
print(j2l_xored, ''.join( [ chr(i) for i in j2l_xored] ) )   # [70, 204, 249, 164] >as ascii> FÌù¤


Sincerely Yours dear Challengers
occasus
Global Rank: 1
Totalscore: 760035
Posts: 431
Thanks: 491
UpVotes: 456
Registered: 14y 246d












The User is Offline
RE: [Python] Byte per Byte XORing
Google/translate1Thank You!0Good Post!0Bad Post! link
What is the question?

As for why you are getting decimals, that's how most people prefer to see their numbers, so that's what most languages will default to when printing numbers. You converted a hexadecimal representation to a number, but after that, there is nothing hexadecimal (or decimal) about that number anymore. (Let alone numbers derived from that one.) So when printing the number, how should Python know you want to see hexadecimal? That is, unless you explicitly tell it.

Quote from occasus
In few hours it will anyway not be solvable with the same code... so... there are no spoilers here, related to anywhere.

Uhm... I'm not sure code works that way. Unless the challenge you are talking about magically changes its encryption method... but I don't remember such a challenge.
Global Rank: 183
Totalscore: 110766
Posts: 88
Thanks: 78
UpVotes: 91
Registered: 9y 196d
occasus`s Avatar



Last Seen: 11d 13m
The User is Offline
RE: [Python] Byte per Byte XORing
Google/translate0Thank You!0Good Post!0Bad Post! link
Thank you very much dear dloser for replying. Yep indeed, the encrypted file will always bee a .jpg but just as file type. As far as I understood (probably I'm wrong) the .jpg changes every different session... like:"aaa.jpg", "bbb.jpg", "ccc.jpg"... The encryption method will always be XOR. The funny thing is that with above 4 tries, the ASCII values are like:"FÌùž", "FÌù¥qðÿ±~A˄", "FÌù«" and "FÌù¤". Therefore I see them as basically pseudo-random bytes those chosen for the encryption. So no hints here... maybe I'm wrong.

By the way... if:
GeSHi`ed Python code
1
j1s_xored = [ int(j1s, 16) ^ int(enc, 16) for j1s, enc in zip(jpg1short, encrypted) ]

... should it not create a list of byte? With the "int(j1s, 16)" statement? I'm telling Python to threat j1s as a base 16 number (hex)... For sure I'm overlooking something... Btw those days (after 21days forced quarantine I'm drunk the last days xD )
Global Rank: 1
Totalscore: 760035
Posts: 431
Thanks: 491
UpVotes: 456
Registered: 14y 246d












The User is Offline
RE: [Python] Byte per Byte XORing
Google/translate1Thank You!1Good Post!0Bad Post! link
Quote from occasus
I'm drunk the last days

I think it shows... Perhaps find a different coping mechanism?

... should it not create a list of byte? With the "int(j1s, 16)" statement? I'm telling Python to threat j1s as a base 16 number (hex)

No. You tell Python to treat it as an integer that is represented as a string of hexadecimal digits. The result of int() is an integer. Python does not know you only feed it two-digit hexadecimals that to you mean bytes or chars.
tunelko, quangntenemy, TheHiveMind, Z, balicocat, Ge0, samuraiblanco, arraez, jcquinterov, hophuocthinh, alfamen2, burhanudinn123, Ben_Dover, stephanduran89, braddie0, SwolloW, dangarbri have subscribed to this thread and receive emails on new posts.
1 people are watching the thread at the moment.
This thread has been viewed 2818 times.