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

Langage PHP Discussion :

Chiffrement DES / ECB / PKCS5Padding


Sujet :

Langage PHP

  1. #1
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut Chiffrement DES / ECB / PKCS5Padding
    Salut,

    J'ai une application java qui crypte des mots de passe dans une base de données en utilisant javax.crypto.Cipher :
    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
    public String encrypt (String value)
    	{
    		String clearText = value;
    		if (clearText == null)
    			clearText = "";
    		//	Init
    		if (m_cipher == null)
    			initCipher();
    		//	Encrypt
    		if (m_cipher != null)
    		{
    			try
    			{
    				m_cipher.init(Cipher.ENCRYPT_MODE, m_key);
    				byte[] encBytes = m_cipher.doFinal(clearText.getBytes());
    				String encString = convertToHexString(encBytes);
    				log.log (Level.ALL, value + " => " + encString);
    				return encString;
    			}
    			catch (Exception ex)
    			{
    				log.log(Level.INFO, value, ex);
    			}
    		}
    		//	Fallback
    		return CLEARVALUE_START + value + CLEARVALUE_END;
    	}
    private synchronized void initCipher()
    	{
    		if (m_cipher != null)
    			return;
    		Cipher cc = null;
    		try
    		{
    			cc = Cipher.getInstance("DES/ECB/PKCS5Padding");
    			m_key = SecureKey.key;
    		}
    		catch (Exception ex)
    		{
    			log.log(Level.SEVERE, "", ex);
    		}
    		m_cipher = cc;
    	}	//	initCipher
    Comment faire la même chose en PHP pour que le résultat crypté soit le même ?

    Merci d'avance

  2. #2
    Membre émérite
    Avatar de Seb33300
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    1 564
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Thaïlande

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 564
    Points : 2 399
    Points
    2 399
    Par défaut
    Il semblerait que tu dois t'orienter vers la fonction php mcrypt_module_open()
    http://www.php.net/manual/fr/functio...odule-open.php

  3. #3
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut
    Citation Envoyé par Seb33300 Voir le message
    Il semblerait que tu dois t'orienter vers la fonction php mcrypt_module_open()
    http://www.php.net/manual/fr/functio...odule-open.php
    Merci beaucoup. En effet, je suis parvenu à crypter. Malheureusement, le résultat n'est pas le même entre PHP et Java.

    Comment les rendre compatibles ?

    Code java :
    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
    String value = "toto";
    SecretKey key = new SecretKeySpec( "tutututu".getBytes(), "DES" );
    Cipher m_cipher = Cipher.getInstance( "DES/ECB/PKCS5Padding" );
    m_cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encBytes = m_cipher.doFinal(value.getBytes());
    String encString = Secure.convertToHexString(encBytes);
    System.out.println( encString );
     
    public static String convertToHexString (byte[] bytes)
    	{
    		//	see also Util.toHex
    		int size = bytes.length;
    		StringBuffer buffer = new StringBuffer(size*2);
    		for(int i=0; i<size; i++)
    		{
    			// convert byte to an int
    			int x = bytes[i];
    			// account for int being a signed type and byte being unsigned
    			if (x < 0)
    				x += 256;
    			String tmp = Integer.toHexString(x);
    			// pad out "1" to "01" etc.
    			if (tmp.length() == 1)
    				buffer.append("0");
    			buffer.append(tmp);
    		}
    		return buffer.toString();
    	}   //  convertToHexString
    Résultat : 8f57949213079709

    Code PHP :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $td = mcrypt_module_open( MCRYPT_DES, '', MCRYPT_MODE_ECB, '' );
     
    $ks = mcrypt_enc_get_key_size($td);
     
    $key = "tutututu";
     
    mcrypt_generic_init($td, $key, null);
     
    $encrypted = mcrypt_generic($td, 'toto');
    echo bin2hex($encrypted);exit();
    Résultat : d9a4024876ddc5da

  4. #4
    Membre émérite
    Avatar de Seb33300
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    1 564
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Thaïlande

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 564
    Points : 2 399
    Points
    2 399
    Par défaut
    Bonjour,

    Je ne connais pas très bien java.
    Qu'est ce qu'est censé faire
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    "tutututu".getBytes()

    Ne faudrait il pas enlever le .getBytes() ?
    Ou appliquer le même traitement sur la chaine php ?

  5. #5
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut
    Je ne pense pas, c'est parce que la classe SecretKeySpec veut recevoir la clé sous forme d'un tableau de bytes. En fait, getBytes retourne le code ascii de chaque caractère de "tutututu" (116, 117, 116, 117, 116, 117, 116, 117).

  6. #6
    Membre émérite
    Avatar de Seb33300
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    1 564
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Thaïlande

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 564
    Points : 2 399
    Points
    2 399
    Par défaut
    Tu saurais me donner la valeur crypté obtenu par ton code java de ton exemple (toto avec key tutututu) ?

  7. #7
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut
    Citation Envoyé par Seb33300 Voir le message
    Tu saurais me donner la valeur crypté obtenu par ton code java de ton exemple (toto avec key tutututu) ?
    Oui, c'est
    8f57949213079709

    alors qu'avec PHP j'obtiens
    d9a4024876ddc5da

  8. #8
    Membre émérite
    Avatar de Seb33300
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    1 564
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Thaïlande

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 564
    Points : 2 399
    Points
    2 399
    Par défaut
    J'ai fait des test avec des crypteur online et la valeur retournée avec la méthode php semble correspondre.

    Peut tu me dire ce que renvoie :
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    String value = "toto";
    SecretKey key = new SecretKeySpec( "tutututu".getBytes(), "DES" );
    Cipher m_cipher = Cipher.getInstance( "DES/ECB/PKCS5Padding" );
    m_cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encBytes = m_cipher.doFinal(value.getBytes());
    System.out.println( encBytes.toString() );

    (sans passer par ta fonction convertToHexString)


    En théorie ça devrais être la même chose que :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $td = mcrypt_module_open( MCRYPT_DES, '', MCRYPT_MODE_ECB, '' );
     
    $ks = mcrypt_enc_get_key_size($td);
     
    $key = "tutututu";
     
    mcrypt_generic_init($td, $key, null);
     
    $encrypted = mcrypt_generic($td, 'toto');
    echo $encrypted;
    exit();
    soit :
    Ù¤HvÝÅÚ
    Si c'est le cas, c'est que ta fonction convertToHexString() et bin2hex() ne font pas la même chose.

  9. #9
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut
    Avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    String value = "toto";
    SecretKey key = new SecretKeySpec( "tutututu".getBytes()/*new byte[] {100,25,28,-122,-26,94,-3,-26}*/, "DES" );
    Cipher m_cipher = Cipher.getInstance( "DES/ECB/PKCS5Padding" );
    m_cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encBytes = m_cipher.doFinal(value.getBytes());
    System.out.println( new String(encBytes) );
    j'obtiens
    �W���

  10. #10
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut
    Au niveau des codes ascii ça donne pour java :
    � = -113
    W = 87
    � = -108
    � = -110
     = 19
     = 7
    � = -105
    = 9
    et pour php :
    Ù = 217
    ¤ = 164
     = 2
    H = 72
    v = 118
    Ý = 221
    Å = 197
    Ú = 218

  11. #11
    lr
    lr est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    338
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2003
    Messages : 338
    Points : 114
    Points
    114
    Par défaut
    Qqun aurait-il une idée du problème ?

Discussions similaires

  1. Logiciel de chiffrement des données pour ordinateurs portables
    Par iusedtodream dans le forum Autres Solutions d'entreprise
    Réponses: 0
    Dernier message: 10/11/2010, 09h54
  2. Chiffrement des page PHP
    Par Ikmuss dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 1
    Dernier message: 19/08/2009, 15h59
  3. Essbase : chiffrement des paramètres de connexion des scripts MaxL
    Par Fleur-Anne.Blain dans le forum Contribuez
    Réponses: 0
    Dernier message: 01/07/2009, 19h36
  4. [Sécurité] Chiffrement des infos dans l'URL
    Par Dsphinx dans le forum Langage
    Réponses: 14
    Dernier message: 04/01/2007, 15h57

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