Reputation:
This is an encryption routine to encrypt a character:
Can anybody shine some light on the decryption routine?
Edit (see comments):
Upvotes: 0
Views: 5302
Reputation: 56822
Without the ROL
the encryption decryption could have been used for decryption, too.
Assuming the encrypted character is in ecx
and the key in eax
, it only needs a small modification:
decrypt5:
push eax
push ecx
This part generates an internal key in edx
from the input key in eax
:
and eax,0x3C
ror eax,1
ror eax,1
inc eax
mov edx,eax
Pop the input character from ecx
into eax
:
pop eax
Reverse the ROL AL,1
:
ror al,1
Undo the XOR-ing. XOR is an involution, that is it is its own inverse. It is also commutative, so the order doesn't matter:
xor eax,edx
Pop the input key from eax
into ecx
and XOR:
pop ecx
xor eax,ecx
ret
I hope is correct, my x86 assembly is rather rusty.
Note that it is a bit of a stretch to call this encryption. It is so weak I'd rather call it obfuscation.
Upvotes: 0
Reputation: 20211
Let's see: In order to decrypt the message we want to reverse the encryption procedure, so let's start at the end of the method:
the last part of the transformation is rol al, 1
. This we can reverse as ror al, 1
.
The rest of the encryption consists of two xors, one of the message and with a modified key and one with the immediate result and the original key. Since xor is self inverting we can reverse this by xoring the encrypted message once with the original key and once with the modified key (using the same key modification function).
All in all the follwing should reverse the encryption:
decrypt: ror cl,1 //reverse rol al, 1
xor ecx, eax //reverse xor message, original key
and eax,0x3C //calculate modified key
ror eax,1
ror eax,1
inc eax //end calculate modified key
xor eax, ecx //reverse xor message modified key
ret
Upvotes: 0
Reputation: 48706
This is the first block that we can decrypt :
xor eax,edx
xor eax,ecx
rol al,1
That is because the values are poped from stack. This is reversible like :
ror al,1
xor eax, ecx (eax = the one we had in the end)
xor eax, edx
Then edx = eax (mov edx,eax). And now :
dec eax
rol eax, 1
rol eax, 1
and the last AND cannot be reversed because :
? AND 0 = 0
? AND 1 = 1 => ? = 1
? AND 1 = 0 => ? = 0
? can not be identified UNLESS there is no ? AND 0 = 0 combination.
Upvotes: 1