Question

I am developing a application where user is only authenticated if he enters a serial key correctly. This serial key is matched with the serial key already present in database. This serial key should consists of 6 digit and 3 alphabets. Users get these key in written form from me.

How do I generate a large number (100,000,000 approx) of serial keys?

Was it helpful?

Solution

Simplest way in my opinion is:

  1. find a desired number in range [1,10000000), let it be x and it will represent your 6 digits.
  2. Find 3 characters (let's assume in range a-z). Let them be a,b,c.
  3. Your number is (a-'a' + b-'a'*26 + c-'a'*26*26)*10000000 + x

(Where 'a' is the ascii representation of the character 'a', and a-'a' means the numeric ascii subtraction)

Note that the generated number can even fit in a long - assuming it is 64 bits.


Generation of several numbers can be simply done with iteration, start with a=b=c='a', and x=0, and start advancing (increase by 1). Once you reached x=10000000, increase a, if a passed z - increase b, .... (similar to what you would do in normal integer addition arithmetic).

OTHER TIPS

You can use a secure random number generator like SecureRandom to generate the keys.

But as already mentioned in a comment, if you generate that many keys, the space of all possible keys is not much larger. So it will be easy for an attacker to try out keys until one is found that works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top