2017-04-02 81 views
0

我已經查看過有關此主題的幾篇帖子,例如this帖子用於實際加密/解密,以及this帖子使用字符串生成密鑰爲一個種子來生成一個隨機密鑰。加密將我的文件大小從3mb減少到16個字節,解密將其進一步減小到0字節。我也關注了這個主題的this YouTube video,並且我的代碼在解密過程中遇到了與文件大小減少到零的相同問題,而他的工作正常。Java使用AES和散列密碼作爲密鑰對圖像文件進行加密和解密

這是我的函數生成基於一個SHA256散列作爲重點k傳遞:

public static Key keyGen(String k) throws Exception { 
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); 
    KeySpec spec = new PBEKeySpec(k.toCharArray(), k.getBytes(), 12, 128); 
    SecretKey tmp = factory.generateSecret(spec); 
    return new SecretKeySpec(tmp.getEncoded(), "AES"); 
} 

這是我使用的加密和解密功能:

public static void encrypt(Key key, byte[] content) throws Exception { 
     Cipher cipher; 
     byte[] encrypted = null; 
     try { 
      cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
      cipher.init(Cipher.ENCRYPT_MODE, key); 
      encrypted = cipher.doFinal(content); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     saveFile(encrypted); 
     JOptionPane.showMessageDialog(null, "Encryption complete"); 
    } 


public static void decrypt(Key key, byte[] textCryp) throws Exception { 
     Cipher cipher; 
     byte[] decrypted = null; 
     try { 
      cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
      cipher.init(Cipher.DECRYPT_MODE, key); 
      decrypted = cipher.doFinal(textCryp); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     saveFile(decrypted); 
     JOptionPane.showMessageDialog(null, "Decryption complete"); 
    } 

功能我用於將內容寫回到文件中:

public static void saveFile(byte[] bytes) throws IOException { 
     FileOutputStream fos = new FileOutputStream("filepath/test.jpg"); 
     fos.write(bytes); 
     fos.close(); 
    } 

而最終LY,即獲取圖像文件的字節表示的功能,傳遞到加密/解密功能的「內容」:

public static byte[] getFile() { 

    File f = new File("filepath/test.jpg"); 
    InputStream is = null; 
    try { 
     is = new FileInputStream(f); 
    } catch (FileNotFoundException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } 
    byte[] content = null; 
    try { 
     content = new byte[is.available()]; 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    try { 
     is.read(content); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return content; 
} 

和主函數調用這些函數:

public static void main(String[] args) throws Exception { 
    Key key = keyGen("1234567812345678"); 
    byte[] fileContents = getFile(); 
    //I change to decrypt after I call encrypt 
    encrypt(key,fileContents); 

} 

沒有錯誤或者引發異常,寫這段代碼時引用的帖子和視頻似乎工作正常。

我真的很感激這方面的建議,因爲這是我長期努力工作的最後一部分。

+3

永遠不要使用available()。它不會做你認爲它做的事。使用https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllBytes-java.nio.file.Path-讀取文件。 –

+0

您不會顯示任何調用這些方法的代碼,因此無法確定您爲什麼獲得您聲稱獲得的結果。 –

+0

我添加了調用它們的主函數。 – CS2016

回答

0

經過更多的試驗後,我意識到我試圖運行代碼的測試文件在雲驅動器中。我將該文件複製到本地目錄,重新編寫代碼並完美運行。

+0

你的代碼仍然不正確,實際上你的'getFile()'方法就像一個反模式演示。 –

相關問題