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 :

Problème cryptage décryptage BlowFish


Sujet :

Sécurité Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Par défaut Problème cryptage décryptage BlowFish
    Bonjour,

    J'ai un code qui permet de crypter / décrypter via l'algorithme de BlowFish un fichier choisi par l'utilisateur grâce à un mot de passe ou à un fichier clé.
    Lors du cryptage aucun problème, cependant, lorsque vient la phase de décryptage, plus rien ne va, le fichier décrypté contient encore des caractères cryptés, si vous voyez ce que je veux dire, comme si la clé n'était pas bonne, alors que je suis sur que c'est la même.
    Voici mon code

    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    import javax.crypto.*;
    import javax.crypto.spec.*;
     
    import java.io.*;
    import java.security.*;
     
    public class thread_crypt implements Runnable {
     
        String action;
        String methode;
        String passchemin;
        String chemin;
        fenetre fen;
     
        public thread_crypt(String action, String methode, String passchemin, String chemin) {
            this.action = action;
            this.methode = methode;
            this.passchemin = passchemin;
            this.chemin = chemin;
        }
        @Override
        public void run() {
            try {
            // TODO Auto-generated method stub
                File file = new File(chemin);
                File file2 = new File(passchemin);
                if(file.isFile()) {
                Key key;
                if(methode.equals("pass")) { //si on veut crypter avec un mot de pass
                    key = new SecretKeySpec(passchemin.getBytes(), "Blowfish");
                }
                else { //sinon si on crypte avec un fichier cle
                    if(file2.isFile()) {
                    FileInputStream in = new FileInputStream(passchemin);
                    ObjectInputStream reader = new ObjectInputStream(in);
                    key = (Key) reader.readObject();
                    }else{ //si le fichier cle specifie est incorrecte
                        System.out.println("error");
                        return;
                    }
                }
                if(action.equals("Crypter")) {
                    Encrypt(chemin, key);
                }else {
                    Decrypt(chemin, key);
                }
                }else {
                    System.out.println("Error fichier");
                }
            }catch(Exception e) { e.printStackTrace(); }
            }
     
        public static void Encrypt(String chemin, Key key) { //methode d'encryption
            try {
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            FileInputStream in = new FileInputStream(chemin);
            FileOutputStream out = new FileOutputStream("text_encrypted.txt");
            CipherOutputStream out_cipher = new CipherOutputStream(out, cipher);
            byte[] tab = new byte[1024];
            int nbreoctet = 0;
            while((nbreoctet = in.read(tab)) != -1) {
                out_cipher.write(cipher.doFinal(tab), 0, nbreoctet);
            }
            out_cipher.close();
            out.close();
            in.close();
            System.out.println("Cryptage terminee");
            }catch(Exception e) { e.printStackTrace(); }
        }
     
        public static void Decrypt(String chemin, Key key) {
            try {
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(cipher.DECRYPT_MODE, key);
            FileInputStream in = new FileInputStream(chemin);
            CipherInputStream in_cipher = new CipherInputStream(in, cipher);
            FileOutputStream out = new FileOutputStream("text_decrypted.txt");
            byte[] tab = new byte[1024];
            int nbreoctet = 0;
            while((nbreoctet = in_cipher.read(tab)) != -1) {
                out.write(tab, 0, nbreoctet);
            }
            out.close();
            in_cipher.close();
            in.close();
            System.out.println("Decryptage terminee");
            }catch(Exception e) { e.printStackTrace(); }
        }
    }
    Quelqu'un voit il d'où vient le problème?

    Merci d'avance

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 38
    Par défaut
    Bonjour,

    Je pense qu'il y a un problème a ce niveau:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    byte[] tab = new byte[1024];
    int nbreoctet = 0;
    while((nbreoctet = in.read(tab)) != -1) {
      out_cipher.write(cipher.doFinal(tab), 0, nbreoctet);
    }
    out_cipher.close();
    Utilise plutôt la méthode update! de cette façon:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    byte[] buffer = new byte[4096];
    int len;				
    while((len = is.read(buffer)) != -1)
    {
      	byte[] cipherBlock = cipher.update(buffer, 0, len);
        	os.write(cipherBlock);
    }		    
    os.write(cipher.doFinal());

  3. #3
    Expert confirmé
    Avatar de Marco46
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    4 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Août 2005
    Messages : 4 419
    Par défaut
    Tu n'as pas donné à ton Cipher quel padding et quel mode elle doit utiliser pour l'algorithme Blowfish. Je pense que ça peut venir de là.

    La JCE supporte différents modes et padding pour Blowfish.

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Par défaut
    Ca marche, le problème venait du fait que je faisais un dofinal dans le CipherOutputStream, il y avait donc 2 cryptages.

    Merci

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

Discussions similaires

  1. Problème de décryptage Blowfish
    Par bombers dans le forum Général Java
    Réponses: 3
    Dernier message: 10/02/2014, 19h14
  2. Réponses: 0
    Dernier message: 28/02/2011, 20h58
  3. problème cryptage blowfish
    Par xokami35x dans le forum Sécurité
    Réponses: 10
    Dernier message: 25/02/2010, 09h29
  4. Problème de cryptage/décryptage RSA en Java
    Par Reeter dans le forum Sécurité
    Réponses: 2
    Dernier message: 29/03/2009, 22h41
  5. Problème cryptage Blowfish
    Par Nicoclem dans le forum Composants VCL
    Réponses: 2
    Dernier message: 12/03/2009, 15h26

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