MD5 encryption in Java?

Asked

Viewed 1,571 times

0

I’m creating a program that takes a login and a password and encrypts it using MD5,I know that MD5 is not an algorithm that you can call encryption,but is actually a hash,but it does not matter, what I am trying to do is what has already been mentioned and the variable login with MD5 works normal, but the variable password always returns the same result.

Variables :

String login;
String password;
MessageDigest m; 
MessageDigest m1;

Encryption process for MD5 :

try 
{ 
m = MessageDigest.getInstance("MD5"); 
m1 = MessageDigest.getInstance("MD5");
m.update(login.getBytes(),0,login.length()); 
m1.update(password.getBytes(),0,password.length());
BigInteger login1 = new BigInteger(1, m.digest()); 
BigInteger password1 = new BigInteger(1, m.digest());


//Formatando o resultado em uma cadeia de 32 caracteres, completando com 0 caso falte 
login = String.format("%1$032X", login1); 
password = String.format("%1$032X", password1); 

System.out.println("MD5: "+ login); 
System.out.println("MD5: " + password);
} 

In the output :

login : 011DD1032ECECFB4497613E48049972C
password : D41D8CD98F00B204E9800998ECF8427E

In the output of the password always ends up being the same hash, and in the login always end up leaving a different result(a different hash),I wonder how I can fix it to always exit a different result as well as the variable login.

1 answer

1


Good people managed to solve my problem,and I decided to post here in case someone wants to use the code as example or even to re-use it,what was missing was to reset the Messagedigest so later he can hash the password.

Here’s the code that’s neat :

try
{
    m = MessageDigest.getInstance("MD5");
    m.update(login.getBytes(), 0, login.length());
    BigInteger login1 = new BigInteger(1, m.digest());
    login = String.format("%1$032X", login1);

    m.reset(); // <---- Reseta antes de fazer o password
    m.update(password.getBytes(), 0, password.length());
    BigInteger password1 = new BigInteger(1, m.digest());
    password = String.format("%1$032X", password1);

    System.out.println(login);
    System.out.println(password);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.