Encryption with MD5 in Java?

0
I am creating a program that takes a login and a password and encrypts it using MD5, I know that MD5 is not an algorithm that can be called that it encrypts, but in fact it is a hash, a hash is an algorithm that maps variable length data for fixed length data, but this does not matter, what I am trying to do is what has already been quoted and the login variable with MD5 works fine, but the password variable 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);
} 

No output:

login : 011DD1032ECECFB4497613E48049972C
password : D41D8CD98F00B204E9800998ECF8427E

In the output of the password always ends up being the same hash, and in the login always comes out a different result (a different hash), I would like to know how I can arrange this to always get a different result like the login variable.     

asked by anonymous 22.05.2016 / 04:08

1 answer

1

Well, I was able to solve my problem, and I decided to post here if someone wanted to use the code as an example or even to reuse it, what was missing was to reset the MessageDigest so that it can be hashed in the password. p>

Here is the code that is 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);
}
    
22.05.2016 / 22:18