0

Hi guys I have to do this and I can encrypt file according to the des algorithm but I can not decyrpt again file ,I recieve error messaje like that :

javax.crypto.BadPaddingException Given final block not properly padded

I can not decrypt file I couldnt find why. Can u help me please Thank you guys.

JAVA CODE :

public class Sifreleme {

    public static void encrypt(){
     try {
            SecretKey key = KeyGenerator.getInstance("DES").generateKey();

            FileOutputStream fosKey = new FileOutputStream("..\\KEY");
            SecretKeyFactory keyfac = SecretKeyFactory.getInstance("DES");
            DESKeySpec keyspec = (DESKeySpec) keyfac.getKeySpec(key, DESKeySpec.class);
            fosKey.write(keyspec.getKey());
            fosKey.close();

            Cipher crypt = Cipher.getInstance("DES");
            crypt.init(Cipher.ENCRYPT_MODE, key);

            FileInputStream fis = new FileInputStream("C:\\Users\\akif\\Desktop\\zilsesi.mp3");
            FileOutputStream fos = new FileOutputStream("C:\\Users\\akif\\Desktop\\sifrelenenzilsesi.mp3");
            byte[] arrayBytes = new byte[8];
            int bytesReads;
            while ((bytesReads = fis.read(arrayBytes)) != -1) {
                fos.write(crypt.doFinal(arrayBytes), 0, bytesReads);
            }
            fis.close();
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }




public static void decrypt(){
       try {
                FileInputStream fisKey = new FileInputStream("..\\KEY");
                byte[] arrayKey = new byte[fisKey.available()];
                fisKey.read(arrayKey);
                SecretKey key = new SecretKeySpec(arrayKey, "DES");

                Cipher decrypt = Cipher.getInstance("DES");
                decrypt.init(Cipher.DECRYPT_MODE, key);

                FileInputStream fis = new FileInputStream("C:\\Users\\akif\\Desktop\\sifrelenenzilsesi.mp3");
                byte[] encText = new byte[16];
                int bytesReads;
                while ((bytesReads = fis.read(encText)) != -1) {
                    fis.read(decrypt.doFinal(encText), 0, bytesReads);
                }
                fis.close();
                System.out.println(new String(encText));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String []args) throws IOException{
        encrypt();
        decrypt();
        }
2
  • 1
    You are calling decrypt.doFinal(encText) before reading all of the bytes from the file. Read all bytes, then pass them to the decrypter. Commented May 9, 2017 at 14:20
  • Do I need to use decrypt.update(enctext) ? because I didnt understand this dofinal code line , Where will I use at ? Commented May 9, 2017 at 15:09

1 Answer 1

2

Your code here:

    while ((bytesReads = fis.read(encText)) != -1) {
        fis.read(decrypt.doFinal(encText), 0, bytesReads);
    }

Is rather obviously wrong: you need to write the plaintext generated by calling decrypt.doFinal just like you do during encryption. Currently you are overwriting the generated plaintext by the next block(s) of ciphertext because you call read twice in the loop.

Furthermore, depending on your DES Cipher implementation, you forgot about the IV.


A lot of other things are wrong as well, including:

  • the stream handling using getAvailable();
  • the use of the 56 bit DES cipher;
  • the use of ECB mode;
  • the repeated calls to doFinal (which results in a very large overhead and insecure code);
  • not using the CipherInputStream and CipherOutputStream (etcetera);
  • using a string as the key;
  • forgetting to close your streams when an exception occurs (use the try with resources);
  • the printStackTracke() exception handling;
  • the use of static fields as variables.

Using the platform encoding within new String(encText) is only likely wrong.


Note that using the wrong key / ciphertext combination will likely also result in this error.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I'm used to perform code reviews now and then :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.