2011-03-15 2 views

Répondre

0

Essayez quelque chose comme ceci pour crypter vos données.

MessageDigest md = MessageDigest.getInstance("MD5"); 


...... 


synchronized (md) { 

md.reset(); 
byte[] hash = md.digest(plainTextPassword.getBytes("CP1252")); 

StringBuffer sb = new StringBuffer(); 
for (int i = 0; i < hash.length; ++i) { 
sb.append(Integer.toHexString((hash[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3)); 
} 

String password = sb.toString(); 
} 
+0

-1 pour l'utilisation de la fonction de hachage rapide. Voir http://security.stackexchange.com/a/242/5501 –

-1

Vous pouvez également utiliser quelque chose comme ci-dessous. Voici une méthode crypt qui prend une entrée de chaîne et retournera une chaîne cryptée. Vous pouvez passer un mot de passe à cette méthode.

public static String crypt(String str) { 
    if (str == null || str.length() == 0) { 
     throw new IllegalArgumentException(
       "String to encrypt cannot be null or zero length"); 
    } 

    StringBuffer hexString = new StringBuffer(); 

    try { 
     MessageDigest md = MessageDigest.getInstance("MD5"); 
     md.update(str.getBytes()); 
     byte[] hash = md.digest(); 

     for (int i = 0; i < hash.length; i++) { 
      if ((0xff & hash[i]) < 0x10) { 
       hexString.append("0" 
         + Integer.toHexString((0xFF & hash[i]))); 
      } else { 
       hexString.append(Integer.toHexString(0xFF & hash[i])); 
      } 
     } 
    } catch (NoSuchAlgorithmException e) { 

    } 

    return hexString.toString(); 
} 
+0

-1 pour l'utilisation de la fonction de hachage rapide. Voir http://security.stackexchange.com/a/242/5501 –

+0

@Andrey Botalov. Ok point valide. Merci pour le lien utile. – ashishjmeshram

8

Les algorithmes auto-écrits présentent un risque de sécurité et sont difficiles à maintenir. Le numéro MD5 est not secure.

Utilisez l'algorithme bcrypt, fourni par jBcrypt (open source):

// Hash a password 
String hashed = BCrypt.hashpw(password, BCrypt.gensalt()); 

// Check that an unencrypted password matches or not 
if (BCrypt.checkpw(candidate, hashed)) 
    System.out.println("It matches"); 
else 
    System.out.println("It does not match"); 

Si vous utilisez Maven, vous pouvez obtenir la bibliothèque en insérant la dépendance suivante dans votre pom.xml (si une version plus récente est disponible s'il vous plaît faites le moi savoir):

<dependency> 
    <groupId>de.svenkubiak</groupId> 
    <artifactId>jBCrypt</artifactId> 
    <version>0.4.1</version> 
</dependency> 
+0

Comment l'utiliser dans mon code –

+0

@VedPrakash: J'ai ajouté un paragraphe pour l'obtenir de Maven, est-ce que cela aide? –

+0

@Nicolass Raoul Merci monsieur –

Questions connexes