0

I am making a 2D game in Java for the PC and this game has data like sprites, sound effects, music, and text.

The problem is that I need to store it somehow.

For now I need to store my sprites in an encrypted format and I need my Java game To decrypt and load the encrypted images To bufferedimages to display them on my game.

I don't want to encapsulate everything into a single executable .jar or .exe file.

I need to make my images(Resources,Spritesheets) encrypted and secure because I don't want to the player to interfere with them or use them.

I can't use ZIP files even encrypted or protected ones by using some sort of a Java library because Java have to export them first on the disc before it uses it

I have searched many forums and I can't find a clear answer.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 1
    There is no security in obscurity. If you want to protect your images from tampering use hashes. If you want to forbid their use by a third party it's a copyright thing. If you display your images at some point one can make a screenshot and get the images that way. Also Java code can easily be decompiled so anyone who has some Java knowledge can decode your images. You're putting a lot of effort into a utterly useless feature. – konqi Apr 24 '15 at 15:27

1 Answers1

0

Depending upon what sort of encryption you had in mind - you can use ImageIO to write/read the image to Output and Input Streams, taking the resulting bytes and decrypt/encrypt.

To save the image, use ImageIO to write the Image to an OutputStream (for example a ByteArrayOutputStream ). From the bytes written, you can encrypt, and then save

    ByteArrayOutputStream os = null;
    OutputStream fos = null;
    try{
        os = new ByteArrayOutputStream(); 
        ImageIO.write(image, "jpg", os);
        byte[] bytes = os.toByteArray();
        encrypt(bytes);
        fos = new FileOutputStream(outputfile);
        fos.write(bytes);
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        if ( fos != null ){try{fos.close();}catch(Exception e){}}
        if ( os != null ){try{os.close();}catch(Exception e){}}//no effect, but hear for sanity's sake
    }

To read the and decrypt, just read the file as bytes, decrypt the bytes, then send the byte stream to ImageIO

    InputStream is = null;
    ByteArrayOutputStream os = null;
    ByteArrayInputStream input = null;
    try{
        is = new FileInputStream(inputFile);
        os = new ByteArrayOutputStream ();
        byte[] buffer = new byte[500];
        int len = -1;
        while ( ( len = is.read(buffer) ) != -1 ){
            os.write(buffer, 0, len);
        }
        byte[] fileBytes = os.toByteArray();
        decrypt(fileBytes);
        input = new ByteArrayInputStream(fileBytes);
        Image image = ImageIO.read(input);
    }catch(IOException io){
        io.printStackTrace();
    }finally{
        if ( is != null ){try{is.close();}catch(Exception e){}}
        if ( os != null ){try{os.close();}catch(Exception e){}}
        if ( input != null ){try{input.close();}catch(Exception e){}}
    }

The type of encryption you use is your choice. You can use a simple Cipher to encrypt the byte array using a bitwise exclusive or (^)

for ( int i = 0; i < bytes.length; i++ ){
    bytes[i] = (byte)(bytes[i] ^ 123);
}

Or more fancy encryption using a Cipher

Note that for animated gif's, you may need to search for a way to save the frames of the gif (for instance see this)

Community
  • 1
  • 1
copeg
  • 8,290
  • 19
  • 28
  • For a simple byte-by-byte cipher (i.e. a Stream Cipher) try [RC4](http://en.wikipedia.org/wiki/RC4). It is obsolescent, but probably secure enough for what you seem to need. For something more secure, and heavy-duty, use AES in CTR mode. – rossum Apr 23 '15 at 20:03