Encryption with DES in Java

4k Views Asked by At

I'm trying to use DES encryption to encrypt a String and then decrypt it, but I haven't been able to succeed.

When I run the program, I created some System.out.println to check if it's capturing what's being typed (and it is), but it's failing to encrypt and decrypt. Returns blank and in System.out.printl returns the "Fail" that I put in the catch block of the functions

The following code is not encrypting (and therefore not decrypting) a string using DES encryption:

public class CryptographyDES {

    String KEY = "password";
    
    public SecretKeySpec createKey(String chave) {
        try {
            byte[] charac = chave.getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            charac = md.digest(charac);
            charac = Arrays.copyOf(charac, 16);
            SecretKeySpec secretKeySpec = new SecretKeySpec(charac, "DES/ECB/PKCS5Padding");
            return secretKeySpec;
        } catch (Exception e) {
            return null;
        }

    }

    
    public String crypt(String encrypt) {

        try {
            SecretKeySpec secretKeySpec = createKey(KEY);
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

            byte[] charac = encrypt.getBytes("UTF-8");
            byte[] encrypted = cipher.doFinal(charac);
            String encrypted_charac = java.util.Base64.getEncoder().encodeToString(encrypted);
            return encrypted_charac;

        } catch (Exception e) {
            return "fail";
        }
    }

    public String decrypt(String decrypt) {

        try {
            SecretKeySpec secretKeySpec = createKey(KEY);
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

            byte[] charac = Base64.getDecoder().decode(decrypt);
            byte[] decryption = cipher.doFinal(charac);
            String decrypted_charac = new String(decryption);
            return decrypted_charac;

        } catch (Exception e) {
            return "fail decrp";
        }
    }

}

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        String encrypted = "";
        
        String pass = "";

        CryptographyDES cryptography = new CryptographyDES();   

        
        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");          
        UIManager.put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14));          
        pass = JOptionPane.showInputDialog("Enter a password to be encrypted of up to 8 characters: ");
        if (pass.length() <= 8) {
            System.out.println(cryptography.crypt(pass));
            System.out.println(pass);               
            encrypted = cryptography.crypt(pass);
            JOptionPane.showMessageDialog(null, encrypted);
            System.out.println(encrypted);
            JOptionPane.showMessageDialog(null, "decrypted password: " + cryptography.decrypt(encrypted));
            
            
        } else {
            JOptionPane.showMessageDialog(null, "Error. Password longer than 8 characters. ");
            System.exit(0);
        }

    }

}

When I replace "DES/ECB/PKCS5Padding" to "AES" is working. Where am I wrong?

UPDATE

Updating with error:

java.security.InvalidKeyException: Wrong algorithm: DES required

I changed it from "DES/ECB/PKCS5Padding" to "DES" and now it's returning: "java.security.InvalidKeyException: Wrong key size"

Solution After user tips and error attempts I found the solution to the problem. Thank you guys

public class CryptographyDES {
    
        String KEY = "password";
        
        public SecretKeySpec createKey(String chave) {
            try {
                byte[] charac = chave.getBytes("UTF-8");
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                charac = md.digest(charac);
                charac = Arrays.copyOf(charac, 16);
                SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                SecretKey key = factory.generateSecret(new   DESKeySpec(charac));
                return secretKeySpec;
            } catch (Exception e) {
                return e.toString();
            }
    
        }
    
        
        public String crypt(String encrypt) {
    
            try {
                SecretKeySpec secretKeySpec = createKey(KEY);
                Cipher cipher = Cipher.getInstance("DES");
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    
                byte[] charac = encrypt.getBytes("UTF-8");
                byte[] encrypted = cipher.doFinal(charac);
                String encrypted_charac = java.util.Base64.getEncoder().encodeToString(encrypted);
                return encrypted_charac;
    
            } catch (Exception e) {
                return e.toString();
            }
        }
    
        public String decrypt(String decrypt) {
    
            try {
                SecretKeySpec secretKeySpec = createKey(KEY);
                Cipher cipher = Cipher.getInstance("DES");
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    
                byte[] charac = Base64.getDecoder().decode(decrypt);
                byte[] decryption = cipher.doFinal(charac);
                String decrypted_charac = new String(decryption);
                return decrypted_charac;
    
            } catch (Exception e) {
                return e.toString();
            }
        }
    
    }
0

There are 0 best solutions below