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:
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