title: User Authentication in Java using 128-bit AES encryption in CBC mode with PKCS #5 padding
body: <p>I am using 128-bit AES encryption in CBC mode with PKCS #5 padding to save passwords to my database.
  However, when I try to log in, the system tells me the password is invalid even though I use the correct password.</p>
  
  <p>On my UserSetup class, these are my codes for encrypting the passwords and saving them to my database:</p>
  
  <pre><code>try {
      String input = simple_text.getText();
  
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  
      byte[] iv = new byte[cipher.getBlockSize()];
      new SecureRandom().nextBytes(iv);
      IvParameterSpec ivSpec = new IvParameterSpec(iv);
  
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      digest.update(keyString.getBytes());
      byte[] key = new byte[16];
      System.arraycopy(digest.digest(), 0, key, 0, key.length);
      SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
  
      // encrypt
      cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
      byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
      System.out.println("encrypted: " + new String(encrypted));
      encrypt_text .setText(new String(encrypted));
  } catch (Exception e2) {
      JOptionPane.showMessageDialog(null, e2);
  }
  
  try {
      String sql = "INSERT INTO user(username,password) VALUES(?,?) ON DUPLICATE KEY UPDATE username=VALUES(username),password=VALUES(password)";
  
      pst=conn.prepareStatement(sql);
  
      pst.setString(1, fLoginName.getText());
      pst.setString(2, encrypt_text.getText());
  
      pst.execute();
  
      JOptionPane.showMessageDialog(null, "saved");
  } catch (Exception e2) {
      JOptionPane.showMessageDialog(null, e2);
  }
  </code></pre>
  
  <p>On my Login Class, I have the following codes:</p>
  
  <pre><code> String sql ="select * from user where username=? and password=?";
   if(loginNameField.getText().equals("me") &amp;&amp; passwordField.getText().equals("me")){
      frmLoginWindow.dispose();
      new GridMain().setVisible(true);                            
   }else{
      try{
          pst=conn.prepareStatement(sql);
          pst.setString(1,loginNameField.getText());
          pst.setString(2,passwordField.getText());
  
          rs=pst.executeQuery();
  
          if(rs.next()){
              //JOptionPane.showMessageDialog(null, "Username and Password is correct ");
              rs.close();
              pst.close();
              //  close();
              frmLoginWindow.dispose();
              new GridMain().setVisible(true);
          }
          else{
           JOptionPane.showMessageDialog(null, "Username and Password is not correct");
          }
      }
      catch(Exception e)
      {
             JOptionPane.showMessageDialog(null, e);
      } finally {
          try{
            rs.close();
            pst.close();
          }
          catch(Exception e) {}
      }
  }
  </code></pre>
  
  <p>My encrypted passwords are saving successfully, but I need a way to match my login password with the encrypted passowrd, since AES encryption does not give the same encrypted value even if you encrypt the same password.</p>