Results 1 to 2 of 2
- 05-23-2012, 03:12 AM #1
Enable or disable encryption in a JPasswordField
Setting echo char to 'u\0000' will disable masking on a JPasswordField, but it still forbid copy/cut operations.
This bug(or whatever you call it) do not exist on java.awt.TextField, but the methods of returning password is very unsafe on that class.
I have tried many solutions, such as a adding a JTextField and JPasswordField in a JLayedPane. Did not worked.
I tried writing my own password field class by extending JTextField, but its to many bugs.
Also tried a CardLayer, couldnt figure out how to use it.
What options do I have?
- 05-26-2012, 09:35 PM #2
Re: Enable or disable encryption in a JPasswordField
Alright, I am progressing on my own class. I have two bugs I need help with.
Please try it out:
First bug: The last entered character will not be masked. So, if you enter abc, it will look like this: **c. If you now write/append 'd', it will look like this ***d.Java Code:import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.UIManager; @SuppressWarnings("serial") public final class PasswordBar extends JTextField { public static void main(String[] args) throws Exception { JFrame f = new JFrame (); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300,300); PasswordBar pw =new PasswordBar (); pw.enableMasking(); f.add(pw); f.setVisible(true); } private transient StringBuilder password; protected char echoChar; protected boolean echoEnabled; public PasswordBar () { super (); } public PasswordBar (javax.swing.text.Document doc, String text, int columns) { super (doc, text, columns); } public PasswordBar (int columns) { super (columns); } public PasswordBar (String text) { super (text); } public PasswordBar (String text, int columns) { super (text, columns); } { password = new StringBuilder (); echoChar = '*'; echoEnabled = false; } public void setEchoChar (char echoChar) { this.echoChar = echoChar; } public void enableMasking () { echoEnabled = true; this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (!echoEnabled) return; char c = e.getKeyChar(); if (c == (char) 22)//Test is copied, ie a lot of characters System.exit(0);//TESTING if (c == '\n')//NewLine return; if (c == (char)8)//Backspace { try { password.deleteCharAt(password.length() - 1); System.out.println(password); } catch (Exception ex) {} return; } if (c != KeyEvent.CHAR_UNDEFINED && !e.isActionKey()) { password.append (c); int length = getText().length(); StringBuilder dummy = new StringBuilder(length); for (int i = 0; i < length; i++) dummy.append(echoChar); setText(dummy.toString()); System.out.println(password); } } }); } // @Override // public void copy () // { // if (echoEnabled) // UIManager.getLookAndFeel().provideErrorFeedback(this); // else // super.copy(); // } // // @Override // public void cut () // { // if (echoEnabled) // UIManager.getLookAndFeel().provideErrorFeedback(this); // else // super.cut(); // } // // @Override // public void paste () // { // if (echoEnabled) // UIManager.getLookAndFeel().provideErrorFeedback(this); // else // super.paste(); // } public void disableMasking () { echoEnabled = false; setText(password.toString()); clearPassword(); } public char[] getPassword () { char[] pw = new char[password.length()]; for (int i = 0; i < pw.length; i++) pw[i] = password.charAt(i); return pw; } public void clearPassword () { for (int i = 0; i < password.length(); i++) password.setCharAt(i, '0'); password = new StringBuilder (); } }
How to I resolve this?
The second and final bug, is when I am pasting text. First of all, I want to disable text pasting(you can see how I did on the commented-out methods), but it did not work. So I tried to make pasting work, but it does not work at all.
When a text is pasted, getKeyChar return char value 22, instead of a char array/string. How do I handle this? I would also like to know how I disable copy & paste from this box. Copying text masked text would not be a security issue in this class. Pasting could work too, but it seems to much of a nightmare to fix plus it is unsafe as user can forget what they type.
Similar Threads
-
Enable/Disable tabs based on user
By R S Reddy in forum New To JavaReplies: 2Last Post: 08-30-2011, 08:57 AM -
Enable/Disable tabs based on user
By R S Reddy in forum Advanced JavaReplies: 1Last Post: 08-30-2011, 08:56 AM -
How to Enable and Disable JtextField on Selection of Checkbox
By deshmukh.niraj04 in forum New To JavaReplies: 1Last Post: 04-11-2011, 12:26 PM -
Please help : JSP enable/disable button
By kalyana in forum New To JavaReplies: 3Last Post: 03-24-2011, 05:06 PM -
use javascript to enable/disable hyperlinks
By sauravsinha in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 04-22-2010, 06:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks