0

I am creating a properties JFrame for use in my GUI, and I wanted to save all of the data inputted into the frame into a file called config.properties. The Poperties class already has several convienent methods to save, load, and get properties, but it stores all the data in plain text format. This is fine for most of the data, but for the password, it is not. I wanted to encrypt the password into "DES" (some coding algorithm) and save it into the file. This has gone well... but the loading of the property file on the next startup throws a BadPaddingException.

javax.crypto.BadPaddingException: Given final block not properly padded
	at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
	at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
	at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)
	at javax.crypto.Cipher.doFinal(DashoA13*..)
	at KidsTopFrame$KTPropFrame.decrypt(KidsTopFrame.java:312)
	at KidsTopFrame$KTPropFrame.readConfig(KidsTopFrame.java:283)
	at KidsTopFrame$KTPropFrame.<init>(KidsTopFrame.java:274)
	at KidsTopFrame.<init>(KidsTopFrame.java:38)
	at KidsTopDriver.main(KidsTopDriver.java:7)

(KidsTopFrame is the public class... KTPropFrame is a private class inside of KidsTopFrame)

The apparent culprit? The decrypt() method... I have no idea why this is giving me trouble.

What I have so far: (NOTE: this is an internal private class in another of my GUI classes. For brevity, I have excluded the none pertaining code (I think... Please point out any inconsistancy and I will do my best.))

//the properties panel
private class KTPropFrame extends JFrame {
      
	private JLabel blank, pass1Label, pass2Label, progLabel;
	private JTextField pass1Field, pass2Field;
	private JLabel[] paths, names;
	private JButton[] buttons;
	private JTextField[] nameFields, pathFields;
	private Container container;
	private JButton saveButton;
	private KeyGenerator kg;
	private Key key;
	
	public KTPropFrame() {
		super("Properties");
		container = getContentPane();
		
		setLayout(new GridLayout(20, 5, 5, 5));
		
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		kg = null;
		key = null;
		try {
			kg = KeyGenerator.getInstance("DES");
			key = kg.generateKey();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		blank = createAndAddLabel("");
		pass1Label = createAndAddLabel("Set Password: ");
		pass1Field = createAndAddTextField();
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		
		blank = createAndAddLabel("");
		pass2Label = createAndAddLabel("Confirm Password: ");
		pass2Field = createAndAddTextField();
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		
		progLabel = createAndAddLabel("Programs:");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		
		paths = new JLabel[ROWS * COLUMNS];
		names = new JLabel[ROWS * COLUMNS];
		buttons = new JButton[ROWS * COLUMNS];
		nameFields = new JTextField[ROWS * COLUMNS];
		pathFields = new JTextField[ROWS * COLUMNS];
		
		for(int i = 0; i < ROWS * COLUMNS; i++) {
			names[i] = createAndAddLabel("Program "+(i+1)+": Name: ");
			nameFields[i] = createAndAddTextField();
			paths[i] = createAndAddLabel("Path: ");
			pathFields[i] = createAndAddTextField();
			buttons[i] = createAndAddButton("Browse", new ActionHandler(i));
		}
		
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		blank = createAndAddLabel("");
		saveButton = createAndAddButton("Save", new SaveHandler());
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((int)(screenSize.width * .125), (int)(screenSize.height * .125), (int)(screenSize.width * .75), (int)(screenSize.height * .75));
		setAlwaysOnTop(true);
		addWindowFocusListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					JOptionPane.showMessageDialog(container, "Click the save button");
				}
			});
			
		readConfig();
	}
     
	//method to read the config file
	public void readConfig() {
		try {
			config.load(new FileInputStream(new File("config.properties")));
			String password = config.getProperty("Password");
			if (password != null || !password.equals(""))
				pass1Field.setText(decrypt(config.getProperty("Password")));
			pass2Field.setText(pass1Field.getText());
			for (int i = 0; i < ROWS * COLUMNS; i++) {
				nameFields[i].setText(config.getProperty("Program"+(i+1)+"Name"));
				pathFields[i].setText(config.getProperty("Program"+(i+1)+"Path"));
			}
		}
		catch (Exception e) {}
        }
      
	//method to encrypt the password
	private String encrypt(String s) {
		try {
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE,key);
			byte[] encrypted = cipher.doFinal(s.getBytes());
			return new String(encrypted);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//method to decrypt the password
	private String decrypt(String s) {
		try {
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE,key);
			byte[] decrypted = cipher.doFinal(s.getBytes());
			return new String(decrypted);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private class ActionHandler implements ActionListener {
        
                                private int index;
          
		public ActionHandler(int i) {
	                	index = i;
                                }
         
		public void actionPerformed(ActionEvent e) {
			final JFileChooser fc = new JFileChooser();
			fc.setFileFilter(new FileNameExtensionFilter("Executable Files", "exe", "bat", "cmd", "com"));
			int returnVal = fc.showOpenDialog(container);
			if(returnVal == JFileChooser.APPROVE_OPTION){
				File executable = fc.getSelectedFile();
				pathFields[index].setText(executable.getAbsolutePath());
			}
		}
         	}
	
	private class SaveHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				configFile.createNewFile();
				FileOutputStream fos = new FileOutputStream(configFile);
				if(pass1Field.getText().equals(pass2Field.getText()))
					config.setProperty("Password", encrypt(pass1Field.getText()));
				for (int i = 0; i < ROWS * COLUMNS; i++) {
					try {
						config.setProperty("Program"+(i+1)+"Name", "" + nameFields[i].getText());
						config.setProperty("Program"+(i+1)+"Path", "" + pathFields[i].getText());
					}  
					catch (NullPointerException npe) {
						continue;
					}
				}
			config.store(fos, "Properties file for KidsTop.  Do not modify.  If there is a problem, delete this file and run the software again.");
			fos.close();
			container.validate();
			prop.setVisible(false);
			}
			catch (IOException ioe) {
				ioe.printStackTrace();
				System.exit(0);
			}
                                }
	}
		
	//method to create and add a button to the panel
	private JButton createAndAddButton(String s, ActionListener li) {
		JButton temp = new JButton();
		temp.setText(s);
		temp.addActionListener(li);
		add(temp);
		return temp;
	}
      
	//method to create and add a label to the panel
	private JLabel createAndAddLabel(String s) {
		JLabel temp = new JLabel();
		temp.setText(s);
		add(temp);
		return temp;
	}
	
	//method to create and add a text field to the panel
	private JTextField createAndAddTextField() {
		JTextField temp = new JTextField();
		add(temp);
		return temp;
	}
}
1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by twilightwolf90
0

I attached the full source code if you want to give it a shot.

Interestingly enough, when I created this file, I had no problems with it.

import javax.crypto.*;
import java.security.*;
import java.util.Properties;
import java.io.*;

public class CryptoTest {

	private KeyGenerator kg;
	private Key key;
	private Properties config;
	private File configFile;
	private String password;

	//constructor
	public CryptoTest() {
		//initialize
		config = new Properties();
		configFile = new File("config.properties");
		password = "password";
		
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		
		//throws off compiler error
		kg = null;
		key = null;
		
		try {
			configFile.createNewFile();//I don't care whether this is true or false.  Just makes sure that the file exists.
			kg = KeyGenerator.getInstance("DES");
			key = kg.generateKey();
		}
		catch (Exception e) { //so many exceptions... so little time XD
			e.printStackTrace();
		}
		
		//"save"
		config.setProperty("Password", encrypt(password));
	}
	
	public static void main(String[] args) {
		CryptoTest ct = new CryptoTest();
		System.out.println(ct);
    }
	
	//method used to encrypt password
	private String encrypt(String s) {
		try {
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE,key);
			byte[] encrypted = cipher.doFinal(s.getBytes());
			return new String(encrypted);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//method to decrypt the password
	private String decrypt(String s) {
		try {
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE,key);
			byte[] decrypted = cipher.doFinal(s.getBytes());
			return new String(decrypted);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
			return null;
	}
	
	public String toString() {
		return "Password: "+password+"\n"+
		"Encrypted: "+config.getProperty("Password")+"\n"+
		"Decrypted: "+decrypt(config.getProperty("Password"));
	}
}
Attachments
//Andrew Shomette Period 5
//Final GUI Project
//KidsTopDriver Driver class

public class KidsTopDriver {
	public static void main(String[] args) {
		KidsTopFrame ktf = new KidsTopFrame();
	}
}
//Andrew Shomette Period 5
//Final GUI Project
//KidsTopFrame Resource class

	import java.awt.*;
	import java.awt.event.*;
	import java.io.*;
	import java.util.*;
	import javax.swing.*;
	import javax.swing.filechooser.*;
	import java.security.*;
	import javax.crypto.*;
	
public class KidsTopFrame extends JFrame {
	
	private Container container;
	private JPanel nPanel, ePanel, cPanel;
	private JLabel banner;
	private Scanner sc;
	private int maxX, maxY;
	private final int ROWS = 3, COLUMNS = 5;
	private KTPropFrame prop;
	private Properties config;
	private JButton quitButton, propButton;
	private File configFile;
	private JButton[] progButtons;
	private JLabel[] progNames;
	private JPanel[] progPanels;
   
	public KidsTopFrame() {
		setLayout(new BorderLayout());
		
		container = getContentPane();
		sc = new Scanner(System.in);
		maxX = 0;
		maxY = 0;
		config = new Properties();
		prop = new KTPropFrame();
		configFile  = new File("config.properties");
		
		ePanel = new JPanel();
		ePanel.setLayout(new FlowLayout());
		container.add(ePanel, BorderLayout.EAST);
		quitButton = createAndAddButton("Quit", 
				new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					attemptExit();
				}}, ePanel);
		propButton = createAndAddButton("Properties", 
				new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					attemptOpenProperties();
				}}, ePanel);
      
		nPanel = new JPanel();
		nPanel.setLayout(new FlowLayout());
		container.add(nPanel, BorderLayout.NORTH);
		banner = createAndAddLabel("", nPanel);
		banner.setIcon(new ImageIcon("kidstopbanner.PNG"));
      
		setUndecorated(true);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0, 0, screenSize.width, screenSize.height);
		setMinimumSize(Toolkit.getDefaultToolkit().getScreenSize());
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(
				new WindowAdapter() {
				public void windowClosing(WindowEvent we) {
					attemptExit();
				}});
		setVisible(true);
		checkConfig();
		
		cPanel = new JPanel();
		cPanel.setLayout(new GridLayout(ROWS, COLUMNS, 5, 5));
		progPanels = new JPanel[ROWS * COLUMNS];
		progButtons = new JButton[ROWS * COLUMNS];
		progNames = new JLabel[ROWS * COLUMNS];
		for (int i = 0; i < (ROWS * COLUMNS); i++) {
			progPanels[i] = new JPanel();
			cPanel.add(progPanels[i]);
			progPanels[i].setLayout(new GridLayout(2, 1, 5, 5));
			String name = config.getProperty("Program"+i+"Name");
			try {
				progButtons[i] = createAndAddButton(name, new ProgramHandler(i), progPanels[i]);
			}
			catch (NullPointerException npe) {
				continue;
			}
		}
	}
	
	private void checkConfig() {
		try {
			config.load(new FileInputStream("config.properties"));
		}
		catch(IOException ioe) {
			JOptionPane.showMessageDialog(container, "You need to set the properties before you can use this software");
			prop.setVisible(true);
		}
	}
	
	private void attemptExit() {
		String password = "a";//change this
		String passwordTry = JOptionPane.showInputDialog(container, "What is the password");
		if(passwordTry.equals(password))
			System.exit(0);
		else
			JOptionPane.showMessageDialog(container, "The password you entered is incorrect", "", JOptionPane.ERROR_MESSAGE);
	}
	
	private void attemptOpenProperties() {
		String password = "a";//change this
		String passwordTry = JOptionPane.showInputDialog(container, "What is the password");
		if(passwordTry.equals(password))
			prop.setVisible(true);
		else
			JOptionPane.showMessageDialog(container, "The password you entered is incorrect", "", JOptionPane.ERROR_MESSAGE);
	}

	//method to create and add a button to a panel
	private JButton createAndAddButton(String s, ActionListener li, JPanel p) {
		JButton temp = new JButton();
		temp.setText(s);
		temp.addActionListener(li);
		p.add(temp);
		return temp;
	}
	
	//method to create and add a button to the frame
	private JButton createAndAddButton(String s, ActionListener li) {
		JButton temp = new JButton();
		temp.setText(s);
		temp.addActionListener(li);
		add(temp);
		return temp;
	}
   
	//method to create and add a label to a panel
	private JLabel createAndAddLabel(String s, JPanel p) {
		JLabel temp = new JLabel();
		temp.setText(s);
		p.add(temp);
		return temp;
	}

	//method to create and add a label to the frame
	private JLabel createAndAddLabel(String s) {
		JLabel temp = new JLabel();
		temp.setText(s);
		add(temp);
		return temp;
	}

	//Action handler to handle the execution of programs
	private class ProgramHandler implements ActionListener {
		
		private int index;
		
		public ProgramHandler(int i){
			index = i;
		}
		
		public void actionPerformed(ActionEvent e) {
			try {
				String cmd = config.getProperty("Program"+index+"Path");
				
				Runtime rt = Runtime.getRuntime();
				Process proc = rt.exec(cmd);
				
				// any error message?
				StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());            
				
				// any output?
				StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());
				
				// kick them off
				errorGobbler.start();
				outputGobbler.start();
				
				// any error???
				int exitVal = proc.waitFor();
			} 
				catch (Throwable t) {
					t.printStackTrace();
			}
		}
	}
   
   	//the properties panel
	private class KTPropFrame extends JFrame {
      
		private JLabel blank, pass1Label, pass2Label, progLabel;
		private JTextField pass1Field, pass2Field;
		private JLabel[] paths, names;
		private JButton[] buttons;
		private JTextField[] nameFields, pathFields;
		private Container container;
		private JButton saveButton;
		private KeyGenerator kg;
		private Key key;
		
		public KTPropFrame() {
			super("Properties");
			container = getContentPane();
			
			setLayout(new GridLayout(20, 5, 5, 5));
			
			Security.addProvider(new com.sun.crypto.provider.SunJCE());
			kg = null;
			key = null;
			try {
				kg = KeyGenerator.getInstance("DES");
				key = kg.generateKey();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
			blank = createAndAddLabel("");
			pass1Label = createAndAddLabel("Set Password: ");
			pass1Field = createAndAddTextField();
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			
			blank = createAndAddLabel("");
			pass2Label = createAndAddLabel("Confirm Password: ");
			pass2Field = createAndAddTextField();
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			
			progLabel = createAndAddLabel("Programs:");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			
			paths = new JLabel[ROWS * COLUMNS];
			names = new JLabel[ROWS * COLUMNS];
			buttons = new JButton[ROWS * COLUMNS];
			nameFields = new JTextField[ROWS * COLUMNS];
			pathFields = new JTextField[ROWS * COLUMNS];
			
			for(int i = 0; i < ROWS * COLUMNS; i++) {
				names[i] = createAndAddLabel("Program "+i+": Name: ");
				nameFields[i] = createAndAddTextField();
				paths[i] = createAndAddLabel("Path: ");
				pathFields[i] = createAndAddTextField();
				buttons[i] = createAndAddButton("Browse", new ActionHandler(i));
			}
			
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			blank = createAndAddLabel("");
			saveButton = createAndAddButton("Save", new SaveHandler());
			
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			setBounds((int)(screenSize.width * .125), (int)(screenSize.height * .125), (int)(screenSize.width * .75), (int)(screenSize.height * .75));
			setAlwaysOnTop(true);
			addWindowFocusListener(
				new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						JOptionPane.showMessageDialog(container, "Click the save button");
					}
				});
				
			readConfig();
		}
      
		//method to read the config file
		public void readConfig() {
			try {
				config.load(new FileInputStream(new File("config.properties")));
				String password = config.getProperty("Password");
				if (password != null || !password.equals(""))
					pass1Field.setText(decrypt(config.getProperty("Password")));
				pass2Field.setText(pass1Field.getText());
				for (int i = 0; i < ROWS * COLUMNS; i++) {
					nameFields[i].setText(config.getProperty("Program"+i+"Name"));
					pathFields[i].setText(config.getProperty("Program"+i+"Path"));
				}
			}
			catch (Exception e) {}
         }
      
		//method to encrypt the password
		private String encrypt(String s) {
			try {
				Cipher cipher = Cipher.getInstance("DES");
				cipher.init(Cipher.ENCRYPT_MODE,key);
				byte[] encrypted = cipher.doFinal(s.getBytes());
				return new String(encrypted);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		//method to decrypt the password
		private String decrypt(String s) {
			try {
				Cipher cipher = Cipher.getInstance("DES");
				cipher.init(Cipher.DECRYPT_MODE,key);
				byte[] decrypted = cipher.doFinal(s.getBytes());
				return new String(decrypted);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		private class ActionHandler implements ActionListener {
         
            private int index;
         
			public ActionHandler(int i) {
			index = i;
            }
         
			public void actionPerformed(ActionEvent e) {
				final JFileChooser fc = new JFileChooser();
				fc.setFileFilter(new FileNameExtensionFilter("Executable Files", "exe", "bat", "cmd", "com"));
				int returnVal = fc.showOpenDialog(container);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					File executable = fc.getSelectedFile();
					pathFields[index].setText(executable.getAbsolutePath());
				}
			}
         
		}
		
		private cl
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.