Java - MD5

 
Vista:

MD5

Publicado por ricardo (5 intervenciones) el 12/01/2009 21:57:47
hola amigos tengo un problema
tengo que encriptar las password de los usuarios
el paso de encriptacion esta listo el problema es la desencriptacion
no se como retornar la encriptacion que son numeros y letras raras
a una cadena de texto legible.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:MD5

Publicado por Danilo Juvinao (278 intervenciones) el 14/01/2009 21:08:02
Saludos!!.

Los password MD5 no se pueden desencriptar(en teoria), de ahi su eficiencia.

Lo que debes hacer para comparar si los password de tus usuarios estan bien es tomar la cadena digitada, convertirla a un hash md5 y compararla con el hash md5 que ya tienes guardado, esa es la forma; pero en teoria un hash md5 no puede ser desencriptado, claro està, todo es posible!.

Exitos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:MD5

Publicado por esfera (6 intervenciones) el 21/01/2009 18:42:33
Otra forma es utlizar las librerias de seguridad y criptograficas.. aca va un ejemplo que les comparto.

import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import sun.misc.BASE64Encoder;

public class Cifrador {

private static byte[] SALT_BYTES = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 };

private static int ITERATION_COUNT = 19;

public static String encriptar(String passPhrase, String str) {

Cipher ecipher = null;
Cipher dcipher = null;
try {
// Crear la key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT_BYTES, ITERATION_COUNT);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());

// Preparar los parametros para los ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(SALT_BYTES, ITERATION_COUNT);

// Crear los ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

// Encodear la cadena a bytes usando utf-8
byte[] utf8 = str.getBytes("UTF8");

// Encriptar
byte[] enc = ecipher.doFinal(utf8);

// Encodear bytes a base64 para obtener cadena
return new BASE64Encoder().encode(enc);

} catch (Exception e) {
return null;
}
}

public static String desencriptar(String passPhrase, String str) {
Cipher ecipher = null;
Cipher dcipher = null;

try {
// Crear la key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT_BYTES, ITERATION_COUNT);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());

// Preparar los parametros para los ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(SALT_BYTES, ITERATION_COUNT);

// Crear los ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

// Decodear base64 y obtener bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

// Desencriptar
byte[] utf8 = dcipher.doFinal(dec);

// Decodear usando utf-8
return new String(utf8, "UTF8");

} catch (Exception e) {
return null;
}
}

public static void main(String args[]) {
String enc = Cifrador.encriptar("clave", "texto");
String des = Cifrador.desencriptar("clave", "RF9CBZ6tsmc=");

System.out.println(enc);
System.out.println(des);
}

}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar