6

I'm trying to generate a license key with two strings combined both into another one string.

String key1 = "X1X2X3X4..Xn"; //this is for the imei key cellphone
String key2 = "Y1Y2Y3Y4M1M2D1D2H1H2m1m2"; //this is a key to mix with the first one

The result of the combination should be like this:

String result = "D1H1X1X2X3Y2Y4X4X5...XnY3Y1D2m2m1H1H2";

I split my strings every two spaces like this and I save into an array:

String [] key1splited = splitStringEvery(key1, 2);
String [] key2splited = splitStringEvery(key2, 2);

public String[] splitStringEvery(String s, int interval) {
    int arrayLength = (int) Math.ceil(((s.length() / (double)interval);
    String[] result = new String[arrayLength];

    int j = 0;
    int lastIndex = result.length - 1;
    for (int i = 0; i < lastIndex; i++) {
        result[i] = s.substring(j, j + interval);
        j += interval;
    } 
    result[lastIndex] = s.substring(j);

    return result;
}

How can I make the combination of my strings give me a result that looks like this:

String result = "D1H1X1X2X3Y2Y4X4X5...XnY3Y1D2m2m1H1H2";

I hope someone could give me an idea how to solve this.

I am trying to do something like this, but it is very poor method:

static String res = "";
String[] key1splited = splitStringEvery(key1, 2);
String[] key2splited = splitStringEvery(key2, 2);
for (int i = 0; i < key2splited.length; i++) {
    if (key2splited[i].equals("D1")) {
        res = key2splited[i];
    }
    if (key2splited[i].equals("H1")) {
        res += key2splited[i];
        for (int j = 0; j < key1splited.length; j++) {
            if (key1splited[j].equals("X1")) {
                res += key1splited[j];
            }
            if (key1splited[j].equals("X2")) {
                res += key1splited[j];
            }
            if (key1splited[j].equals("X3")) {
                res += key1splited[j];
            }
        }
    }
}

And so on, but this isn't a good way to do it because the strings are going to change.

henser
  • 3,307
  • 2
  • 36
  • 47
  • 1
    Can you be bit clear how you mix two string? – SMA Dec 15 '14 at 16:05
  • 1
    Why would you like to _mix_ those two? Is it the case that you want to get a deterministic license key based on environmental parameters, but not immediately recognisable? If yes, do you want the key to be reversible (i.e. key1 and key2 computable from the result (maybe in possession of a third secret))? There are better methods for any of the above than mixing the two strings up... – P.Péter Dec 15 '14 at 16:17
  • Well am trying to do this – Salvador Figueroa Dec 15 '14 at 16:23
  • I put some code in my question to show what i am doing but i dont like how i am trying to solve my problem i need a better way to do it. if you could help me with and idea thanks :) – Salvador Figueroa Dec 15 '14 at 16:39

4 Answers4

1

I'm hurry so if i get it, you want something like this? This may give you some ideas using ArrayList. If someone find something wrong fell free to edit.

    String tmp = "X1X2X3";
    String tmp2 = "Y1Y2Y3";

    ArrayList<String> list = new ArrayList<String>();

    for(int i = 0 ; i < (tmp.length()/2) ; i  = i + 2)
    {
        list.add(tmp.substring(i,i+1));
        list.add(tmp2.substring(i,i+2));
    }

    int max = list.size();
    String finalMix = null;

    for(int j = 0 ; j < max; j++)
    {
        Random rnd = new Random();
        int rndNumber = rnd.nextInt((list.size()) + 1);
        finalMix += list.get(rndNumber);
        list.remove(rndNumber);
    }
    System.out.println(finalMix);
}
Lucas Bertollo
  • 373
  • 2
  • 5
  • 19
  • Thanks, was almost near what i need i tried with arrayList too, the solution was provide by nem but i really apreciate your help and the others i am learning a lot from here. – Salvador Figueroa Dec 15 '14 at 18:39
1

I think the easiest way you could do this is to separate the tokens out of each key (2-char long tokens like X1, D1 etc.) and then combine the tokens into a single String.

Further, you shuffle up the String, extracting random tokens and building the Licence Key:

Random rand = new Random(); // generate Random object only once for efficiency purposes

// ... rest of your code

String getLicenceKey(String key1, String key2){
    List<String> tokens = new ArrayList<String>();

    // add tokens from key1
    for(int i = 0; i < key1.length(); i += 2) {
        tokens.add(key1.substring(i, i + 2));
    }

    // add tokens from key2
    for(int i = 0; i < key2.length(); i += 2) {
        tokens.add(key2.substring(i, i + 2));
    }

    // build the random result out of the tokens
    StringBuilder result = new StringBuilder();
    while(tokens.size() != 0){
        int randomPos = rand.nextInt(tokens.size());
        result.append(tokens.remove(randomPos));
    }

    return result.toString();
}

Sample outputs for the inputs you gave:

m2XnD2m1H2X1..Y3Y1Y2Y4M1X2M2D1H1X4X3
H2Y2X4M1M2H1Y3Y1m2X1X2D1m1Xn..X3Y4D2
X1X4X3H2D2H1..M2m2Y3m1Y4M1D1Y1X2XnY2

Sample outputs for key1="A1B1C1", key2="D1E1F1":

D1F1B1A1C1E1
C1A1D1B1E1F1
F1E1B1C1D1A1
nem035
  • 34,790
  • 6
  • 87
  • 99
  • Thanks i was doing this ArrayList arrayList = new ArrayList();for (int i = 0; i < key2splited.length; i++) { arrayList.add(key2splited[i] + " " + key1splited[i]); }Collections.sort(arrayList); but your way it is better – Salvador Figueroa Dec 15 '14 at 18:36
0

Take a look at this SO question. How do I generate random integers within a specific range in Java?

Generate a random number like cited, and rebuild a string with your random index from your array of string "parts".

Community
  • 1
  • 1
MeetTitan
  • 3,383
  • 1
  • 13
  • 26
  • I am watching now, let me try to understand. Thanks for your time – Salvador Figueroa Dec 15 '14 at 16:41
  • @Salvador, no problem. It's an easy concept to follow; you've indexed your string "parts" (split substrings and put them into an array), now read back what you have randomly (get random integer, then get the substring at your random index and append it to a `StringBuilder` for instance.) – MeetTitan Dec 15 '14 at 17:45
  • Thanks for your help and time :) i solved the problem with the nem idea. – Salvador Figueroa Dec 15 '14 at 18:43
  • @Salvador; humorously, that was what I was suggesting with my answer. That will teach me to not include code in my answer. – MeetTitan Dec 15 '14 at 20:17
0

I would suggest forgetting the mixing up of strings, and using simple cryptographic hash, like:

org.apache.commons.codec.digest.DigestUtils.sha384Hex(key1+key2);
  • This yields always the same result for the same key1,key2 pair, but cannot be used to calculate any of them.
  • You can truncate the resulting 24 bytes, but of course you somewhat increase the risk of collisions (you're still pretty good with 12 bytes).
  • You need a different strategy if you need to reverse the process (i.e. get key1,key2 from the result), or simply store those two server-side with the result if you need to (this may create privacy issues).
P.Péter
  • 1,527
  • 16
  • 39