how can we store the encrypted passwaord in swings?

how can we store the encrypted passwaord in swings?

how can we store the encrypted passwaord in swings?

View Answers

June 18, 2012 at 1:03 PM

The given code accepts username and password from the user in order to save data into database. The password is first encrypted and then save into database.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.security.*;
import javax.crypto.*;

class Login extends JFrame implements ActionListener
{
    JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
    private static String algorithm = "DESede";
        private static Key key = null;
        private static Cipher cipher = null;
         private static byte[] encrypt(String input)throws Exception {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] inputBytes = input.getBytes();
            return cipher.doFinal(inputBytes);
        }
         private static String decrypt(byte[] encryptionBytes)throws Exception {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] recoveredBytes =  cipher.doFinal(encryptionBytes);
            String recovered =  new String(recoveredBytes);
            return recovered;
          }

  Login()
  {
  label1 = new JLabel();
  label1.setText("Username:");
  text1 = new JTextField(15);

  label2 = new JLabel();
  label2.setText("Password:");
  text2 = new JPasswordField(15);

  SUBMIT=new JButton("SUBMIT");

  panel=new JPanel(new GridLayout(3,1));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(SUBMIT);
  add(panel,BorderLayout.CENTER);
  SUBMIT.addActionListener(this);
  }
 public void actionPerformed(ActionEvent ae)
  {
     try{

String uname=text1.getText();
String pass=text2.getText();

 key = KeyGenerator.getInstance(algorithm).generateKey();
            cipher = Cipher.getInstance(algorithm);
            String input = pass;
            byte[] encryptionBytes = encrypt(input);
            String passw=new String(encryptionBytes);
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection con=null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionURL, "root", "root");
PreparedStatement ps = con.prepareStatement("INSERT INTO login(username,password) VALUES(?,?)");
ps.setString(1,uname);
ps.setString(2,passw);
int i = ps.executeUpdate();
ps.close();

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from login where username='"+uname+"'");
String str="";
if(rs.next()){
str=rs.getString("password");
}
JOptionPane.showMessageDialog(null,"Your password is decrypted successfully!");
}
catch(Exception e){}
}
  public static void main(String arg[])throws  Exception
  {
  try
  {
  Login frame=new Login();
  frame.setSize(300,100);
  frame.setVisible(true);
  }
  catch(Exception e)
  {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}









Related Tutorials/Questions & Answers:
how can we store the encrypted passwaord in swings?
How many cookie object we can store in a session? is there any limit?
Advertisements
How can we destroy the cookie?
how can i store text box values as it is in database table
How can we know that a session is started or not?
How can we register the variables into a session?
How can we repair a MySQL table?
How we can create a table through procedure ?
How can store image in server folder when deployed with a .war file?
how we can create website through java
How can we get hibernate statistics?
How can we save a data list in jsf?
How can we destroy the session, how can we unset the variable of a session?
How can we solve this puzzle using java ?
How can we submit a form without a submit button?
How can we increase the execution time of a php script?
How many ways we can give the output to a browser?
How can we encrypt the username and password using PHP?
How can we create a database using PHP and mysql?
how can we prevent back option after log out.
How can we know the count/number of elements of an array?
what is cloud computing? any how we can implement?
How to store image into database
How can we get second of the current time using date function?
How can we find the number of rows in a result set using PHP?
How many ways can we get the value of current session id?
how can we pass parameters from jsp href to another servlet
Can anybody help how to store values to the following xml from .NET application
How can we implement Pop-up in JQuery without Plugin?
ModuleNotFoundError: No module named 'tensorflow-encrypted'
ModuleNotFoundError: No module named 'encrypted-config'
ModuleNotFoundError: No module named 'encrypted-dns'
ModuleNotFoundError: No module named 'encrypted-stream'
ModuleNotFoundError: No module named 'tf-encrypted'
ModuleNotFoundError: No module named 'encrypted_bigquery'
ModuleNotFoundError: No module named 'encrypted-storage'
Maven dependency for com.pddstudio - encrypted-preferences version 1.0.1 is released. Learn to use encrypted-preferences version 1.0.1 in Maven based Java projects
What is the maximum size of a file that can be uploaded using PHP and how can we change this?
How to store an image in database
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for encrypted-preferences version 1.2.0
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for encrypted-preferences version 1.0.1
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for encrypted-preferences version 1.3.0
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for encrypted-preferences version 1.1.0
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for encrypted-preferences version 1.0.0
Java swing store the encrypted password into database
ModuleNotFoundError: No module named 'django-encrypted-cookie-session-py3'
ModuleNotFoundError: No module named 'django-encrypted-fields'
ModuleNotFoundError: No module named 'django-encrypted-filefield'
ModuleNotFoundError: No module named 'django-encrypted-id'
ModuleNotFoundError: No module named 'django-encrypted-model-fields'

Ads