javax.crypto.BadPaddingException and Properties

Reply

Join Date: May 2008
Posts: 4
Reputation: twilightwolf90 is an unknown quantity at this point 
Solved Threads: 0
twilightwolf90 twilightwolf90 is offline Offline
Newbie Poster

javax.crypto.BadPaddingException and Properties

 
0
  #1
May 31st, 2008
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.

  1. javax.crypto.BadPaddingException: Given final block not properly padded
  2. at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
  3. at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
  4. at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)
  5. at javax.crypto.Cipher.doFinal(DashoA13*..)
  6. at KidsTopFrame$KTPropFrame.decrypt(KidsTopFrame.java:312)
  7. at KidsTopFrame$KTPropFrame.readConfig(KidsTopFrame.java:283)
  8. at KidsTopFrame$KTPropFrame.<init>(KidsTopFrame.java:274)
  9. at KidsTopFrame.<init>(KidsTopFrame.java:38)
  10. 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.))
  1. //the properties panel
  2. private class KTPropFrame extends JFrame {
  3.  
  4. private JLabel blank, pass1Label, pass2Label, progLabel;
  5. private JTextField pass1Field, pass2Field;
  6. private JLabel[] paths, names;
  7. private JButton[] buttons;
  8. private JTextField[] nameFields, pathFields;
  9. private Container container;
  10. private JButton saveButton;
  11. private KeyGenerator kg;
  12. private Key key;
  13.  
  14. public KTPropFrame() {
  15. super("Properties");
  16. container = getContentPane();
  17.  
  18. setLayout(new GridLayout(20, 5, 5, 5));
  19.  
  20. Security.addProvider(new com.sun.crypto.provider.SunJCE());
  21. kg = null;
  22. key = null;
  23. try {
  24. kg = KeyGenerator.getInstance("DES");
  25. key = kg.generateKey();
  26. }
  27. catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30.  
  31. blank = createAndAddLabel("");
  32. pass1Label = createAndAddLabel("Set Password: ");
  33. pass1Field = createAndAddTextField();
  34. blank = createAndAddLabel("");
  35. blank = createAndAddLabel("");
  36.  
  37. blank = createAndAddLabel("");
  38. pass2Label = createAndAddLabel("Confirm Password: ");
  39. pass2Field = createAndAddTextField();
  40. blank = createAndAddLabel("");
  41. blank = createAndAddLabel("");
  42.  
  43. progLabel = createAndAddLabel("Programs:");
  44. blank = createAndAddLabel("");
  45. blank = createAndAddLabel("");
  46. blank = createAndAddLabel("");
  47. blank = createAndAddLabel("");
  48.  
  49. paths = new JLabel[ROWS * COLUMNS];
  50. names = new JLabel[ROWS * COLUMNS];
  51. buttons = new JButton[ROWS * COLUMNS];
  52. nameFields = new JTextField[ROWS * COLUMNS];
  53. pathFields = new JTextField[ROWS * COLUMNS];
  54.  
  55. for(int i = 0; i < ROWS * COLUMNS; i++) {
  56. names[i] = createAndAddLabel("Program "+(i+1)+": Name: ");
  57. nameFields[i] = createAndAddTextField();
  58. paths[i] = createAndAddLabel("Path: ");
  59. pathFields[i] = createAndAddTextField();
  60. buttons[i] = createAndAddButton("Browse", new ActionHandler(i));
  61. }
  62.  
  63. blank = createAndAddLabel("");
  64. blank = createAndAddLabel("");
  65. blank = createAndAddLabel("");
  66. blank = createAndAddLabel("");
  67. blank = createAndAddLabel("");
  68.  
  69. blank = createAndAddLabel("");
  70. blank = createAndAddLabel("");
  71. blank = createAndAddLabel("");
  72. blank = createAndAddLabel("");
  73. saveButton = createAndAddButton("Save", new SaveHandler());
  74.  
  75. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  76. setBounds((int)(screenSize.width * .125), (int)(screenSize.height * .125), (int)(screenSize.width * .75), (int)(screenSize.height * .75));
  77. setAlwaysOnTop(true);
  78. addWindowFocusListener(
  79. new WindowAdapter() {
  80. public void windowClosing(WindowEvent e) {
  81. JOptionPane.showMessageDialog(container, "Click the save button");
  82. }
  83. });
  84.  
  85. readConfig();
  86. }
  87.  
  88. //method to read the config file
  89. public void readConfig() {
  90. try {
  91. config.load(new FileInputStream(new File("config.properties")));
  92. String password = config.getProperty("Password");
  93. if (password != null || !password.equals(""))
  94. pass1Field.setText(decrypt(config.getProperty("Password")));
  95. pass2Field.setText(pass1Field.getText());
  96. for (int i = 0; i < ROWS * COLUMNS; i++) {
  97. nameFields[i].setText(config.getProperty("Program"+(i+1)+"Name"));
  98. pathFields[i].setText(config.getProperty("Program"+(i+1)+"Path"));
  99. }
  100. }
  101. catch (Exception e) {}
  102. }
  103.  
  104. //method to encrypt the password
  105. private String encrypt(String s) {
  106. try {
  107. Cipher cipher = Cipher.getInstance("DES");
  108. cipher.init(Cipher.ENCRYPT_MODE,key);
  109. byte[] encrypted = cipher.doFinal(s.getBytes());
  110. return new String(encrypted);
  111. }
  112. catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. return null;
  116. }
  117.  
  118. //method to decrypt the password
  119. private String decrypt(String s) {
  120. try {
  121. Cipher cipher = Cipher.getInstance("DES");
  122. cipher.init(Cipher.DECRYPT_MODE,key);
  123. byte[] decrypted = cipher.doFinal(s.getBytes());
  124. return new String(decrypted);
  125. }
  126. catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. return null;
  130. }
  131.  
  132. private class ActionHandler implements ActionListener {
  133.  
  134. private int index;
  135.  
  136. public ActionHandler(int i) {
  137. index = i;
  138. }
  139.  
  140. public void actionPerformed(ActionEvent e) {
  141. final JFileChooser fc = new JFileChooser();
  142. fc.setFileFilter(new FileNameExtensionFilter("Executable Files", "exe", "bat", "cmd", "com"));
  143. int returnVal = fc.showOpenDialog(container);
  144. if(returnVal == JFileChooser.APPROVE_OPTION){
  145. File executable = fc.getSelectedFile();
  146. pathFields[index].setText(executable.getAbsolutePath());
  147. }
  148. }
  149. }
  150.  
  151. private class SaveHandler implements ActionListener {
  152. public void actionPerformed(ActionEvent e) {
  153. try {
  154. configFile.createNewFile();
  155. FileOutputStream fos = new FileOutputStream(configFile);
  156. if(pass1Field.getText().equals(pass2Field.getText()))
  157. config.setProperty("Password", encrypt(pass1Field.getText()));
  158. for (int i = 0; i < ROWS * COLUMNS; i++) {
  159. try {
  160. config.setProperty("Program"+(i+1)+"Name", "" + nameFields[i].getText());
  161. config.setProperty("Program"+(i+1)+"Path", "" + pathFields[i].getText());
  162. }
  163. catch (NullPointerException npe) {
  164. continue;
  165. }
  166. }
  167. config.store(fos, "Properties file for KidsTop. Do not modify. If there is a problem, delete this file and run the software again.");
  168. fos.close();
  169. container.validate();
  170. prop.setVisible(false);
  171. }
  172. catch (IOException ioe) {
  173. ioe.printStackTrace();
  174. System.exit(0);
  175. }
  176. }
  177. }
  178.  
  179. //method to create and add a button to the panel
  180. private JButton createAndAddButton(String s, ActionListener li) {
  181. JButton temp = new JButton();
  182. temp.setText(s);
  183. temp.addActionListener(li);
  184. add(temp);
  185. return temp;
  186. }
  187.  
  188. //method to create and add a label to the panel
  189. private JLabel createAndAddLabel(String s) {
  190. JLabel temp = new JLabel();
  191. temp.setText(s);
  192. add(temp);
  193. return temp;
  194. }
  195.  
  196. //method to create and add a text field to the panel
  197. private JTextField createAndAddTextField() {
  198. JTextField temp = new JTextField();
  199. add(temp);
  200. return temp;
  201. }
  202. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 4
Reputation: twilightwolf90 is an unknown quantity at this point 
Solved Threads: 0
twilightwolf90 twilightwolf90 is offline Offline
Newbie Poster

Re: javax.crypto.BadPaddingException and Properties

 
0
  #2
May 31st, 2008
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.

  1. import javax.crypto.*;
  2. import java.security.*;
  3. import java.util.Properties;
  4. import java.io.*;
  5.  
  6. public class CryptoTest {
  7.  
  8. private KeyGenerator kg;
  9. private Key key;
  10. private Properties config;
  11. private File configFile;
  12. private String password;
  13.  
  14. //constructor
  15. public CryptoTest() {
  16. //initialize
  17. config = new Properties();
  18. configFile = new File("config.properties");
  19. password = "password";
  20.  
  21. Security.addProvider(new com.sun.crypto.provider.SunJCE());
  22.  
  23. //throws off compiler error
  24. kg = null;
  25. key = null;
  26.  
  27. try {
  28. configFile.createNewFile();//I don't care whether this is true or false. Just makes sure that the file exists.
  29. kg = KeyGenerator.getInstance("DES");
  30. key = kg.generateKey();
  31. }
  32. catch (Exception e) { //so many exceptions... so little time XD
  33. e.printStackTrace();
  34. }
  35.  
  36. //"save"
  37. config.setProperty("Password", encrypt(password));
  38. }
  39.  
  40. public static void main(String[] args) {
  41. CryptoTest ct = new CryptoTest();
  42. System.out.println(ct);
  43. }
  44.  
  45. //method used to encrypt password
  46. private String encrypt(String s) {
  47. try {
  48. Cipher cipher = Cipher.getInstance("DES");
  49. cipher.init(Cipher.ENCRYPT_MODE,key);
  50. byte[] encrypted = cipher.doFinal(s.getBytes());
  51. return new String(encrypted);
  52. }
  53. catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. return null;
  57. }
  58.  
  59. //method to decrypt the password
  60. private String decrypt(String s) {
  61. try {
  62. Cipher cipher = Cipher.getInstance("DES");
  63. cipher.init(Cipher.DECRYPT_MODE,key);
  64. byte[] decrypted = cipher.doFinal(s.getBytes());
  65. return new String(decrypted);
  66. }
  67. catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. return null;
  71. }
  72.  
  73. public String toString() {
  74. return "Password: "+password+"\n"+
  75. "Encrypted: "+config.getProperty("Password")+"\n"+
  76. "Decrypted: "+decrypt(config.getProperty("Password"));
  77. }
  78. }
Attached Files
File Type: java KidsTopFrame.java (12.1 KB, 2 views)
File Type: java KidsTopDriver.java (201 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC