2017-10-12 185 views
0

我正在使用mysql数据库在Java中构建登录系统,数据库中的密码被加密。所以当我在数据库中手动输入纯文本密码时,系统会将我登录,但无法读取已加密的密码。我希望有人能帮助我。Java登录不读取加密密码

这里是我的尝试:

package techtight; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import static javax.management.Query.and; 
import javax.swing.JButton; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
public class myLogin extends JFrame { 
    public void contentMethod(){ 
    JFrame myframe = new JFrame("login Techtight"); 
    myframe.setLayout(null); 
    myframe.setSize(200,170); 

    myframe.setVisible(true); 

     JTextField txtuser = new JTextField(); 
      JPasswordField txtpass = new JPasswordField(); 
      JButton btnLoging = new JButton("Login"); 
      txtuser.setBounds(40, 20, 120, 25); 
      txtpass.setBounds(40, 50, 120, 25); 
      btnLoging.setBounds(40,80, 80, 30); 
      myframe.add(txtuser); 
      myframe.add(txtpass); 
      myframe.add(btnLoging); 
     ///How to code the button 
    btnLoging.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      // connect to the db 
      try { 
       Connection conn; 
       String dbuser = "root"; 
       String dbpassw = "12345"; 
       String databasename = "taxi"; 
       String url = "jdbc:mysql://localhost/taxi"; 
       Class.forName("com.mysql.jdbc.Driver"); 
       conn = DriverManager.getConnection(url,dbuser,dbpassw); 
       Statement st = conn.createStatement(); 
        String username = txtuser.getText(); 
        char[] pass = txtpass.getPassword(); 
        String password = new String (pass); 
        //String password = String.copyValueOf(pass); // converting from array to string 
        //String password = txtpass.getText(); 
        ResultSet rs = st.executeQuery("SELECT * FROM users where username='" + username + "' and password='" + password + "'"); 
        if(rs.next()){ 
         JOptionPane.showMessageDialog(null, "Login Succesful"); 
         myframe.dispose();// make login for disapear 

        }else { 
              JOptionPane.showMessageDialog(null, "Login Failed "); 
               new Techtight(); 


        } 
       //JOptionPane.showMessageDialog(null, "okay"); 

      } catch (SQLException ex) { 
       Logger.getLogger(myLogin.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (ClassNotFoundException ex) { 
       Logger.getLogger(myLogin.class.getName()).log(Level.SEVERE, null, ex); 
      }    
     } 
});   // 

    } 
    public static void main (String args[]){ 
     myLogin m = new myLogin(); 
     m.contentMethod(); 

    } 

} 
+2

你确定这是一个[最小,完整和可验证的例子](https://stackoverflow.com/help/mcve)? – Clijsters

+2

如果'password'在数据库中被加密,那么你需要在你的查询中使用加密的'password'。 – juvenislux

+2

@humphrey你会用一些算法来加密密码,在这种情况下你必须加密你的密码,然后你可以比较。 –

回答

0

在程序中你逝去的非加密密码。不知道你是如何在数据库加密密码,但你应该在这之后添加:

String password = new String (pass); 

A码负责加密线可以关注一下:

String encrypted_password = sha1_enc_method(password); // it could be another encryption 

,然后在普通使用加密的一个替代文字一个。

1

您必须为查询使用加密密码。

所以你必须使用相同的加密或散列函数用于存储的数据。

如果在数据库中的密码散列SHA1查询可能看起来像:

SELECT * FROM users WHERE username = user AND password = SHA1(password) 

你可以将它发送到数据库之前,还加密密码。只需使用加密函数的Java等价物即可。

+0

我使用md5和php来创建这些密码 – humphrey

+0

因此,只需在此查询中使用“MD5(...)” –