dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] Password Encryption and store in database

  1. #1
    Join Date
    Oct 2019
    Posts
    9

    [RESOLVED] Password Encryption and store in database

    HI,
    I have created a simple login page with basic requirements with the following example https://krazytech.com/programs/a-log...design-pattern. I have stored the registered user details in database. As of now I have stored their password in plain text. But I want to encrypt the password and then store it in database. I am using java and jsp for views.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: Password Encryption and store in database

    Search google for "java encrypt decrypt".

  3. #3
    Join Date
    Oct 2019
    Posts
    9

    Re: Password Encryption and store in database

    I followed the post [stackoverflow.com/questions/47537908/…. But when I tried login with the password i am getting Invalid user credentials,as the password entered in db looks like SHA2(CONCAT('gayuranimini', 78622100), 256). I tried entering 78622100 alone in db and when tried login with gayuranimini i am getting Invalid user credentials error. when I tried entering with 78622100 the login was successful. Please tell me where I am wrong.

    When registering i want the encrypted password to insert into database. And when login i want the original password.

    userNameDB = resultSet.getString("userName"); //fetch the values present in database
    passwordDB = resultSet.getString("password");

    if(userName.equals(userNameDB) && password.equals(passwordDB))
    {
    return "SUCCESS"; ////If the user entered values are already present in database, which means user has already registered so I will return SUCCESS message.
    }
    The above code is for login and there entering the password checks the database. So the password doesn't get match. How to match the password?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: Password Encryption and store in database

    Here is what you need to do:
    1) encrypt the incoming password
    2) store the username and encrypted password into the database

    When the user logs on,...
    3) Encrypt the passed-in password
    4) Use the passed-in username to retrieve the user record from the database
    5) Compare the encrypted passed from step 3) with the encrypted password value in the user record.
    6) If the values match, they are logged on

    Note: you must use the same algorithm/seed value for the encrypt/decrypt operations.

  5. #5
    Join Date
    Oct 2019
    Posts
    9

    Re: Password Encryption and store in database

    I am using the following code for encryption at my registration side.
    MessageDigest alg = MessageDigest.getInstance("MD5");
    alg.reset();
    alg.update(password.getBytes());
    byte[] digest = alg.digest();
    StringBuffer hashedpasswd = new StringBuffer();
    String hx;
    for (int i=0;i<digest.length;i++){
    hx = Integer.toHexString(0xFF & digest[i]);
    //0x03 is equal to 0x3, but we need 0x03 for our md5sum
    if(hx.length() == 1){hx = "0" + hx;}
    hashedpasswd.append(hx);
    }
    How will I decrypt in the login side? Please help me.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: Password Encryption and store in database

    No need to decrypt. See step 5).

  7. #7
    Join Date
    Oct 2019
    Posts
    9

    Re: Password Encryption and store in database

    Thanks for your suggestion. Now it works fine. I just added the same code in login page and matches the password.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: Password Encryption and store in database

    Great.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)