辦法有加密的文件是使用CipherInputStream
和CipherOutputStream
:
private BufferedImage load(String s){
BufferedImage image;
try{
image = ImageIO.read(getDecryptedStream(Buffers.class.getResourceAsStream(s)));
return image;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
private InputStream getDecryptedStream(InputStream inputStream) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, this.key);
CipherInputStream input = new CipherInputStream(inputStream, cipher);
return input;
}
使用的OutputStream將文件保存
private OutputStream getEncryptedStream(OutputStream ouputStream) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, this.key);
CipherOutputStream output = new CipherOutputStream(ouputStream, cipher);
return output;
}
的問題是,你也很難隱藏解密密鑰。如果它在Jar中的任何地方,人們可以找到它並使用它來解密圖像。 – RealSkeptic
@RealSkeptic,但我要混淆jar文件難以獲取密鑰......我不想讓它不可能解密。 – Dormin