English 中文(简体)
如何将两种体形与随机位置和roid结合
原标题:How combine two strings with random position android
  • 时间:2014-12-15 16:00:07
  •  标签:
  • java
  • android

I m试图生产一个许可证钥匙,有两条护法将两者合并成另一条str。

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

组合的结果应如此:

String result = "D1H1X1X2X3Y2Y4X4X5...XnY3Y1D2m2m1H1H2";

我将座右铭分成两处,我只把座椅分成一阵:

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;
}

我怎么能够把我的发言结合起来,给我带来这样的结果:

String result = "D1H1X1X2X3Y2Y4X4X5...XnY3Y1D2m2m1H1H2";

我希望有人能给我一个如何解决这一问题的想法。

我正试图做这样的事情,但这种方法非常糟糕:

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];
            }
        }
    }
}

因此,但这不能成为这样做的良好途径,因为扼杀物会发生变化。

最佳回答

我认为,你最容易做到的办法是将脚印从每个钥匙中分离出来(如X1、D1等2个长期标注),然后把标语合并成一个<条码>。

此外,你填写了<代码>St,抽取随机标本,并建造了许可证钥匙:

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();
}

你提供的投入的样本:

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

关键1=“A1B1C1>、关键2=<>D1E1F1:

D1F1B1A1C1E1
C1A1D1B1E1F1
F1E1B1C1D1A1
问题回答

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);
}

我建议采用简单的加密法,如:

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).

我最近的面谈逻辑之一

public class Main {
  public static void main(String[] args) {
  
  String a = "abcd";

  String b = "123456";

  StringBuffer sb = new StringBuffer(b);
  
  for(int i=0;i<4;i++){

  sb.insert(i*2,a.charAt(i));

 }
    
    System.out.println(sb);
  }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签