-1
我有一個應用程序需要我有一個密碼終端。我正在使用JPassword
字段(JAVA)。現在我想要的是能夠以加密的形式將密碼保存在數據庫中,而我無法閱讀,並且當我檢索比較時,兩者應該是相同的,但是要保持不可讀。請我怎麼去這個,因爲目前,使用jpassword
,我轉換它,當我保存它被看到。當我保存而不轉換時,我無法比較,因爲值不同。我希望能夠比較數據庫和GUI
的密碼,而不在數據庫中顯示密碼。請幫忙。將密碼保存到德比或sql數據庫
下面是我的代碼:
package trackme;
/**
*
* @author kels
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.Arrays;
/**
*
* @author kels
*/
public class Login extends JFrame implements ActionListener, ItemListener{
DBOperator login;
Statement stat;
ResultSet rs;
Connection con;
String comparestring="Registration"; int getint=4;
java.sql.Time t = new java.sql.Time(new java.util.Date().getTime());
java.sql.Date dt = new java.sql.Date(new java.util.Date().getTime());
private String dbtxt = null, getpassword, username=null, gettype=null;
//private String gettype="";
//name components
JLabel lbllogin = new JLabel("Password: ");
final JCheckBox cbshow = new JCheckBox("Show Password");
JPasswordField txtlogin = new JPasswordField(10);
JLabel lblusername = new JLabel("UserName:");
JTextField txtusername = new JTextField(10);
JButton btnlogin = new JButton("Login");
JLabel label = new JLabel("");
//create containers
JPanel loginpanel = new JPanel();
JPanel wrappanel = new JPanel();
JPanel finalpanel = new JPanel();
JPanel btnpanel=new JPanel();
JPanel showpanel = new JPanel();
JPanel usernamepanel = new JPanel();
@SuppressWarnings("LeakingThisInConstructor")
public Login(){
super("Login");
setResizable(false);
setLocation(250, 260);
setSize(250,170);
Container pane = getContentPane();
//set Layout Managers
GridLayout grid = new GridLayout(4,1);
BorderLayout bord = new BorderLayout();
//set the managers to the panels
wrappanel.setLayout(grid);
pane.setLayout(bord);
//add components
usernamepanel.add(lblusername);
usernamepanel.add(txtusername);
loginpanel.add(lbllogin);
loginpanel.add(txtlogin);
wrappanel.add(usernamepanel);
wrappanel.add(loginpanel);
finalpanel.add(wrappanel);
wrappanel.add(showpanel);
showpanel.add(cbshow);
btnpanel.add(btnlogin);
wrappanel.add(label);
pane.add(btnpanel, BorderLayout.SOUTH);
pane.add(finalpanel, BorderLayout.CENTER);
setContentPane(pane);
setVisible(true);
btnlogin.addActionListener(this);
cbshow.addItemListener(this);
txtlogin.setEchoChar('*');
}
@Override
public void actionPerformed(ActionEvent ae){
Object source = ae.getSource();
login = new DBOperator();
stat = login.getStatement();
// PreparedStatement preparedStatement;
String status = ""; String getstatus="Loggedout"; String getusername=""; String gettypedb="";
try{
if(source == btnlogin){
java.sql.Time dbtime = null; java.sql.Date dbdate = null;
char [] paswrdtxt, passchar;
paswrdtxt =txtlogin.getPassword();
getpassword= String.valueOf(paswrdtxt);
String getstatustrim="";
//making sure user in not logged in
rs=stat.executeQuery("SELECT USERNAMEDB,PASSWORD,TYPE FROM PWORD WHERE PASSWORD="
+ ""+"'"+getpassword+"'" +" AND USERNAMEDB="+"'"+txtusername.getText()+"'"+"");
while(rs.next()){
username=rs.getString("Usernamedb");
dbtxt=rs.getString("Password");
gettype=rs.getString("Type");
}
rs.close();
if(getpassword.equals(dbtxt) && txtusername.getText().equals(username)){
/*do insert to logindb here and check status first to know if logged in or out
if loggedin error cause u cant login in while logged in and u can logout when not logged in
*/
rs = stat.executeQuery("SELECT STATUS FROM LOGINDB WHERE LOGINDATE="+"'"+dt+"'"
+" AND USERNAME="+"'"+txtusername.getText()+"'"+"");
while(rs.next()){
status=rs.getString("Status");
}
rs.close();
}//end if comparison
}//end btnlogin
stat.close();
}//end try
catch(SQLException ex){
System.out.println(ex.toString() + "no deal");
}
}
public static void main(String[] args){
Login lm = new Login();
}
}
在此先感謝
我不知道什麼JPassword有問題的事,但任何關於「如何安全存儲密碼」教程應告訴你你需要什麼 –
相關:http://security.stackexchange.com/questions/88940/is-it-truly-possible-to-store-passwords-securely-but-能夠看到該密碼/ 88942 –
這也是很多代碼 - 你能簡化它到我們更好理解的最小需求嗎? – Krease