1

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;
}
lesderid
  • 3,388
  • 8
  • 39
  • 65

3 Answers3

4

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
  • 124,994
  • 33
  • 282
  • 431
1

byte Encode(byte EncodedByte) 
{ 
    EncodedByte = (byte)((EncodedByte << 4) | (EncodedByte >> 4)); 
    EncodedByte ^= (byte)194; 
    return EncodedByte; 
} 
Henrik
  • 23,186
  • 6
  • 42
  • 92
0

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

Encrypt and decrypt a string

Community
  • 1
  • 1
Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • 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 Sep 15 '10 at 13:44
  • 1
    This isn't very helpful - he's likely dealing with data created through some other process that has to use this approach. – Rudu Sep 15 '10 at 13:49
  • Indeed, see Scott's answer's comments. – lesderid Sep 15 '10 at 13:51