如何加密 BufferedImage 以仅由程序读取?

How can I encrypt a BufferedImage to only be read by the program?

我在 class 命名的缓冲区中有这个方法:

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