Win a copy of Head First Android this week in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Devaka Cooray
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• Jj Roberts
• Al Hobbs
• Piet Souris

# Encrypt / Decrypt 10 digit Number

Ranch Hand
Posts: 79
• Number of slices to send:
Optional 'thank-you' note:

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

Marshal
Posts: 74354
334
• Number of slices to send:
Optional 'thank-you' note:
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".

author
Posts: 23909
142
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 96
• Number of slices to send:
Optional 'thank-you' note:
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.

Bartender
Posts: 1638
• Number of slices to send:
Optional 'thank-you' note:
Since this is related to encrypt/decrypt, moving it to security forum.

Trupti Mehta
Ranch Hand
Posts: 79
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 23909
142
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 23909
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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?)

Bartender
Posts: 2899
150
• Number of slices to send:
Optional 'thank-you' note:
Isnt this Hashing ???

Trupti Mehta
Ranch Hand
Posts: 79
• Number of slices to send:
Optional 'thank-you' note:
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: 2899
150
• Number of slices to send:
Optional 'thank-you' note:
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: 2899
150
• Number of slices to send:
Optional 'thank-you' note:

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.

Ranch Hand
Posts: 220
• Number of slices to send:
Optional 'thank-you' note:
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.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton