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 :

Cryptage d'un fichier en Blowfich


Sujet :

Sécurité Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2012
    Messages : 40
    Par défaut Cryptage d'un fichier en Blowfich
    Bonsoir,j'ai un problème au moment de décryptage d'un fichier.voila mon code
    package application;

    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
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
     
    import org.apache.commons.codec.binary.Base64;
     
     
    public class DecrypBlowfish {
     
        private static String strkey ="Blowfish";
        private static Base64 base64 = new Base64(true);
     
         //encrypt using blowfish algorithm
        public static byte[] encrypt(byte[] Data)throws Exception{
     
            SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.ENCRYPT_MODE, key);
     
             return base64.encode(Data);
     
        }
     
        //decrypt using blow fish algorithm
        public static byte[] decrypt(byte[] encrypted)throws Exception{
            byte[] encryptedData = base64.decodeBase64(encrypted);
             SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.DECRYPT_MODE, key);
             byte[] decrypted = cipher.doFinal(encryptedData);
             return decrypted; 
     
        }
         private static String LireString() {
            String ligne_lue=null; 
    try{ 
    InputStreamReader lecteur=new InputStreamReader(System.in); 
    BufferedReader entree=new BufferedReader(lecteur); 
    ligne_lue=entree.readLine(); 
    } 
    catch(IOException err){ 
    System.exit(0); 
    } 
    return ligne_lue;
        }
     
        public static void main(String[] args) throws Exception {
            String fichier;
    System.out.println("Donner le nom du fichier a decrypter:  ");
    fichier = LireString();
    File f = new File(fichier);
    byte[] tab =java.nio.file.Files.readAllBytes(f.toPath());
            byte[] decoded = decrypt(tab);
            FileWriter fw = new FileWriter("bh.txt",true);
            fw.write(new String(decoded));
            fw.close();
        }
    }
    voila l'exeption
    Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(BlowfishCipher.java:319)
    at javax.crypto.Cipher.doFinal(Cipher.java:2087)
    at application.DecrypBlowfish.decrypt(DecrypBlowfish.java:38)
    at application.DecrypBlowfish.main(DecrypBlowfish.java:61)
    Pouvez vous m'aider?

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    ta méthode encrypt fait tout sauf de l'encryptage. Elle se contente de faire de l'encodage base64.


    Tes deux codes doivent travailler de la même manière.

  3. #3
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2012
    Messages : 40
    Par défaut
    T'as raison il faut faire
    return cipher.doFinal(base64.encode(Data));
    Mais toujours j'ai la même exception
    Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    Que dois je faire?

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Tu a inversé tes 2 appels

    On encrypt et puis on protège avec du base64, pas l'inverse


    Là tu est occupé de faire

    data -> base64Encode -> crypt -> base64Decode -> decrypt -> data
    Alors qu'il faudrait
    data -> crypt -> base64Encode -> base64Decode -> decrypt -> data

  5. #5
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2012
    Messages : 40
    Par défaut
    Merci beaucoup,je suis débutante dans ce domaine.Avez vous une idée pour récupérer la clé de cryptage?
    J'ai essayer en l'enregistrant dans un fichier mais quand je la récupère en forçant le type( SecretKey) me génère une erreur.

Discussions similaires

  1. Cryptage d'un fichier par inversion de bits
    Par Theo190107 dans le forum x86 16-bits
    Réponses: 19
    Dernier message: 31/08/2007, 22h34
  2. cryptage d'un fichier sous UNIX
    Par medora dans le forum Linux
    Réponses: 5
    Dernier message: 13/04/2007, 11h39
  3. cryptage d'un fichier par la méthode césar
    Par wedge.tm dans le forum C
    Réponses: 6
    Dernier message: 12/01/2007, 16h08
  4. Problème cryptage simple de fichier
    Par darthnexus dans le forum C++
    Réponses: 4
    Dernier message: 03/06/2006, 18h16
  5. Suprimer le cryptage d'un fichier
    Par sehing dans le forum Autres Logiciels
    Réponses: 1
    Dernier message: 25/01/2006, 16h59

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