gpt4 book ai didi

java - 我无法登录,因为我在密码字段中使用了 aes_encrypt

转载 作者:行者123 更新时间:2023-11-29 22:05:18 24 4
gpt4 key购买 nike

我正在使用 NetBeans IDE 练习使用 Java 和 MySQL 进行编程。我可以使用下面的代码登录我的应用程序。但是如果我使用 MySQL 的 aes_encrypt 功能加密我的密码,我不知道如何解密它。我知道有 aes_decrypt 但我在语法上遇到了困难。

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
if(lblUsername.getText().length()==0) // Checking for empty field
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else if(password.getPassword().length==0) // Checking for empty field
JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else{
String user = lblUsername.getText(); // Collecting the input
char[] pass = password.getPassword();
String pwd = String.copyValueOf(pass); // converting from array to string
if(validate_login(user,pwd)){
JOptionPane.showMessageDialog(null, "Correct Login Credentials");
MainStudentRecord mainstudentrecord = new MainStudentRecord();
mainstudentrecord.setVisible(true);
this.dispose();
}
else{
JOptionPane.showMessageDialog(null, "Incorrect Login Credentials");
lblUsername.setText("");
password.setText("");
}
}


}

private boolean validate_login(String username,String password) {
try{
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentrecord","neil","basabe");
PreparedStatement pst = conn.prepareStatement("Select * from user where username=? and password=?");
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
return true;
else
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}

这是我的表定义:

username varchar(16) 
password varchar(16)

至:

username varchar(16)
password blob --------- this is for me to use aes_encrypt

我知道这是我应该编辑的部分:

PreparedStatement pst = conn.prepareStatement("Select * from user where username=? and password=?");

至:

PreparedStatement pst = conn.prepareStatement("Select * from user where username=? and password=_______this is the confusing part_________");

请帮忙!

最佳答案

这里有多个问题。我将解决您首先确定的问题:

您已通过某种方式转换密码以将其存储在数据库中。对它执行相同的转换以确定它是否存储在那里,而不是尝试对已存储的数据反转转换。

您使用的转换是可逆加密。您应该存储密码(加盐)密码的不可逆哈希(和盐)。考虑到时间和空间,这是一个计算成本较高的操作,这是一个好主意。使用 scrypt 之类的工具生成密码哈希值有助于满足该要求。

如何存储密码是一个我不会进一步讨论的话题。但是,如果可以根本不存储密码数据并使用第三方(想想所有那些允许您使用 google/facebook/twitter/etc 登录的网站......就像这个一样),请这样做,他们比您更有可能获得正确的安全密码存储。

关于java - 我无法登录,因为我在密码字段中使用了 aes_encrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com