我正在使用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();
}
}
你确定这是一个[最小,完整和可验证的例子](https://stackoverflow.com/help/mcve)? – Clijsters
如果'password'在数据库中被加密,那么你需要在你的查询中使用加密的'password'。 – juvenislux
@humphrey你会用一些算法来加密密码,在这种情况下你必须加密你的密码,然后你可以比较。 –