Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Encrypt / Decrypt 10 digit Number

Trupti Mehta
Ranch Hand
Posts: 79

Hello Friends,

I have to encrypt and decrypt a 10 digit number to get a unique value. I want the results less than 6 digit/char long. I tried something like following.

The algorithm is
Pick a 8 or 9 digit number at random, say 839712541.
Then, take your order number's binary representation (for this example,
I'm not using 2's complement), pad it out to the same number of bits (30), reverse it,
and xor the flipped order number and the magic number.

To get the order numbers back, xor the output number with your magic number,
convert to a bit string, and reverse.

The above code was just used as an example and tried to implement it to see it running successfully. Can anyone help me correct the above results and get them right and
Find or have a better soluiton to my requirements. I am badly in need for the same. I looked around over internet but couldn't find any thing helpful.

Your help and guidens can be of great help.

Thanks

Campbell Ritchie
Sheriff
Posts: 48917
58
Don't know, but you need to consider what sort of output your require. There are 10^10 possible values in a 10-digit number, and you wish to reduce it to <= 6 characters. So you require (10^10)^(1/6) different possible characters = 46.4 at least. You can therefore represent 10^10 in terms of 6 English characters if you include upper-case and lower-case, but it is probably easier to divide your binary string into 6 lots of 6 bits, requiring 64 different characters. Try something matching this regex: "[A-Za-z0-9_.]+" maybe?
If you really require <6 then you need 7 bits a time, requiring a 128-character "alphabet".

Henry Wong
author
Marshal
Posts: 21114
78
You are not being consistent. Going one direction, you used the reverse() method. Going the other direction, you used the reverseBytes() method. Since these two methods do two different things, it would make sense that you don't get the original number back.

Henry

John Kimball
Ranch Hand
Posts: 96
So the input is definitely a number. But can the magic "number" be any character? If so, and if you don't care about a SECURE algorithm, then look into the uuencode/decode algorithm. It's not "the answer" but it should give you some ideas.

If the magic # has to be all digits (that is, 0 to 9) AND must be guaranteed unique, then you're asking for the impossible.

Nitesh Kant
Bartender
Posts: 1638
Since this is related to encrypt/decrypt, moving it to security forum.

Trupti Mehta
Ranch Hand
Posts: 79
Henry Wong wrote:You are not being consistent. Going one direction, you used the reverse() method. Going the other direction, you used the reverseBytes() method. Since these two methods do two different things, it would make sense that you don't get the original number back.

Henry

Oh Thanks Henry, That mistake gave me the correct ans back. Now, can you help out me to get the results as only 6 numeric/alphanumeric unique code. Like Xor with any number gives unique results, but at presnet the value returned is quiet long, plus my number will be a 10 digit number not just 1 or 2. I want to reduce it to 3-4 numeric/alphanumeric unique code.

Henry Wong
author
Marshal
Posts: 21114
78
Nitesh Kant wrote:Since this is related to encrypt/decrypt, moving it to security forum.

Even though "encrypt" and "decrypt" is in the subject title, this topic has nothing to do with security. It is just munging of some bits.

Henry

Henry Wong
author
Marshal
Posts: 21114
78
Now, can you help out me to get the results as only 6 numeric/alphanumeric unique code.

Nope. Not usless you change the algorithm. Your algorithm is very specific. It is not possible to take any number, do an XOR, and a bit reverse, and guarantee the result to less than 6 digits. (well, actually you can, if you don't care to decrypt it ... )

Here is another way to think about it... You have a 10 digit number. There are 10,000,000,000 possible 10 digit numbers. You need to generate unique numbers. So, your algorithm needs to generate 10,000,000,000 unique (ie. different) numbers. There are only 1,000,000 possible six digit numbers. Can you assign 10,000,000,000 unique numbers from a pool of 1,000,000 numbers? With any algorithm?

Henry

John Kimball
Ranch Hand
Posts: 96
It's certainly possible to uniquely-encode an integer into an ALPHAnumeric representation that takes up less characters.

Log_10(N) vs. Log_36(N)

Edit: Though granted, his algorithm is tailored to numbers only (even if his requirement states otherwise?)

salvin francis
Bartender
Posts: 1274
10
Isnt this Hashing ???

Trupti Mehta
Ranch Hand
Posts: 79
From one of the search reuslts on encode/decode numbers, I found the algorithm that I have used. The author says, by doing Xor with any magic number, you are sure to get a unique number for any number and also tells how to decode that unique number back to original number using the magic number.

Henry, I need the code (number) to be unique, it may be alphanumeric. And also that should be decryptable to provide the same original number. Since decrypt is associated, that's where I am finding difficult to generate unique code of 6 chars of length.

John the unique code can be anything but should be max 6 chars. The magic number can also be anything, SECURE algorithm is also not required it should just solve the purpose. If the magic # is alpha or alpha numeric, then how do you plan to work around. Can you throw some light on this and Log_10 and Log_36 with maybe a eg statement.

Security issue coming up, is their any other way to do the same with any other resources.

Regards,

salvin francis
Bartender
Posts: 1274
10
how can you reduce no of digits and yet get a "unique" no???
you are bound to get clashes.

btw by XORing you are tryin the "One time pad" algo

salvin francis
Bartender
Posts: 1274
10

Secret Hash:

Choose a Number

XOR it with the 10 digit no

Calculate MD5 of the result
now crop result to 6 digit

you cannot get more secure than that.

greg stark
Ranch Hand
Posts: 220
As Campbell and others have noted, you cannot achieve your goal of using 5 characters or less. There are only 94 printable ASCII characters (95 if you include SPACE), and 94^5 is less than 10^10. So it is impossible.

On the other hand, if you allow for 6 characters, then it is possible, and if you use Campbell's 64 character alphabet it is easy.