我在名为Buffers的类中具有此方法:

private static BufferedImage load(String s){
    BufferedImage image;
            try{
                image = ImageIO.read(Buffers.class.getResourceAsStream(s));
                return image;
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
}


项目中的所有图形内容都用于加载图像。例:

public static BufferedImage background = load("/path/");




我想知道是否有一种方法仅加载加密的图像,然后仅在通过此方法调用时才解密。

如果对我要问的问题有任何疑问,请告诉我。

谢谢!

最佳答案

加密文件的一种方法是使用CipherInputStreamCipherOutputStream

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;
}

10-05 21:16
查看更多