Stack Exchange
Decrypting image using java
I take this code from YouTube video.From this code I encrypt image correctly but could not decrypt that image..
Can anyone help me???
Encrypt code
        FileInputStream  file = new FileInputStream("src/image/A.jpg");
        FileOutputStream output = new FileOutputStream("src/image/AA.jpg");
        byte j[]="12345678".getBytes();
        SecretKeySpec kye = new SecretKeySpec(j,"DES");
        System.out.println(kye);
        Cipher enc = Cipher.getInstance("DES");
        enc.init(Cipher.ENCRYPT_MODE,kye);
        CipherOutputStream cos = new CipherOutputStream(output, enc);
        byte[] buf = new byte[1024];
        int read;
        while((read=file.read(buf))!=-1){
            cos.write(buf,0,read);
        }
        file.close();
        output.flush();
        cos.close();

Decrypt code
        FileInputStream  file = new FileInputStream("src/image/AA.jpg");
        FileOutputStream output = new FileOutputStream("src/image/AAA.jpg");
        byte j[]="12345678".getBytes();
        SecretKeySpec kye = new SecretKeySpec(j,"DES");
        System.out.println(kye);
        Cipher enc = Cipher.getInstance("DES");
        enc.init(Cipher.DECRYPT_MODE,kye);
        CipherOutputStream cos = new CipherOutputStream(output, enc);
        byte[] buf = new byte[1024];
        int read;
        while((read=file.read(buf))!=-1){
            cos.write(buf,0,read);
        }
        file.close();
        output.flush();
        cos.close();

thank you

If you can't decrypt it why are you sure you encrypted it correctly?

You should probably flush() (if you're going to flush()) before you close().

because I had a image that invalid image.
Is there something wrong????

@NoughT Is it working correctly?

@ElliottFrisch No it is not working...

I just figure out.
Even encryption code is not working.
After run that encrypt code,it gives 0 byte output image.

Your code works correctly for me, when I tried it. I've voted to close as I can't reproduce your problem with the code supplied.

I've tested your code and it is working well on my workstation. Did you run the encryption and decryption on the same computer? the getBytes() method depends on the actual system configured encoding.

I figure it out...
It is on encryption key.
key must be 10byte code.
thank all of you

@NoughT Well.. your code worked without modification on my machine, so your fix sounds like it's not really a fix. Perhaps take heed of Jcs' comments?

That code has the same quality as YouTube comments, please do not use it on production machines. @Duncan Yeah, it runs, and that's regrettable.

@NoughT  have u achieved your target succesffully is your decryption algorithm working ??

It is a relativly old post but I think I can help.
First, you should encode the Image into a ASCII representation. I would recommend Base64. It is much easier and less error attached when encrypting Base64. (Maybe not as strong but that depends on your needs)
The benefit of Base64 is the Alphabet it is using. No weird symbols at all. 
1) Convert the image into a ByteArrayOutputStream by writing it with the ImageIO Class into one. 
2) Encode the byte array into a Base64 String
3) Encrypt like you did above (Do not forget the flush).
4) Save bytes to new File. Delete old one.
Decrypt accordingly .....
Be aware,  encoding into Base64 will blow up your memory and the file will be much bigger because of the Base64 AND the Encryption overhead.
Hope that helps ! 
