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 :

Soucis décryptage Blowfish Java


Sujet :

Sécurité Java

  1. #1
    Candidat au Club
    Inscrit en
    Décembre 2012
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Décembre 2012
    Messages : 1
    Points : 2
    Points
    2
    Par défaut Soucis décryptage Blowfish Java
    Bonjour,

    J'espère avoir posté cela dans la bonne section ^^.

    Voila je suis un train de faire un programme qui permet de crypter le contenue d'un fichier texte en blowfish et d’écrire le résultat dans un nouveau fichier.

    Pour le cryptage j'ai de pas de soucis, cependant pour le décryptage il me met l'erreur suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    Exception in thread "main" java.lang.NullPointerException
    	at java.lang.String.<init>(Unknown Source)
    	at MyBlowfish.decryptInString(MyBlowfish.java:65)
    	at MyBlowfish.main(MyBlowfish.java:121)
    D’après les recherches que j'ai effectué le problème viendrai des byte [].

    Je vous joint mon code entier en espérant que quelqu’un puisse m'éclairer


    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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.math.*;
    import java.nio.charset.Charset;
     
    import javax.crypto.*;
    import java.security.*;
    import javax.crypto.spec.*;
     
     
    public class MyBlowfish {
    	public final static int KEY_SIZE = 128; // [32..448]
     
    	private Key secretKey;
     
    	public MyBlowfish() {
    	}
     
    	public Key getSecretKey() {
    		return secretKey;
    	}
     
    	public byte[] crypt(byte[] plaintext, String key) {
    		Charset UTF8 = Charset.forName("UTF-8");
    		if (key.getBytes(UTF8).length > 8) {
    			key = key.substring(0, 8);
    		}
    		try {
    			secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "Blowfish");
    			Cipher cipher = Cipher.getInstance("Blowfish");
    			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    			return cipher.doFinal(plaintext);
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    		return null;
    	}
     
    	public byte[] crypt(String plaintext, String key) {
    		return crypt(plaintext.getBytes(Charset.forName("UTF-8")), key);
    	}
     
    	public byte[] decryptInBytes(byte[] ciphertext, String key) {
    		Charset UTF8 = Charset.forName("UTF-8");
    		if (key.getBytes(UTF8).length > 8) {
    			key = key.substring(0, 8);
    		}
    		try {
    			secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "Blowfish");
    			Cipher cipher = Cipher.getInstance("Blowfish");
    			cipher.init(Cipher.DECRYPT_MODE, secretKey);
    			return cipher.doFinal(ciphertext);
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    		return null;
    	}
     
    	public String decryptInString(byte[] ciphertext, String key) {
    		return new String(decryptInBytes(ciphertext, key));
    	}
     
    	public String read(String path) {
    		String myText = "";
    		try {
    			InputStream ips = new FileInputStream(path);
    			InputStreamReader ipsr = new InputStreamReader(ips);
    			BufferedReader br = new BufferedReader(ipsr);
    			String ligne;
    			while ((ligne = br.readLine()) != null) {
    				myText += ligne + "\n";
    			}
    			br.close();
    		} catch (Exception e) {
    			System.out.println(e.toString());
    		}
    		return myText;
    	}
     
    	public void write(String path, String text) {
    		PrintWriter ecri;
    		try {
    			ecri = new PrintWriter(new FileWriter(path));
    			ecri.print(text);
    			ecri.flush();
    			ecri.close();
    		}// try
    		catch (NullPointerException a) {
    			System.out.println("Erreur : pointeur null");
    		} catch (IOException a) {
    			System.out.println("Problème d'IO");
    		}
    	}
     
    	public static void main(String[] args) {
    		if (args[0].equals("crypt")) {
    			MyBlowfish bf = new MyBlowfish();
    			String myText = bf.read(args[2]);
    			System.out.println("plaintext = " + myText);
    			String myKey = args[1];
    			byte[] crypted = bf.crypt(myText, myKey);
    			System.out.println("Your key: " + myKey);
    			System.out.println("text Crypted: " + new BigInteger(crypted));
    			System.out.println("text Crypted: " + new String(crypted));
    			bf.write(args[3], new String(crypted));
    			String textDecrypted = bf.decryptInString(crypted, myKey);
    			System.out.println("Text Decrypted: " + textDecrypted);
    		}else if (args[0].equals("decrypt")){
    			MyBlowfish bf = new MyBlowfish();
    			String myKey = args[1];
    			String myText = bf.read(args[2]);
    			byte[] myTextByte = myText.getBytes(Charset.forName("UTF-8"));
     
    			System.out.println("plaintext = " + myText);
    			System.out.println("plaintext = " + new BigInteger(myTextByte));
    			String textDecrypted = bf.decryptInString(myTextByte, myKey);
    			System.out.println("Your key: " + myKey);
    			System.out.println("Text Decrypted: " + textDecrypted);
    			bf.write(args[3], textDecrypted);			
    		}
    	}
    }
    Merci d'avance.

  2. #2
    Expert éminent sénior
    Avatar de Marco46
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    4 413
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Août 2005
    Messages : 4 413
    Points : 19 609
    Points
    19 609
    Par défaut
    Ton problème est au niveau de la génération de la clef de session.

    Pour générer une clé de session cf la doc officielle.

    Pour générer une clé de session depuis un mot de passe utilisateur cf la doc officielle.

    Sinon le bon vocabulaire c'est chiffrer quand on veut rendre illisible un message.
    Déchiffrer quand on veut rendre lisible un message chiffré et qu'on a la clé.
    Et décrypter quand on veut rendre lisible un message chiffré et qu'on a PAS la clé.
    Un problème avec Git ? Essayez la FAQ, sinon posez votre question sur le forum.



    "Toute personne croyant qu'une croissance exponentielle peut durer indéfiniment dans un monde fini est soit un fou, soit un économiste."
    Kenneth E. Boulding

    "Les richesses naturelles sont inépuisables, car, sans cela, nous ne les obtiendrions pas gratuitement. Ne pouvant être ni multipliées ni épuisées, elles ne sont pas l’objet des sciences économiques."
    Jean-Baptiste Say, Traité d'économie politique, 1803.

    "/home/earth is 102% full ... please delete anyone you can."
    Inconnu

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

Discussions similaires

  1. Problème cryptage décryptage BlowFish
    Par xokami35x dans le forum Sécurité
    Réponses: 3
    Dernier message: 31/03/2010, 13h15
  2. Cryptage/Décryptage compatible Java Linux
    Par pragmatique dans le forum Langage
    Réponses: 0
    Dernier message: 30/11/2009, 14h46
  3. Souci de compilation java sur unix
    Par Lolitaaa dans le forum ANT
    Réponses: 3
    Dernier message: 22/05/2009, 16h15
  4. Soucis menu déroulant Java / CSS sous IE
    Par ghmpou dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 20/12/2007, 19h34
  5. Décryptage en java d'un fichier crypté en php
    Par yodaruli dans le forum Sécurité
    Réponses: 3
    Dernier message: 18/07/2007, 11h13

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