2017-04-02 150 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()'方法就像一个反模式演示。 –

相关问题