支持BigIntegers for Great Number的Wordlist生成器

时间:2015-04-25 18:55:36

标签: java list biginteger word-list

我需要一个帮助来完成一个程序,该程序将根据所选字符和长度生成一个单词列表(它需要支持很长的时间)。

首先,你需要通过添加所需的长度(字长)和制作指定字符的字符串(字母)来解决这个问题。

所以单词的全部数字是:

long MAX_WORDS = (long) Math.pow(alphabet.length(), wordlength);

实际上,我做了它并且它起作用(对于2或66个字符的短字的例子)。

import java.math.BigInteger;
public class wordlistgenenreg {

public static void main(String[] args) { 
generate(); 
}

private static void generate(){
int wordlength =2;
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~";
final long MAX_WORDS = (long) Math.pow(alphabet.length(), wordlength);
final int RADIX = alphabet.length();

for (long i = 0; i < MAX_WORDS; i++) {
    int[] indices = convertToRadix(RADIX, i, wordlength);
    char[] word = new char[wordlength];
    for (int k = 0; k < wordlength; k++) {word[k] = alphabet.charAt(indices[k]);}
    String fullword=new String(word);
    System.out.println(fullword);
}

System.out.println("completed!");
}

private static int[] convertToRadix(int radix, long number, int wordlength) {
int[] indices = new int[wordlength];
for (int i = wordlength - 1; i >= 0; i--) {
    if (number > 0) {
        int rest = (int) (number % radix);
        number /= radix;
        indices[i] = rest;
    } else {
        indices[i] = 0;
    }

}
return indices;
}
}

但是当我想从66生成一个非常大的64个字符的字符串时有一个问题。因为:

  

MAX_WORDS = 66 ^ 64 = 282365657377235405270307754780751252031361330095689004197961218014051357270480550051149871489969454245263206971867136

所以我尝试更改它以使其与BigInteger一起使用。但是我们得到一个结果,我总是得到字符串:

  

&#34; 0000000000000000000000000000000000000000000000000000000000000000&#34;

所以有一个问题,我没有弄清楚。这是我改变它的工作:

import java.math.BigInteger;

public class wordlistgen {

public static void main(String[] args) {
    generate();
}

private static void generate() {
int wordlength = 64;
String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~";
BigInteger max_words=new BigInteger("282365657377235405270307754780751252031361330095689004197961218014051357270480550051149871489969454245263206971867136");
final int RADIX = alphabet.length(); 
BigInteger plus=BigInteger.valueOf(1);

for (BigInteger i = new BigInteger("0"); i.compareTo(max_words) <0; i.add(plus)) {
    int[] indices = convertToRadix(RADIX, i, wordlength);
    char[] word = new char[wordlength];
    for (int k = 0; k < wordlength; k++) {word[k] = alphabet.charAt(indices[k]);}
    String fullword=new String(word);
    System.out.println(fullword);        
}
}

private static int[] convertToRadix(int radix, BigInteger i2, int wordlength) {
BigInteger zero=BigInteger.valueOf(0);
BigInteger big_radix=BigInteger.valueOf(radix);
int[] indices = new int[wordlength];
for (int i = wordlength - 1; i >= 0; i--) {
    if (i2.compareTo(zero)==0) {

        BigInteger rest =i2.remainder(big_radix);
        BigInteger ab=i2.divide(big_radix);
        i2=ab;
        indices[i] = rest.intValue();
    } else {
        indices[i] = 0;
    }
}
return indices;
}
}

1 个答案:

答案 0 :(得分:1)

这是原始版本中的if

if (number > 0) {
    int rest = (int) (number % radix);
    number /= radix;
    indices[i] = rest;
} else {
    indices[i] = 0;
}

if版本中的BigInteger

if (i2.compareTo(zero)==0) {

    BigInteger rest =i2.remainder(big_radix);
    BigInteger ab=i2.divide(big_radix);
    i2=ab;
    indices[i] = rest.intValue();
} else {
    indices[i] = 0;
}

正如您所看到的,在您的新if中,您要问的是number == 0而不是number > 0。所以你总是最终进入else

作为旁注:您正在运行从0到max_words的循环。如果每次迭代只需要一纳秒来完成,那么它仍然需要368788667672120349090672500612638816231217766896306723928560063188563281831044121479026746095987887263264265岁。宇宙有足够的时间分裂成完整的熵。我建议重新考虑你的算法。