views:

132

answers:

4

How to generate a code from two numbers, that when you know this code and one of the numbers you can descramble the second number? For example you have two numbers: 983 and 2303 and you "mix" it into hex-string like this:4b17a190bce4ea32236b98dd. When you know first number and this hex-string you can descramble second number. But when you know the second number and hex-string you cannot descramble first number. How to do this?

A: 

You need something like XOR: you xor the two numbers and store the result and one of them. Then you xor the result with the number you know and get the other number back. Not all operations will do for this purpose of course.

sharptooth
+1  A: 

The easiest and really NONSAFE (as in dead easy to crack by anyone paying a bit of attention) method of doing it:

 int number1;
 int number2;
 int key;

 //encode  (we know number1 and number2 and we want to know the key)
 number1 = 983;
 number2 = 2303;
 key = number1 ^ number2;

 //decode  (we know number1 and the key and we want to know number2)
 number1 = 983;
 number2 = key ^ number1;

Yet again (to avoid downvotes). This method is cracked in seconds by anyone who sits down for it. It does work for clueless users (clueless as in, no math or programming background).

Toad
Very good, this is what i was going to post.
Rook
A: 

In python:

hash = 0x4b17a190bce4ea32236b98dd # hash
input = 983
output = 2303

#k = hash - (input * output)
k = 23239944001398166727313395124

def decode(hash, input):
  return (hash - k) / input

def encode(input, output):
  return '%x' % (k + (input * output))

print k, decode(hash, input), encode(input, output)
Ewan Todd
This answers the OP's question quite literally. Any punishment for weak encryption should be on the question, not the answer.
Ewan Todd
A: 
Why the negative marks?? Thought that was a good answer.