Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: How to convert password to md5?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to convert password to md5?

    I want to convert the input in a jpasswordfield to md5 but the problem is I don't know how


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    It's not difficult, you just need the magic incantation in Java.

    Let me google that for you

    MessageDigest is what you need. It looks a bit daunting at first, but most times you use it it'll be very little code: just a line or two.

  3. #3
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: How to convert password to md5?

    Hello A4Andy,

    I am going to assume you know how to get the value in the JPassowrdField already. As for converting that to md5, check out Message Digest.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert password to md5?

    too bad I don't know how to get the value of JPasswordField using JButton as the trigger. I already knew converting from String to md5 but not from JPasswordField to md5.

    I tried this code but it didn't work
    	public class HandlerClass1 implements ActionListener { 
    		public void actionPerformed(ActionEvent event)
    		{
     
    			char[] pass = pfPword.getPassword();
    			String s= new String(pass);
    		    MessageDigest m;
    			try {
    				m = MessageDigest.getInstance("MD5");
    				m.update(s.getBytes(),0,s.length());
    				System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
    			} catch (NoSuchAlgorithmException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
    		}
    	}

    I'm getting a null pointer exception

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    I'm getting a null pointer exception
    Copy and paste it. You've read the API doc for getPasswordField and seen that it can throw a NPE?

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert password to md5?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at db$HandlerClass1.actionPerformed(db.java:100)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    	at java.awt.Component.processMouseEvent(Component.java:6203)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:5968)
    	at java.awt.Container.processEvent(Container.java:2105)
    	at java.awt.Component.dispatchEventImpl(Component.java:4564)
    	at java.awt.Container.dispatchEventImpl(Container.java:2163)
    	at java.awt.Component.dispatchEvent(Component.java:4390)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    	at java.awt.Container.dispatchEventImpl(Container.java:2149)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4390)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    	at java.awt.EventQueue.access$000(EventQueue.java:96)
    	at java.awt.EventQueue$1.run(EventQueue.java:608)
    	at java.awt.EventQueue$1.run(EventQueue.java:606)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    	at java.awt.EventQueue$2.run(EventQueue.java:622)
    	at java.awt.EventQueue$2.run(EventQueue.java:620)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

    I input test in the passwordfield
    Last edited by A4Andy; September 12th, 2011 at 06:21 AM.

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    There's not much in there that could be throwing an NPE. Which is line 100 in db.java? If you've edited the file since you posted the stack trace, make sure you select the line on which the Exception is thrown in your latest source.

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert password to md5?

    public class HandlerClass1 implements ActionListener { 
    		public void actionPerformed(ActionEvent event)
    		{
     
    		100	char[] pass = pfPword.getPassword();
    		101	String s= new String(pass);
    		102   MessageDigest m;
    		103	try {
    		104		m = MessageDigest.getInstance("MD5");
    		105		m.update(s.getBytes(),0,s.length());
    		106		System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
    		107	} catch (NoSuchAlgorithmException e) {
    		108		// TODO Auto-generated catch block
    		109		e.printStackTrace();
    		110	}
     
    		}
    	}

  9. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    Why is pfPword null? Isn't that your password control? I'm guessing it's not the NPE that the API doc warns about in JPasswordField.getPassword() - or the stack trace should start inside that method. Printing the value of pfPword before line 100 would show for sure.
    Last edited by Sean4u; September 12th, 2011 at 07:37 AM. Reason: type faster than think

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert password to md5?

    Here is my whole db class

    import java.awt.*;
    import java.awt.event.*;
    import java.security.*;
    import java.math.*;
     
    import javax.swing.*;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.PlainDocument;
     
    public class db extends JPanel {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JLabel lbldbURL,lbldbName,lbldbUName,lbldbPword;
    	private JTextField txtdbURL,txtdbName,txtdbUname;
    	private JPasswordField pfPword;
    	private JButton saveButton;
     
    	public db() 
    	{
    		this.setBackground(Color.WHITE);
    		setLayout(null);
    		Font serif = new Font("serif",Font.PLAIN,14);
    		////////////
    		//Labels
    		////////////
    		//
    		//Database URL
    		JLabel lbldbURL = new JLabel("Database URL:");
    		lbldbURL.setBounds(290, 110, 170, 20);
    		lbldbURL.setFont(serif);
    		add(lbldbURL);		
    		//
    		//Database Name
    		JLabel lbldbName = new JLabel("Database Name:");
    		lbldbName.setFont(serif);
    		lbldbName.setBounds(280, 150, 170, 20);
    		add(lbldbName);
    		//
    		//Database User name
    		JLabel lbldbUName = new JLabel("Database Username:");
    		lbldbUName.setFont(serif);
    		lbldbUName.setBounds(250, 190, 170, 20);
    		add(lbldbUName);
    		//
    		//Database Password
    		JLabel lbldbPword = new JLabel("Database Password:");
    		lbldbPword.setFont(serif);
    		lbldbPword.setBounds(255, 230, 170, 20);
    		add(lbldbPword);
    		////////////////
    		//text field
    		///////////////
    		//
    		//text database URL
    		JTextField txtdbURL  = new JTextField();
    		txtdbURL.setFont(serif);
    		txtdbURL.setBounds(410, 110, 170, 20);
    		add(txtdbURL);
    		//
    		//text database name 
    		JTextField txtdbName  = new JTextField();
    		txtdbName.setFont(serif);
    		txtdbName.setBounds(410, 150, 170, 20);
    		add(txtdbName);
    		//
    		//Database User textfield
    		JTextField txtdbUName = new JTextField();
    		txtdbUName.setFont(serif);
    		txtdbUName.setBounds(410, 190, 170, 20);
    		add(txtdbUName);
    		//
    		//password field database password 
    		JPasswordField pfPword  = new JPasswordField();
    		pfPword.setFont(serif);
    		pfPword.setBounds(410, 230, 170, 20);
    		add(pfPword);
    		//
    		//Save Button
    		JButton saveButton  = new JButton("Save");
    		saveButton.setBounds(410, 260, 170, 20);
    		add(saveButton);
    		////////////
    		//Action Listener
    		////////////
    		HandlerClass1 handler1 = new HandlerClass1();
    		saveButton.addActionListener(handler1);
    	}
    	static class AL implements ActionListener{
    		public void actionPerformed(ActionEvent event)
    		{			
    		}
    	}
    	public class HandlerClass1 implements ActionListener  
    	{ 
    		public void actionPerformed(ActionEvent event) 
    		{
    			char[] pass = pfPword.getPassword();
    			String s= new String(pass);
    			MessageDigest m;
    			try 
    			{
    				m = MessageDigest.getInstance("MD5");
    				m.update(s.getBytes(),0,s.length());
    				System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
    			} 
    			catch (NoSuchAlgorithmException e) 
    			{
    				e.printStackTrace();
    			}
    		}
    	}	
    }

  11. #11
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    Heheh - there's another thread on the forum today about this. How many times have you declared pfPword?

  12. #12
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert password to md5?

    what do you mean I think only once.

  13. #13
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    search your text file for "JPasswordField". It should be in there only twice - once when you declare the reference pfPword and once when you use the JPasswordField constructor. I count more than two in your 'whole db class' post:
    http://www.javaprogrammingforums.com...html#post42561

  14. #14
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert password to md5?

    it's now working well

    import java.awt.*;
    import java.awt.event.*;
    import java.security.*;
    import java.math.*;
     
    import javax.swing.*;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.PlainDocument;
     
    public class db extends JPanel {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JLabel lbldbURL,lbldbName,lbldbUName,lbldbPword;
    	private JTextField txtdbURL,txtdbName,txtdbUname;
    [COLOR="#FF0000"]	private JPasswordField pfPword = new JPasswordField();[/COLOR]
    	private JButton saveButton;
     
    	public db() 
    	{
    		this.setBackground(Color.WHITE);
    		setLayout(null);
    		Font serif = new Font("serif",Font.PLAIN,14);
    		////////////
    		//Labels
    		////////////
    		//
    		//Database URL
    		JLabel lbldbURL = new JLabel("Database URL:");
    		lbldbURL.setBounds(290, 110, 170, 20);
    		lbldbURL.setFont(serif);
    		add(lbldbURL);		
    		//
    		//Database Name
    		JLabel lbldbName = new JLabel("Database Name:");
    		lbldbName.setFont(serif);
    		lbldbName.setBounds(280, 150, 170, 20);
    		add(lbldbName);
    		//
    		//Database User name
    		JLabel lbldbUName = new JLabel("Database Username:");
    		lbldbUName.setFont(serif);
    		lbldbUName.setBounds(250, 190, 170, 20);
    		add(lbldbUName);
    		//
    		//Database Password
    		JLabel lbldbPword = new JLabel("Database Password:");
    		lbldbPword.setFont(serif);
    		lbldbPword.setBounds(255, 230, 170, 20);
    		add(lbldbPword);
    		////////////////
    		//text field
    		///////////////
    		//
    		//text database URL
    		JTextField txtdbURL  = new JTextField();
    		txtdbURL.setFont(serif);
    		txtdbURL.setBounds(410, 110, 170, 20);
    		add(txtdbURL);
    		//
    		//text database name 
    		JTextField txtdbName  = new JTextField();
    		txtdbName.setFont(serif);
    		txtdbName.setBounds(410, 150, 170, 20);
    		add(txtdbName);
    		//
    		//Database User textfield
    		JTextField txtdbUName = new JTextField();
    		txtdbUName.setFont(serif);
    		txtdbUName.setBounds(410, 190, 170, 20);
    		add(txtdbUName);
    		//
    		//password field database password 
    		pfPword.setBounds(410, 230, 170, 20);
    		add(pfPword);
    		//
    		//Save Button
    		JButton saveButton  = new JButton("Save");
    		saveButton.setBounds(410, 260, 170, 20);
    		add(saveButton);
    		////////////
    		//Action Listener
    		////////////
    		HandlerClass1 handler1 = new HandlerClass1();
    		saveButton.addActionListener(handler1);
    	}
    	static class AL implements ActionListener{
    		public void actionPerformed(ActionEvent event)
    		{			
    		}
    	}
    	public class HandlerClass1 implements ActionListener  
    	{ 
    		public void actionPerformed(ActionEvent event) 
    		{
    			char[] pass = pfPword.getPassword();
    			String s= new String(pass);
    			MessageDigest m;
    			try 
    			{
    				m = MessageDigest.getInstance("MD5");
    				m.update(s.getBytes(),0,s.length());
    				System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
    			} 
    			catch (NoSuchAlgorithmException e) 
    			{
    				e.printStackTrace();
    			}
    		}
    	}	
    }

    My code before was actually a bad practice?

  15. #15
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: How to convert password to md5?

    Glad you got it working. It's not so much 'bad practice' as just wrong. You declared two references to JPasswordField, only one of which you assigned to a JPasswordField object. The other one's value is by default null. It's quite a common source of error - the other guys were talking about it here yesterday as 'shadowing' - more than one variable with the same name. The two variables may have completely different scope (where they can be 'seen' from in the code), or may even overlap. The compiler decides which one you mean (usually the most *local* one), but you may have last assigned to the other one ... boom.

    Not always easy to spot when you've done it either. If you find yourself thinking "that's not null, I just set it there...", it's worth checking to see if you've declared the reference more than once.

Similar Threads

  1. Help with GUI Password Tester
    By java.kid21 in forum AWT / Java Swing
    Replies: 10
    Last Post: December 13th, 2010, 08:06 AM
  2. Using a password field
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 11:54 AM
  3. [SOLVED] password denied
    By chronoz13 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 28th, 2010, 09:22 PM
  4. [SOLVED] Password screens
    By Dave in forum AWT / Java Swing
    Replies: 7
    Last Post: August 26th, 2009, 06:37 AM
  5. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM