2013-02-23 32 views
1

我認爲這可能是一個非常簡單的解決方案,但我不太確定。具有Java傳遞字符串錯誤的蠻力算法

我想創建一個字符數組,通過遞增數組的一個特定索引並在末尾將它們扔到一個字符串中來對它們進行排序。 (通過將結果打印到控制檯進行驗證,「aaaaaaaa」爲「aaaaaaab」等等。但是,我的新版本代碼通過ASCII值33 - 126進行排序。'!'到「〜」

現在,有一兩件事,我試圖做的是調用的主要方法是繩子,讓我們被分配到的加密/解密程序,可以一遍又一遍使用此字符串。

我們最初分配的程序可以在這裏找到: http://www.avajava.com/tutorials/lessons/how-do-i-encrypt-and-decrypt-files-using-des.html

import java.lang.Class.*; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import javax.crypto.Cipher; 
    import javax.crypto.CipherInputStream; 
    import javax.crypto.CipherOutputStream; 
    import javax.crypto.SecretKey; 
    import javax.crypto.SecretKeyFactory; 
    import javax.crypto.spec.DESKeySpec; 

public class Example1 { 

public String keyGen() { 
//create an array used for storing each character  
char array[] = new char[8]; 

    //for loop checks for each character between '!' and '~' 
    for (char c0 = '!'; c0 <= '~'; c0++) { 
    array[0] = c0; 

    for (char c1 = '!'; c1 <= '~'; c1++) { 
    array[1] = c1; 

    for (char c2 = '!'; c2 <= '~'; c2++) { 
    array[2] = c2; 

    for (char c3 = '!'; c3 <= '~'; c3++) { 
    array[3] = c3; 

    for (char c4 = '!'; c4 <= '~'; c4++) { 
    array[4] = c4; 

    for (char c5 = '!'; c5 <= '~'; c5++) { 
    array[5] = c5; 

    for (char c6 = '!'; c6 <= '~'; c6++) { 
    array[6] = c6; 

    for (char c7 = '!'; c7 <= '~'; c7++) { 
    array[7] = c7; 

    //create new string that stores the array 
    String pKey = new String(array); 

    //trying to return the new string 
    return pKey; 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 

public static void main(String []args) { 
try { 

// I am getting an error here; I know it has something to do with static references 

    String key = new String(keyGen(pKey); 

        // needs to be at least 8 characters for DES 

     FileInputStream fis = new FileInputStream("original.txt"); 
     FileOutputStream fos = new FileOutputStream("encrypted.txt"); 
     encrypt(key, fis, fos); 

     FileInputStream fis2 = new FileInputStream("encrypted.txt"); 
     FileOutputStream fos2 = new FileOutputStream("decrypted.txt"); 
     decrypt(key, fis2, fos2); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
} 

public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable { 
    encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os); 
} 

public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable { 
    encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os); 
} 

public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable { 

    DESKeySpec dks = new DESKeySpec(key.getBytes()); 
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); 
    SecretKey desKey = skf.generateSecret(dks); 
    Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE 

    if (mode == Cipher.ENCRYPT_MODE) { 
     cipher.init(Cipher.ENCRYPT_MODE, desKey); 
     CipherInputStream cis = new CipherInputStream(is, cipher); 
     doCopy(cis, os); 
    } else if (mode == Cipher.DECRYPT_MODE) { 
     cipher.init(Cipher.DECRYPT_MODE, desKey); 
     CipherOutputStream cos = new CipherOutputStream(os, cipher); 
     doCopy(is, cos); 
    } 
} 

public static void doCopy(InputStream is, OutputStream os) throws IOException { 
    byte[] bytes = new byte[64]; 
    int numBytes; 
    while ((numBytes = is.read(bytes)) != -1) { 
     os.write(bytes, 0, numBytes); 
    } 
    os.flush(); 
    os.close(); 
    is.close(); 
} 

}

回答

2

希望這有助於你解決問題。至於現在,你keyGen()等同於下面的代碼:

public String keyGen() { 
    return "!!!!!!!!"; 
} 

,因爲你只要你進入最裏面的for環路返回值。也許你想改變方法來返回List<String>,並在最內層的for循環中將你的字符串添加到該列表中,並在所有for循環之後返回該列表?這樣,您可以遍歷main方法中的列表。


如果我計算正確,則有93^8 = 5.595.818.096.650.401不同的字符串。將所有這些存儲在列表中是一個不好的建議。杜克林在評論中指出,爲此,最好使用自定義Iterator<String>


編輯

這裏有這樣一個迭代器的實現:

import java.util.Arrays; 
import java.util.Iterator; 

public class BruteForceIterator implements Iterator<String> { 

    private char min, max; 

    private char[] current; 

    private char[] last; 

    private int reachedLast = 0; 

    public BruteForceIterator(char min, char max, int length) { 
     this.min = min; 
     this.max = max; 
     current = new char[length]; 
     Arrays.fill(current, min); 
     last = new char[length]; 
     Arrays.fill(last, max); 
    } 

    @Override 
    public boolean hasNext() { 
     return reachedLast < 2; 
    } 

    @Override 
    public String next() { 
     String str = new String(current); 
     for(int i = current.length - 1; i >= 0; i--) { 
      char next = following(current[i]); 
      current[i] = next; 
      if (next != min) { 
       break; 
      } 
     } 
     if (Arrays.equals(current, last) || reachedLast > 0) { 
      reachedLast++; 
     } 
     return str; 
    } 

    private char following(char in) { 
     if (in < max) { 
      return (char) (in + 1); 
     } else { 
      return min; 
     } 
    } 

    @Override 
    public void remove() { 
     throw new UnsupportedOperationException("No with me, sir!"); 
    } 

    public static void main(String[] args) { 
     BruteForceIterator bit = new BruteForceIterator('a', 'c', 3); 
     while (bit.hasNext()) { 
      System.out.println(bit.next()); 
     } 
    } 
} 
+0

@Dukeling:看到我的編輯;) – jlordo 2013-02-23 23:22:45

+0

@MattheusWardius:您發佈的代碼執行代碼不正是我寫了... – jlordo 2013-02-24 00:05:50

+0

@MattheusWardius:看看我編輯這樣一個'Iterator'。它有'主要'方法來演示如何使用它。 – jlordo 2013-02-24 00:11:56