0

ive written an Program, with that you can Encrypt and Decrypt and File.

The GUI: http://www11.pic-upload.de/05.09.14/hitob84e9j1j.png (I've translated the Buttontexts with Gimp)

Here the Encrypt and Decrypt code:

public final ActionListener encAL = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        new Thread(new Runnable() {

            @Override
            public void run() { // Starting encryt the File:
                File inputfile = new File(input.getText());
                File outputfile = new File(output.getText());
                String algo = alg.getText(); // Alogrythmus (Default AES)

                {
                    boolean enabled = false;
                    input.setEnabled(enabled);
                    inputBrowse.setEnabled(enabled);
                    output.setEnabled(enabled);
                    outputBrowse.setEnabled(enabled);
                    enc.setEnabled(enabled);
                    dec.setEnabled(enabled);
                    keytf.setEnabled(enabled);
                    alg.setEnabled(enabled);
                    procbar.setEnabled(true);
                }

                procbar.setValue(0);

                ArrayList<Byte> inBytes = new ArrayList<Byte>();

                try {
                    InputStream instream = new FileInputStream(inputfile);
                    OutputStream outstream = new FileOutputStream(outputfile);

                    MessageDigest md = MessageDigest.getInstance("MD5");
                    byte[] key = md.digest(keytf.getText().getBytes("UTF-8"));// Get md5Hash of the Key
                    SecretKey secret_key = new SecretKeySpec(key, algo);

                    Cipher cipher = Cipher.getInstance(algo);
                    cipher.init(Cipher.ENCRYPT_MODE, secret_key);

                    byte[] buffer = new byte[1024];
                    int count;
                    long totalcount = 0;

                    int lastProzent = -1;

                    while ((count = instream.read(buffer)) > 0) {

totalcount = totalcount + count;

if(totalcount == inputfile.length()){
byte[] outbytes = cipher.doFinal(buffer);
System.out.println("DoFinal");
outstream.write(outbytes);
}else{
byte[] outbytes = cipher.update(buffer);

outstream.write(outbytes);
}


                        int Prozent = (int) ((totalcount * 100) / inputfile.length());
                        if (Prozent != lastProzent) {
                            lastProzent = Prozent;
                            procbar.setValue(Prozent);
                        }

                    }

                    instream.close();
                    outstream.flush();
                    outstream.close();
                    info.setText("Datei erfolgreich verschlüsselt.");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                {
                    boolean enabled = true;
                    input.setEnabled(enabled);
                    inputBrowse.setEnabled(enabled);
                    output.setEnabled(enabled);
                    outputBrowse.setEnabled(enabled);
                    enc.setEnabled(enabled);
                    dec.setEnabled(enabled);
                    keytf.setEnabled(enabled);
                    alg.setEnabled(enabled);
                    procbar.setEnabled(false);
                }

            }
        }).start();

    }
};

    public final ActionListener decAL = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        new Thread(new Runnable() {

            @Override
            public void run() {// Decrypt file
                File inputfile = new File(input.getText());
                File outputfile = new File(output.getText());
                String algo = alg.getText();

                {
                    boolean enabled = false;
                    input.setEnabled(enabled);
                    inputBrowse.setEnabled(enabled);
                    output.setEnabled(enabled);
                    outputBrowse.setEnabled(enabled);
                    enc.setEnabled(enabled);
                    dec.setEnabled(enabled);
                    keytf.setEnabled(enabled);
                    alg.setEnabled(enabled);
                    procbar.setEnabled(true);
                }

                procbar.setValue(0);

                ArrayList<Byte> inBytes = new ArrayList<Byte>();

                try {
                    InputStream instream = new FileInputStream(inputfile);
                    OutputStream outstream = new FileOutputStream(outputfile);

                    MessageDigest md = MessageDigest.getInstance("MD5");
                    byte[] key = md.digest(keytf.getText().getBytes("UTF-8")); // Get md5Hash of the Key
                    SecretKey secret_key = new SecretKeySpec(key, algo);

                    Cipher cipher = Cipher.getInstance(algo);
                    cipher.init(Cipher.DECRYPT_MODE, secret_key);

                    byte[] buffer = new byte[1024];
                    int count;
                    long totalcount = 0;

                    int lastProzent = -1;

                    while ((count = instream.read(buffer)) > 0) {


totalcount = totalcount + count;
if(totalcount == inputfile.length()){
byte[] outbytes = cipher.doFinal(buffer);
System.out.println("DoFinal");
outstream.write(outbytes);
}else{
byte[] outbytes = cipher.update(buffer);

outstream.write(outbytes);
}


                        int Prozent = (int) ((totalcount * 100) / inputfile.length());
                        if (Prozent != lastProzent) {
                            lastProzent = Prozent;
                            procbar.setValue(Prozent);
                        }

                    }

                    instream.close();
                    outstream.flush();
                    outstream.close();

                } catch (Exception e) {
                    info.setText(e.getClass().getName() + ": " + e.getMessage());
                    e.printStackTrace();
                }
                info.setText("Datei erfolgreich entschlüsselt.");
                {
                    boolean enabled = true;
                    input.setEnabled(enabled);
                    inputBrowse.setEnabled(enabled);
                    output.setEnabled(enabled);
                    outputBrowse.setEnabled(enabled);
                    enc.setEnabled(enabled);
                    dec.setEnabled(enabled);
                    keytf.setEnabled(enabled);
                    alg.setEnabled(enabled);
                    procbar.setEnabled(false);
                }

            }
        }).start();

    }
};

The encryption works fine, but the if I want to decrypt the encrypted file, I get following error:

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:1922)
at mainpkg.Window$InputPanel$2$1.run(Window.java:263) //Window = Name of my Class
at java.lang.Thread.run(Thread.java:745) // The "cipher.doFinal(buffer);"-line in decAL

The Error appears at round about the half of the bytes.

Does anyone know why?

LinusCDE
  • 141
  • 3
  • 8
  • 1
    possible duplicate of [AES Error: Given final block not properly padded](http://stackoverflow.com/questions/15622924/aes-error-given-final-block-not-properly-padded) – DavidPostill Sep 04 '14 at 22:58
  • 2
    Why are you calling `doFinal`? – user253751 Sep 04 '14 at 23:00
  • Exactly. Surely you can see that that there is something wrong with calling doFinal() *in a loop?* – user207421 Sep 04 '14 at 23:13
  • What should i do else? And in the encryption it works fine. – LinusCDE Sep 04 '14 at 23:18
  • 1
    `Cipher.update()` should be called for all data except the last, or *final*, piece of encrypted data. For that piece you call ... you guessed it, `Cipher.doFinal()` – President James K. Polk Sep 04 '14 at 23:20
  • @DavidPostill The method breaks the encryption to: java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV at com.sun.crypto.provider.CipherCore.init(CipherCore.java:449) at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:217) at javax.crypto.Cipher.init(Cipher.java:1183) at javax.crypto.Cipher.init(Cipher.java:1120) at mainpkg.Window$InputPanel$1$1.run(Window.java:168) at java.lang.Thread.run(Thread.java:745) – LinusCDE Sep 04 '14 at 23:26
  • As already stated, you must call `cipher.update()` inside the read-write cycle for all intermediate data blocks and call `cipher.doFinal()` once after the cycle. And saying it does not work and posting another exception in the comments means that you are trying to use StackOverflow as a debugger. – Oleg Estekhin Sep 05 '14 at 05:31

0 Answers0