Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPasswordField.getPassword() method

 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reading my post.

I�m using javax.swing.JPasswordField to read a user entered password. The extracted password (via the getPassword() method) is ued as a seed to create a SecureKey which will be used for encrypting some file contents.

I encrypt the original file contents by hard coding the password on a test program. I use the same set of classes with the Swing GUI too. But when I obtain the password from the JPasswordField object, my cipher instance is throwing the exception: "javax.crypto.BadPaddingException: Given final block not properly padded".

When I hardcode the password in the GUI code I am able to successfully de-cipher the file. I did print the password obtained from the JPasswordField to console and it prints correctly.

I undertand that javax.crypto.spec.SecureKeySpec is considering byte values for the seed and if there is a difference in this the key would be different.

Is there any specific encoding or something I should use to convert the JPasswordField.getPassword() output to a String?

Currently I'm using the String constructor:

String(char[] chars)

to convert the output of the JPasswordField.getPassword().
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

In my opinion there's no need to convert anything because an array of unicode characters stay unicode in the String object. The conversion should follow when getting a byte-array out of the String with #getBytes(). You can use the method with the charset-name as parameter to use a specific encoding (such as "UTF-8" or something). Hmmm maybe you souldn't use a String object at all. Look at this code snippet:

 
Brian Cole
Author
Ranch Hand
Posts: 924
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Isuru Sampath:

When I hardcode the password in the GUI code I am able to successfully de-cipher the file. I did print the password obtained from the JPasswordField to console and it prints correctly.

Currently I'm using the String constructor:

String(char[] chars)

to convert the output of the JPasswordField.getPassword().


Is the constructed String equal to [in the sense of equals()] the hard-coded String?

If so, then this is not a Swing problem. If not, then it should be possible to figure out how the difference crept in.
[ November 23, 2006: Message edited by: Brian Cole ]
 
Maybe he went home and went to bed. And took this tiny ad with him:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!