[Solved] Simple password encryption – how do i do? [closed]


In future, I’d suggest you refrain from begging for answers without first showing some code you’ve tried.

That being said, I’ll bite.

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

public class EncryptHelper
{
    public static String ehashAndSalt(String passedpass) throws NoSuchAlgorithmException, NoSuchProviderException
    {
        String passwordToHash = "password";
        String salt = getSalt();

        String securePassword = getSecurePassword(passwordToHash, salt);

        return securePassword;
    }

    private static String getSecurePassword(String passwordToHash, String salt)
    {
        String generatedPassword = null;
        try
        {
            // Create MessageDigest instance for MD5
            MessageDigest md = MessageDigest.getInstance("MD5");
            //Add password bytes to digest
            md.update(salt.getBytes());
            //Get the hash's bytes
            byte[] bytes = md.digest(passwordToHash.getBytes());
            //This bytes[] has bytes in decimal format;
            //Convert it to hexadecimal format
            StringBuilder sb = new StringBuilder();
            for(int i=0; i< bytes.length ;i++)
            {
                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            //Get complete hashed password in hex format
            generatedPassword = sb.toString();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        return generatedPassword;
    }

    //Add salt
    private static String getSalt() throws NoSuchAlgorithmException, NoSuchProviderException
    {
        //Always use a SecureRandom generator
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
        //Create array for salt
        byte[] salt = new byte[16];
        //Get a random salt
        sr.nextBytes(salt);
        //return salt
        return salt.toString();
    }
}

Here’s a nice and simple helper class for a hash/salt function. Just be sure to use the same “salt” string created when the user was created for when you authenticate the user, otherwise the authentication will fail.

Where passwords are concerned, I find it safer to use a hash/salt function rather than encryption, as encryption can be broken with the correct public/private key.

You can find more information on Java’s Native encryption Here.


EDIT

As @james large pointed out, You should randomise the salt. I’ve amended the code to show this.

Source of the above example: HowToDoInJava

I would then suggest you pass the salt and encrypted password to the database when creating new users, and then getting a resultset containing the salt and password and feeding it into a similar method to getSecurePassword() and using the outcome of this as a validation.

I hope this helps!

Edit – 2

Insert another row into your table called “salt” (or whatever you like), and insert a new user with a PreparedStatement, like so:

PreparedStatement pstmnt  = connection.prepareStatement
("insert into Usernames(`ID`,`Username`,`Password`,`Account type`, `salt`) values (?,?,?,?,?,)");
pstmnt.setInt(1, id); //would ideally be auto-incremented
pstmnt.setString(2, user); //user String obtained by any means
pstmnt.setString(3, securePassword); //from the hash/salt example above
pstmnt.setString(4, accType); //whatever naming structure you have for account types
pstmnt.setString(5, salt); //from the above example also.
pstmnt.executeUpdate();

5

solved Simple password encryption – how do i do? [closed]