IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Comment crypter decrypter un mot de passe


Sujet :

avec Java

  1. #1
    Membre averti Avatar de Philcmoi
    Homme Profil pro
    Inscrit en
    Juillet 2006
    Messages
    666
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 666
    Points : 412
    Points
    412
    Par défaut Comment crypter decrypter un mot de passe
    Bonjour, je n'arrive pas à comprendre le fonctionnement du cryptage decryptage.
    Voici mon code.

    ECRITURE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
     
    String sql2 = "INSERT INTO membre (idmembre,email,pseudo,token,password,role, verification) VALUES (NULL, ?, ?, ?, ?, 'visiteur', '0')";
    		 PreparedStatement statement2 = conn.prepareStatement(sql2);
    		 statement2.setString(1, request.getParameter("email"));
    		 statement2.setString(2, request.getParameter("pseudo"));
     
    		 int Max = 1000000; int Min = 0;
    		 Integer nbaleatoire = Min + (int)(Math.random() * ((Max - Min) + 1));
     
    		 String token = nbaleatoire.toString();
     
    		 statement2.setString(3, token);
     
     
    		 String motpasse= request.getParameter("password");
    		 String motpasse2= request.getParameter("password2");
     
    		 if (!motpasse.equals(motpasse2))
    		 {System.out.println("erreur");
    		    response.sendRedirect("enregistrement.jsp?erreure=3");
     
    		 }
    		 else {
     
     
    		  /*
    		  * Cryptage
    		  * 
    		  */
     
    		  TestCipherDESede Cryptage = new  TestCipherDESede();
    		  final String message = motpasse;
     
    		    KeyGenerator keyGen;
     
    		      keyGen = KeyGenerator.getInstance("DESede");
    		      keyGen.init(168);
    		      SecretKey cle = keyGen.generateKey();
    		      System.out.println("cle : " + new String(cle.getEncoded()));
     
    		      byte[] enc = Cryptage.encrypter(message, cle);
    		      System.out.println("texte encrypte : " + new String(enc));
     
     
     
    		  /*
    		   * fin cryptage
    		   */
     
    		    motpasse = new String(enc);
     
    		    statement2.setString(4, motpasse);
     
    			int rowsInserted = statement2.executeUpdate();
    			if (rowsInserted > 0) {
    			    System.out.println("A new user was inserted successfully!");
    		    response.sendRedirect("index.jsp?erreure=2#formulaire");
    			}
    			else {
    			    System.out.println("erreur");
    		    response.sendRedirect("enregistrement.jsp?erreure=3");
    			}
    		 }
    	}
    	catch (Exception e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}

    LECTURE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
     
    String sql = "SELECT email, idmembre, token, password , pseudo FROM membre WHERE email = ?";			 
    			PreparedStatement statement = conn.prepareStatement(sql);
    			statement.setString(1, request.getParameter("email"));
     
    			System.out.println("requette email" + request.getParameter("email"));
     
    			ResultSet rowsselected =  statement.executeQuery();
    			statement.getResultSet();
    			while (rowsselected.next())
    			{
    				System.out.println("REQUETTE select EFFECTUE");
    				String email = rowsselected.getString("email");
    				String motpasse = rowsselected.getString("password");
     
    				String passwordCrypte = rowsselected.getString("password");
     
    				/*
    				 * debut cryptage
    				 */
    				TestCipherDESede Cryptage = new  TestCipherDESede();
    				String message = passwordCrypte;
     
    			    KeyGenerator keyGen;
    			    try {
    			      keyGen = KeyGenerator.getInstance("DESede");
    			      keyGen.init(168);
    			      SecretKey cle = keyGen.generateKey();
    			      System.out.println("cle : " + new String(cle.getEncoded()));
     
    			      byte[] enc = TestCipherDESede.encrypter(message, cle);
    			      System.out.println("texte encrypte : " + new String(enc));
     
    			      String dec = TestCipherDESede.decrypter(enc, cle);
    			      System.out.println("texte decrypte : " + dec);
    			      message = dec;
     
    			    } catch (Exception e) {
    			      e.printStackTrace();
    			    }
     
    			    /*
    			     * fin cryptage
    			     */
     
    				if (motpasse.equals(message)) {
     
     
    			      response.sendRedirect("index.jsp");
    				}
     
    			}
     
    			    System.out.println("erreur");
    			    response.sendRedirect("index2.jsp?erreure=1");
     
     
    		} catch (Exception ex) {
    		    ex.printStackTrace();
    		}
     
     
    	}
    	}
    Le cryptogramme

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
     
    import java.security.InvalidKeyException;
    	import java.security.NoSuchAlgorithmException;
     
    	import javax.crypto.BadPaddingException;
    	import javax.crypto.Cipher;
    	import javax.crypto.IllegalBlockSizeException;
    	import javax.crypto.KeyGenerator;
    	import javax.crypto.NoSuchPaddingException;
    	import javax.crypto.SecretKey;
     
    	public class TestCipherDESede {
     
    	  public static void main(String[] args) {
     
    	    final String message = "Mon message a traiter";
     
    	    KeyGenerator keyGen;
    	    try {
    	      keyGen = KeyGenerator.getInstance("DESede");
    	      keyGen.init(168);
    	      SecretKey cle = keyGen.generateKey();
    	      System.out.println("cle : " + new String(cle.getEncoded()));
     
    	      byte[] enc = encrypter(message, cle);
    	      System.out.println("texte encrypte : " + new String(enc));
     
    	      String dec = decrypter(enc, cle);
    	      System.out.println("texte decrypte : " + dec);
     
    	    } catch (Exception e) {
    	      e.printStackTrace();
    	    }
    	  }
     
    	  public static byte[] encrypter(final String message, SecretKey cle)
    	      throws NoSuchAlgorithmException, NoSuchPaddingException,
    	      InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    	    Cipher cipher = Cipher.getInstance("DESede");
    	    cipher.init(Cipher.ENCRYPT_MODE, cle);
    	    byte[] donnees = message.getBytes();
     
    	    return cipher.doFinal(donnees);
    	  }
     
    	  public static String decrypter(final byte[] donnees, SecretKey cle)
    	      throws NoSuchAlgorithmException, NoSuchPaddingException,
    	      InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    	    Cipher cipher = Cipher.getInstance("DESede");
    	    cipher.init(Cipher.DECRYPT_MODE, cle);
     
    	    return new String(cipher.doFinal(donnees));
    	  }
     
    	}

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 559
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 559
    Points : 21 619
    Points
    21 619
    Par défaut
    Hello,

    mais encore ? Il y a un moment précis où tu ne comprends pas ce qui se passe ?

  3. #3
    Membre averti Avatar de Philcmoi
    Homme Profil pro
    Inscrit en
    Juillet 2006
    Messages
    666
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 666
    Points : 412
    Points
    412
    Par défaut
    Bonjour je ne sais pas décrypter le mot de passe sans d'abord l'encrypter.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    /*
                     * debut cryptage
                     */
                    TestCipherDESede Cryptage = new  TestCipherDESede();
                    String message = passwordCrypte;
     
                    KeyGenerator keyGen;
                    try {
                      keyGen = KeyGenerator.getInstance("DESede");
                      keyGen.init(168);
                      SecretKey cle = keyGen.generateKey();
                      System.out.println("cle : " + new String(cle.getEncoded()));
     
                      byte[] enc = TestCipherDESede.encrypter(message, cle);
                      System.out.println("texte encrypte : " + new String(enc));  // cryptage enc
     
                      String dec = TestCipherDESede.decrypter(enc, cle);  // décrypter néssécite enc
                      System.out.println("texte decrypte : " + dec);
                      message = dec;
     
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
     
                    /*
                     * fin cryptage
                     *
    Je ne sais pas comment décrypter.
    Y a t'il un cryptage plus puissant.
    SHA-256 or SHA3-256
    Merci

  4. #4
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 559
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 559
    Points : 21 619
    Points
    21 619
    Par défaut
    ............ Oui, c'est normal, on ne peut pas déchiffrer un message qui n'est pas chiffré.

    Note que SHA n'est pas un algo de chiffrement, mais de hachage.
    Et le hachage est en général la solution choisie pour le stockage de mots de passe. Sauf à moins de faire un gestionnaire de mots de passe. Mais quand on fait un gestionnaire de mots de passe, on ne crée pas une nouvelle clé de chiffrage pour chaque nouveau mot de passe, c'est inutile (ou alors je ne vois pas comment c'est rendu utile)

  5. #5
    Membre averti
    Homme Profil pro
    Architecte technique
    Inscrit en
    Mai 2020
    Messages
    329
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mai 2020
    Messages : 329
    Points : 443
    Points
    443
    Par défaut
    Bonjour,

    Comme le dit thelvin, c'est normal:

    Oui, c'est normal, on ne peut pas déchiffrer un message qui n'est pas chiffré.
    Note que SHA n'est pas un algo de chiffrement, mais de hachage.
    En réalité, on ne déchiffre pas le mot de passe. Pour vérifier que le mot de passe soit correct, on hash le nouveau avec le même token et on compare les deux.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Crypter et decrypter le mot de passe
    Par BILANGA dans le forum Développement
    Réponses: 1
    Dernier message: 26/08/2011, 19h10
  2. Réponses: 0
    Dernier message: 20/09/2007, 15h31
  3. [MySQL] Comment modifier l'ancien mot de passe du membres dans la base de donnée ?
    Par oceanne dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 28/03/2007, 12h11
  4. Réponses: 10
    Dernier message: 16/04/2006, 09h18
  5. [vb.net][cryptage] crypter simplement un mot de passe
    Par arnolem dans le forum Windows Forms
    Réponses: 9
    Dernier message: 05/01/2006, 11h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo