views:

57

answers:

3

How to make an Encode function based on this Decode function? I got the source code for the Decode function on the internet but I need the Encode function.

All my attempts to make it failed and the original coder isn't available at the moment.

The (original) code:

byte Decode(byte EncodedByte)
{
    EncodedByte ^= (byte)194;
    EncodedByte = (byte)((EncodedByte << 4) | (EncodedByte >> 4));
    return EncodedByte;
}
A: 

Why do not you use normal encryption/decryption functions of c#?

http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net

Alex Reitbort
Why would I? This code is converted from C++ and this way works perfect. Also, I have no idea how I would convert this to 'normal encryption/decryption functions'.
lesderid
This isn't very helpful - he's likely dealing with data created through some other process that has to use this approach.
Rudu
Indeed, see Scott's answer's comments.
lesderid
+1  A: 

byte Encode(byte EncodedByte) 
{ 
    EncodedByte = (byte)((EncodedByte << 4) | (EncodedByte >> 4)); 
    EncodedByte ^= (byte)194; 
    return EncodedByte; 
} 
Henrik
I didn't knew it's this easy. Thank you very much!
lesderid
Poor argument choice name.
Rudu
+4  A: 

Just some quick back of the napkin coding the answer should be

byte Encode(byte DecodedByte)
{
    DecodedByte = (byte)((DecodedByte << 4) | (DecodedByte >> 4));
    DecodedByte ^= (byte)194;
    return DecodedByte;
}

Also I agree with Alex this is a trivial Encryption method. Anyone who knows the algorithm can trivially decrypt your message. I would not rely on it for any sensitive information and if this is code for public use some countries have laws that data must have some form of encryption. If I was a judge for the person suing you for a data breach I would call this more of a obfuscation technique then a encryption technique.

Scott Chamberlain
Thank you, Henrik was first though. +1.
lesderid
Came up with the same myself - ran a unit test on all possible bytes and it successfully works (as if you had a doubt ;))
Rudu
Henrik and I posted about 15 seconds apart. If I had not changed my EncodedBytes to Decoded bytes I would have been first ;p
Scott Chamberlain
About your edit, I agree. I'm dealing with a file format that I didn't make so I can't really do anything about it.
lesderid
Sorry for not accepting your answer. But you've got 3 upvotes. :P
lesderid