办法有加密的文件是使用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