Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Not able to add my hash password into my database(mssql)

Not able to add my hash password into my database(mssql)

Scheduled Pinned Locked Moved Java
databasesql-servercryptographyquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    private static void insertRecord() throws SQLException
    {
    Connection con = null;
    Statement statement = null;
    try
    {
    PreparedStatement PS = Connectionstring().prepareStatement("INSERT INTO LoginDetails(Username,Password) VALUES (?,?)");
    String User = tf_Uname.getText();
    String _Pass = new String(tf_pass.getPassword());
    //===========================================================================================================================================================
    MessageDigest md = null;
    try {
    md = MessageDigest.getInstance("SHA-256");
    }
    catch (NoSuchAlgorithmException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    md.update(_Pass.getBytes());

      byte byteData\[\] = md.digest();
    
      //convert the byte to hex format method 1
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < byteData.length; i++) {
       sb.append(Integer.toString((byteData\[i\] & 0xff) + 0x100, 16).substring(1));
      }
      System.out.println("Hex format : " + sb.toString());
    
      //convert the byte to hex format method 2
      StringBuffer hexString = new StringBuffer();
    for (int i=0;i
    

    I'm getting the right output but could not able to save the hash password into the database it's being stored as a Text.

    P 2 Replies Last reply
    0
    • C chdboy

      private static void insertRecord() throws SQLException
      {
      Connection con = null;
      Statement statement = null;
      try
      {
      PreparedStatement PS = Connectionstring().prepareStatement("INSERT INTO LoginDetails(Username,Password) VALUES (?,?)");
      String User = tf_Uname.getText();
      String _Pass = new String(tf_pass.getPassword());
      //===========================================================================================================================================================
      MessageDigest md = null;
      try {
      md = MessageDigest.getInstance("SHA-256");
      }
      catch (NoSuchAlgorithmException e)
      {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      md.update(_Pass.getBytes());

        byte byteData\[\] = md.digest();
      
        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
         sb.append(Integer.toString((byteData\[i\] & 0xff) + 0x100, 16).substring(1));
        }
        System.out.println("Hex format : " + sb.toString());
      
        //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
      for (int i=0;i
      

      I'm getting the right output but could not able to save the hash password into the database it's being stored as a Text.

      P Offline
      P Offline
      pa1joc
      wrote on last edited by
      #2

      I think you provide normal username and password to ps you need to provide hex code

      1 Reply Last reply
      0
      • C chdboy

        private static void insertRecord() throws SQLException
        {
        Connection con = null;
        Statement statement = null;
        try
        {
        PreparedStatement PS = Connectionstring().prepareStatement("INSERT INTO LoginDetails(Username,Password) VALUES (?,?)");
        String User = tf_Uname.getText();
        String _Pass = new String(tf_pass.getPassword());
        //===========================================================================================================================================================
        MessageDigest md = null;
        try {
        md = MessageDigest.getInstance("SHA-256");
        }
        catch (NoSuchAlgorithmException e)
        {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        md.update(_Pass.getBytes());

          byte byteData\[\] = md.digest();
        
          //convert the byte to hex format method 1
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i < byteData.length; i++) {
           sb.append(Integer.toString((byteData\[i\] & 0xff) + 0x100, 16).substring(1));
          }
          System.out.println("Hex format : " + sb.toString());
        
          //convert the byte to hex format method 2
          StringBuffer hexString = new StringBuffer();
        for (int i=0;i
        

        I'm getting the right output but could not able to save the hash password into the database it's being stored as a Text.

        P Offline
        P Offline
        pa1joc
        wrote on last edited by
        #3

        Hello chdboy please check this line

        PS.setString(1, User);
        PS.setString(2, _Pass);
        PS.executeUpdate();

        you have provided normal text to user and password field While you need to provide hex code in this line no 1 and 2

        C 1 Reply Last reply
        0
        • P pa1joc

          Hello chdboy please check this line

          PS.setString(1, User);
          PS.setString(2, _Pass);
          PS.executeUpdate();

          you have provided normal text to user and password field While you need to provide hex code in this line no 1 and 2

          C Offline
          C Offline
          chdboy
          wrote on last edited by
          #4

          Thanks a lot ...now let's see if there is a problem in getting hex code back to the string when user enter details .:)

          L 1 Reply Last reply
          0
          • C chdboy

            Thanks a lot ...now let's see if there is a problem in getting hex code back to the string when user enter details .:)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You shouldn't need to, that means anyone else can, and your system is not secure. Passwords should use one-way hashes so they can never be converted back to the original string. See Secure Password Authentication Explained Simply[^].

            Use the best guess

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups