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

Sécurité Java Discussion :

Blowfish PHP -> Java


Sujet :

Sécurité Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mai 2007
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Mai 2007
    Messages : 187
    Par défaut Blowfish PHP -> Java
    Salut, j'essaye de mettre en place un algo de cryptage symétrique qui fonctionne également en Java et Php.

    mais apparement les algorithme de blowfish ne semble pas fonctionner de la même maniere sous php et Java.

    voici ce que je fait :

    en php :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    function encrypt($cleartext, $key){
    	$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
        $key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
        $iv = '12345678';
     
    	// 128-bit blowfish encryption:
    	if (mcrypt_generic_init($cipher, $key, $iv) != -1){
    		// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
    		$cipherText = mcrypt_generic($cipher,$cleartext );
    		mcrypt_generic_deinit($cipher);
    	}
     
    	return $cipherText;
    }
    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
    function decrypt($encrypted, $key){
    	$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
        $key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
        $iv = '12345678';
     
    	// 128-bit blowfish encryption:
    	if (mcrypt_generic_init($cipher, $key, $iv) != -1){
    		// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
    		$cleartext = mdecrypt_generic($cipher,$encrypted );
    		mcrypt_generic_deinit($cipher);
    	}
     
    	return $cleartext;
     
    }
    et en Java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public static byte[] encrypt(byte[] messageBytes, byte[] key) throws GeneralSecurityException {
    	SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
    	Cipher cipher = Cipher.getInstance("Blowfish");
    	cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    	byte[] encrypted = cipher.doFinal(messageBytes);
    	return encrypted;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public static byte[] decrypt(byte[] encrypted, byte[] key) throws GeneralSecurityException {
    	SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
    	Cipher cipher = Cipher.getInstance("Blowfish");
    	cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    	byte[] decrypted = cipher.doFinal(encrypted);
    	return decrypted;
    }
    Je vous passe les détails de cablage...

    la boucle clearText->encoded->clearText fonction bien sous Java et php... mais pas entre Java et Php...

    quelqu'un peut t'il m'aiguiller?

    merci.

    Patrice.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Salut,
    En Java tu ne précises pas ton vecteur initial (IV), alors que tu le fais en php... Je ne sais pas quelle est la valeur par défaut en Java (surement un truc du genre "00000"), mais donne la même valeur qu'en php et ca devrait marcher...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
    IvParameterSpec ivs = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, skey, ivs);
    Vérifie aussi le padding ajouté en java. Pour être sûr de ce que tu as en Java tu peux utiliser ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mai 2007
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Mai 2007
    Messages : 187
    Par défaut
    Merci pour la réponse,
    En effet le résultat est bien meilleurs cependant :

    lorsque j'encode un message avec les mêmes clé, et IV en java et php j'obtient les chaines suivantes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    php  : dc7b240302310f9c017c1e08d1d7c70a9bd755b4026cca857c12227d8a57e74b506fbaf429cc63d520c88866bd16f572
    java : dc7b240302310f9c017c1e08d1d7c70a9bd755b4026cca857c12227d8a57e74b506fbaf429cc63d5b5d1137a52c90b90
           --------------------------------------------------------------------------------^^^^^^^^^^^^^^^^
    tout est identiques SAUF les 8 dernier bytes.

    une idée?

    merci.

  4. #4
    Invité
    Invité(e)
    Par défaut
    je dirais : le padding.
    Php remplit avec "0000000" je crois, je ne sais pas comment java remplit... regarde de ce côté

  5. #5
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Mai 2007
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Mai 2007
    Messages : 187
    Par défaut
    ok, la solution finale etait effectivement dans le padding.
    j'ai finalement préféré gérer le padding manuellement dans php.

    donc le code finale fonctionnel est :
    en 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
    	public static byte[] encrypt(byte[] messageBytes, byte[] key) throws GeneralSecurityException {
    		Provider[] pvs = Security.getProviders();
    		SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
    		Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
    		String iv = "00000000";
    		IvParameterSpec ivs = new IvParameterSpec(iv.getBytes());
    		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivs);
    		byte[] encrypted = cipher.doFinal(messageBytes);
    		return encrypted;
    	}
     
    	public static byte[] decrypt(byte[] encrypted, byte[] key) throws GeneralSecurityException {
    		SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
    		Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
    		String iv = "00000000";
    		IvParameterSpec ivs = new IvParameterSpec(iv.getBytes());
    		cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivs);
    		byte[] decrypted = cipher.doFinal(encrypted);
    		return decrypted;
    	}
    en Php :
    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
    function encrypt($cleartext, $key){
    	$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
        $key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
    	$iv = '00000000';
     
    	// ajout du Padding.
    	$cleartext_length = strlen($cleartext)%8;
    	for($i=$cleartext_length; $i<8; $i++){
    		$cleartext .= chr(8-$cleartext_length);
    	}
     
    	$cipherText='';
    	// 128-bit blowfish encryption:
    	if (mcrypt_generic_init($cipher, $key, $iv) != -1){
    		// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
    		$cipherText = mcrypt_generic($cipher,$cleartext );
    		mcrypt_generic_deinit($cipher);
    	}
     
    	return $cipherText;
    }
     
    function decrypt($encrypted, $key){
    	$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
        $key = substr($key, 0, mcrypt_enc_get_key_size($cipher));
    	$iv = '00000000';
     
    	// 128-bit blowfish encryption:
    	if (mcrypt_generic_init($cipher, $key, $iv) != -1){
    		// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
    		$cleartext = mdecrypt_generic($cipher,$encrypted );
    		mcrypt_generic_deinit($cipher);
    	}
     
    	// suppression du padding.
    	$pad = ord(substr($cleartext,strlen($cleartext)-1));
    	if($pad>0 & $pad<=8){
    		$cleartext = substr($cleartext, 0, strlen($cleartext) - $pad);
    	}
     
    	return $cleartext;
     
    }
    Ce code me permet donc d'encrypter/décrypter indifféremment en Php et en Java de la même manière...

    Ho joie.

    merci George7 pour ton aide.

    Patrice.

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    334
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 334
    Par défaut
    Salut, je me doit de vous remercier tout les 2 car c'est exactement ce que je recherchai



    ps: j'ai parlé un peu vite, je n'ai pas encore réussi à faire fonctionner tout ça ...
    j'ai ouvert un nouveau post ici concernant la transmission des données cryptées.

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

Discussions similaires

  1. Que choisir : PHP versus Java ?
    Par Sniper37 dans le forum Général Conception Web
    Réponses: 164
    Dernier message: 28/04/2009, 16h50
  2. C vs PHP vs JAVA d'un point de vue réseau
    Par deaven dans le forum Langages de programmation
    Réponses: 48
    Dernier message: 17/06/2008, 02h27
  3. [Tomcat]échange de session entre php et java
    Par benwa dans le forum Tomcat et TomEE
    Réponses: 18
    Dernier message: 05/06/2007, 17h01
  4. PHP ou Java pour un site associatif ?
    Par Flashball dans le forum Général Conception Web
    Réponses: 2
    Dernier message: 02/03/2006, 19h56
  5. [JSP/Tomcat] Intégration de PHP dans Java
    Par milhouz_deglingos dans le forum Tomcat et TomEE
    Réponses: 3
    Dernier message: 17/08/2005, 12h02

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