3 Replies - 424 Views - Last Post: 15 September 2016 - 09:02 PM Rate Topic: -----

#1 Deviance  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 16

Java Picture decryption assignment help

Posted 15 September 2016 - 05:41 AM

Hello, so here is the problem

For an assignment, we have to decrypt 5 encrypted picture files. The assignment has given us the key: "2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c" with the parameters that the files were padded and that
1.The file size is represented in 8 bytes, where the first byte is the most significant byte.
2.The size of the padding is the number of bytes needed to make [file_size+padding_size+8] a multiple of 16.
3.The bytes passed for encryption are: original_file, padding, file_size.

I'm having trouble getting the file decrypted. I have ran into the problem of getting a BadPaddingException. I do not understand this error. I think I am doing something wrong with the key as I am passing the file bytes correctly.As a background: I do have java experience but not in encryption and this class has nothing to do with coding encryption but this is the assignment anyway. Any feedback will help. P.s. My code isnt well organized but I'll redo it once I can get it to work.

private String transformation = "AES/ECB/NoPadding";
private void decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException{
    try {
        String key = "2be151628aed2a6abf7158809cf4f3c";
        FileInputStream in = new FileInputStream("AESencrypt_1");
        FileOutputStream outStream = new FileOutputStream("AESencrypt_1.jpeg");
        Key secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher c = Cipher.getInstance(transformation);
        c.init(Cipher.DECRYPT_MODE, secretKey);
        try(CipherInputStream cis = new CipherInputStream(in,c)){
            int read;
            byte[] buf =Files.readAllBytes(new File("AESencrypt_1").toPath());
            while((read = cis.read(buf)) != -1){
                outStream.write(buf, 0, read);
            }
                
        }
        /*
        byte[] inBytes = 
        
        in.read(inBytes);
        byte[] outBytes = c.doFinal(inBytes);
        outStream.write(outBytes);*/
        in.close();
        outStream.close();
        
        
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
        
    
}
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException{
    String key = "2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c";
    Main main = new Main();
    main.decrypt();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java Picture decryption assignment help

#2 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 658
  • View blog
  • Posts: 4,964
  • Joined: 25-December 13

Re: Java Picture decryption assignment help

Posted 15 September 2016 - 06:12 AM

Have you tried with a simpler case to work out the details? For example encrypt a small text file and then try decrypting it. That would make it easier for you to post the input file and the full program code for testing.
Was This Post Helpful? 0
  • +
  • -

#3 Deviance  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 16

Re: Java Picture decryption assignment help

Posted 15 September 2016 - 06:40 AM

View PostNormR, on 15 September 2016 - 06:12 AM, said:

Have you tried with a simpler case to work out the details? For example encrypt a small text file and then try decrypting it. That would make it easier for you to post the input file and the full program code for testing.

Yes I have. I tryed encrypting and decrypting text. And even a picture but for somereason I cannot decrypt the assignment pictures. I believe it has something to either do with the key or I am not doing something with the bytes correctly. The pictures are padded and I think im not doing something right there
Was This Post Helpful? 0
  • +
  • -

#4 RobertSkii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 05-September 14

Re: Java Picture decryption assignment help

Posted 15 September 2016 - 09:02 PM

what error are you getting on the console log? It would be really helpful to know what error and in what line(s) are you getting it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1