0

When I run my program I am given this error:

javax.crypto.IllegalBlockSizeException: 
Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at FileManagment.readEcryptedText(FileManagment.java:90)
at AccountLoginFrame.main(AccountLoginFrame.java:35)

The error directs me to the readEncryptedText void at the bottom of the code.

This is my code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;


public class FileManagment {

    static CreateAccountFrame caf = new CreateAccountFrame();

    File file = new File("accounts.lockedfilecausewhynot");

    Cipher desCipher;

    byte[] usernameEncrypted;
    byte[] passwordEncrypted;
    byte[] chosenUsername;
    byte[] chosenPassword;
    byte[] storedUsername;
    byte[] storedPassword;
    byte[] decryptedUsername;
    byte[] decryptedPassword;

    public void createAccountsFile(){
        try {
            if(!file.exists()){
                file.createNewFile();
            }
        } catch (IOException e){
                e.printStackTrace();
        }
    }

    public void addEncryptedAccount(){
        try {
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            SecretKey desKey = kg.generateKey();

            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            desCipher.init(Cipher.ENCRYPT_MODE, desKey);

            chosenUsername = caf.chooseUsername.getText().getBytes("UTF-8");
            chosenPassword = caf.choosePassword.getText().getBytes("UTF-8");
            usernameEncrypted = desCipher.doFinal(chosenUsername);
            passwordEncrypted = desCipher.doFinal(chosenPassword);

            String encryptedUsername = new String(usernameEncrypted);
            String encryptedPassword = new String(passwordEncrypted);

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw= new BufferedWriter(fw);
            bw.write(encryptedUsername);
            bw.newLine();
            bw.write(encryptedPassword);
            bw.close(); 
        } catch (IOException | NoSuchAlgorithmException |InvalidKeyException 
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e){
            e.printStackTrace();
    }
}

    public void readEcryptedText(){
        try {
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            SecretKey desKey = kg.generateKey();

            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);

            String encryptedText = br.readLine();
            System.out.println(encryptedText);

            storedUsername = encryptedText.getBytes("UTF-8");

            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            desCipher.init(Cipher.DECRYPT_MODE, desKey);

            decryptedUsername = desCipher.doFinal(storedUsername);

            String decryptedText = new String(decryptedUsername).toString();
            System.out.println("decryptedText");

        } catch (NoSuchAlgorithmException | NoSuchPaddingException 
            | InvalidKeyException | IOException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
}

}

I have no clue what the error is as I have literally used the internet to help me with all this encrypting and decrypting code.

How can I stop the error and get the code to decrypt the test?

  • Welcome to the world of programming. Have you tried googling the error? Have you single stepped through your code to find out exactly where the error occurs? – StillLearnin Sep 11 '15 at 21:07
  • Print out the *length* of storedUserName *before* the call to desCipher.doFinal. – Mike Summers Sep 11 '15 at 21:35
  • 2
    Don't transform bytes into strings. That doesn't make sense. The bytes don't represent a valid encoded string. And even if they did, they could very well contain newline characters. Also, all your variables should be local variables, not fields. And you should properly close the readers and writers, using the try-with-resources statement. – JB Nizet Sep 11 '15 at 21:59
  • 2
    Write out 100 times. 'String is not a container for binary data.' – user207421 Sep 11 '15 at 22:25

0 Answers0