Results 1 to 2 of 2
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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?

  2. #2
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Lightbulb 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:
    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 ();
    	}	
    }
    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.
    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

  1. Enable/Disable tabs based on user
    By R S Reddy in forum New To Java
    Replies: 2
    Last Post: 08-30-2011, 08:57 AM
  2. Enable/Disable tabs based on user
    By R S Reddy in forum Advanced Java
    Replies: 1
    Last Post: 08-30-2011, 08:56 AM
  3. How to Enable and Disable JtextField on Selection of Checkbox
    By deshmukh.niraj04 in forum New To Java
    Replies: 1
    Last Post: 04-11-2011, 12:26 PM
  4. Please help : JSP enable/disable button
    By kalyana in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 05:06 PM
  5. use javascript to enable/disable hyperlinks
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 04-22-2010, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •