2013-06-12 68 views
6

我正在寫一個類的程序首先用預定的密鑰加密字符串。那部分完成了。下一部分是我遇到問題或本身不存在問題的地方。它是一個冗餘問題。在此之後,我應該對字符串和加密字符串進行KPA查找密鑰。 這是工作,但我使用像15嵌套循環的蠻力。有沒有另一種方法來做到這一點?而不是遞歸地做!蠻力在Java自己的加密

static String Key = null; 

public static void main(String[] args) { 
    long startTime = System.nanoTime(); 
    long startTime1 = System.currentTimeMillis(); 
    int cntr = 0; 
    String key = "AAAAAAAAAAADDDAM"; 
    String plaintext = "Secretfoemotherd"; 
    StringBuilder cipher = new StringBuilder(); 
    StringBuilder brutus = new StringBuilder(); 


    byte[] ciphertext = encrypt(byteT(key), byteT(plaintext)); 
    for (int i = 0; i < ciphertext.length; i++) { 
     cipher.append(ciphertext[i]); 
    } 

    while (true) { 
     char[] nkey = new char[16]; 
     for (int i1 = 65; i1 < 122; i1++) { 
      nkey[0] = (char) i1; 
      for (int i2 = 65; i2 < 122; i2++) { 
       nkey[1] = (char) i2; 
       for (int i3 = 65; i3 < 122; i3++) { 
        nkey[2] = (char) i3; 
        for (int i4 = 65; i4 < 122; i4++) { 
         nkey[3] = (char) i4; 
         for (int i5 = 65; i5 < 122; i5++) { 
          nkey[4] = (char) i5; 
          for (int i6 = 65; i6 < 122; i6++) { 
           nkey[5] = (char) i6; 
           for (int i7 = 65; i7 < 122; i7++) { 
            nkey[6] = (char) i7; 
            for (int i8 = 65; i8 < 122; i8++) { 
             nkey[7] = (char) i8; 
             for (int i9 = 65; i9 < 122; i9++) { 
              nkey[8] = (char) i9; 
              for (int i10 = 65; i10 < 122; i10++) { 
               nkey[9] = (char) i10; 
               for (int i11 = 65; i11 < 122; i11++) { 
                nkey[10] = (char) i11; 
                for (int i12 = 65; i12 < 122; i12++) { 
                 nkey[11] = (char) i12; 
                 for (int i13 = 65; i13 < 122; i13++) { 
                  nkey[12] = (char) i13; 
                  for (int i14 = 65; i14 < 122; i14++) { 
                   nkey[13] = (char) i14; 
                   for (int i15 = 65; i15 < 122; i15++) { 
                    nkey[14] = (char) i15; 
                    for (int i16 = 65; i16 < 122; i16++) { 
                     nkey[15] = (char) i16; 

                     cntr++; 

                     byte[] brutusCipher = Crack(
                       byteC(nkey), 
                       byteT(plaintext)); 

                     for (int k = 0; k < brutusCipher.length; k++) { 
                      brutus.append(brutusCipher[k]); 

                     } 

                     if (brutus 
                       .toString() 
                       .equals(cipher 
                         .toString())) { 
                      System.out 
                        .println("found it"); 
                      System.out 
                        .println("Key: " 
                          + Key); 
                      System.out 
                        .println("Brutus: " 
                          + brutus); 
                      System.out 
                        .println("i ran: " 
                          + cntr 
                          + "times"); 

                      long endTime = System 
                        .nanoTime(); 
                      System.out 
                        .println("time:" 
                          + (endTime - startTime) 
                          + " ns"); 
                      long endTime1 = System 
                        .currentTimeMillis(); 
                      System.out 
                        .println("Took " 
                          + (endTime1 - startTime1) 
                          + " ms"); 
                      return; 
                     } 
                     brutus.setLength(0); 

                    } 
                   } 
                  } 
                 } 
                } 
               } 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

public static byte[] byteT(String s) { 
    return s.getBytes(); 
} 

public static byte[] byteC(char[] s) { 
    StringBuilder temp = new StringBuilder(); 
    for (int i = 0; i < s.length; i++) { 
     temp.append(s[i]); 
    } 
    Key = temp.toString(); 
    return temp.toString().getBytes(); 
} 

public static byte[] encrypt(byte[] key, byte[] plaintext) { 
    byte[] d = new byte[key.length]; 
    System.out.println(key.length); 
    for (int i = 0; i < key.length; i++) { 
     d[i] = (byte) (key[i]^plaintext[i]); 
    } 

    return d; 
} 

public static byte[] Crack(byte[] key, byte[] plaintext) { 
    byte[] n = new byte[key.length]; 
    for (int i = 0; i < key.length; i++) { 
     n[i] = (byte) (key[i]^plaintext[i]); 
    } 
    return n; 
} 

} 
+4

這是迄今爲止最嵌套循環結構,我已經看到了:) –

+1

Yeeeeeah這就是爲什麼我在這裏:) – Becktor

回答

4

這是我對你如何改進你的代碼的建議:

char[] nkey = new char[16]; 
for (int i =0 ;i<16;++i) { 
    nkey[i] = 65; 
} 

while (true) { 
    //... do the stuff you do in the inner of the cycle 
    int index = 15; 
    nkey[index]++; 
    while (index >= 0 && nkey[index] >= 122) { 
    nkey[index] = 65; 
    index--; 
    if (index < 0) { 
     break; 
    } 
    nkey[index]++; 
    } 
} 

你可以想像我一樣代表你在爲基地122-65一些迭代什麼,並添加一個到它。

+1

真棒作品就像一個魅力!感謝buch! – Becktor

1

您可以創建這樣一個類(未測試):

class IncrementableCharArray { 

    private final char[] array; 

    IncrementableCharArray(int size) { 
     array = new char[size]; 
     Arrays.fill(array, 'A'); 
    } 

    boolean increment() { 
     //here logic to increment the array 
     int index = 0; 
     while(index < array.length && array[index] == 'z') index++; 
     if (index == array.length) return false; 
     array[index]++; 
     return true; 
    } 

    char[] get() { return array; } 

} 

的表現也不會好過,但是這將是更具可讀性一點點。你可以使用它像這樣:

IncrementableCharArray array = new IncrementableCharArray(16); 
while(array.increment()) { 
    char[] nkey = array.get(); 
    //your test here 
} 
+0

注意:遞增方法不起作用,但您明白了。 – assylias

+1

是的,謝謝你的回覆! – Becktor