2009-09-12 129 views
2

我在使用彈性城堡的J2ME中解密加密文件時出現問題。 我想要做的是選擇一個文件進行加密,寫入加密文件並嘗試將其解密回其原始形式(寫入另一個文件以進行驗證)。在J2ME中加密和解密文件

我在閱讀加密文件時發生此錯誤。

Stack Trace : 
s: pad block corrupted 
     at j.a(+219) 
     at e.c(+38) 
     at e.b(+30) 
     at com.aaron.midlets.BluetoothServerMidlet.c(+134) 
     at com.aaron.midlets.BluetoothServerMidlet.b(+161) 
     at com.aaron.midlets.BluetoothServerMidlet.a(+67) 
     at com.aaron.midlets.BluetoothServerMidlet.startApp(+105) 
     at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:43) 
     at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:374) 
     at com.sun.midp.main.Main.runLocalClass(Main.java:466) 
     at com.sun.midp.main.Main.main(Main.java:120) 

這裏是我的代碼部分:

private void createEncryptFile() { 
    FileConnection fc = FileListingUtil.getFile("root1/", "test.encrypt"); 
    try { 
     fc.create(); 
     readAndEncrypt(); 
    } catch (Exception e) { 
    } 
} 

private void readAndEncrypt() { 
    FileConnection fc = FileListingUtil.getFile("root1/", "test.original"); 
    FileConnection fc2 = FileListingUtil.getFile("root1/", "test.encrypt"); 

    try { 
     InputStream test = fc.openDataInputStream(); 
     OutputStreamWriter output = new OutputStreamWriter(fc2.openOutputStream()); 

     int fileSize = (int) fc.fileSize(); 
     byte[] imgData = new byte[fileSize]; 

     int bytesRead = 0; 
     while (bytesRead < fileSize) { 
      bytesRead += test.read(imgData, bytesRead, fileSize - bytesRead); 
     } 

     EncryptorUtil util = new EncryptorUtil("12345678"); 
     try { 
      byte[] dataE = util.encrypt(imgData); 
      for (int y = 0; y < dataE.length; ++y) { 
       output.write((int) dataE[y]); 
      } 
     } catch (CryptoException ex) { 
      ex.printStackTrace(); 
     } 
     test.close(); 
     output.close(); 

     createDecryptFile(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

private void createDecryptFile() { 
    FileConnection fc = FileListingUtil.getFile("root1/", "test.decrypt"); 
    try { 
     fc.create(); 
     readAndDecrypt(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void readAndDecrypt() { 
    FileConnection fc = FileListingUtil.getFile("root1/", "test.encrypt"); 
    FileConnection fc2 = FileListingUtil.getFile("root1/", "test.decrypt"); 

    try { 
     InputStream test = fc.openDataInputStream(); 
     OutputStreamWriter output = new OutputStreamWriter(fc2.openOutputStream()); 

     int fileSize = (int) fc.fileSize(); 
     byte[] imgData = new byte[fileSize]; 

     int bytesRead = 0; 
     while (bytesRead < fileSize) { 
      bytesRead += test.read(imgData, bytesRead, fileSize - bytesRead); 
     } 

     EncryptorUtil util = new EncryptorUtil("12345678"); 

     try { 
      byte[] dataE = util.decrypt(imgData); 
      for (int y = 0; y < dataE.length; ++y) { 
       output.write((int) dataE[y]);     
      } 
     } catch (CryptoException ex) { 
      ex.printStackTrace(); 
     } 
     test.close(); 
     output.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

的最後一個函數會拋出異常。

回答

6

我可以看到一個問題。您將test.encrypt文件編寫爲writer(將每個字節轉換爲char,將其加倍)。您將其讀回爲InputStream,它讀出字節。所以你的加密數據已損壞。

+1

+1 - 很好的捕獲。 – 2009-09-13 01:35:57

+0

Thx man ... 它解決了我的問題。 – 2009-09-20 14:36:22