Cryptage Blowfish

foufou20071987 Messages postés 13 Date d'inscription mercredi 18 mai 2011 Statut Membre Dernière intervention 9 novembre 2013 - 30 août 2013 à 15:51
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 2 sept. 2013 à 08:09
Bonjour,


j'ai trouver un code de cryptage Blowfish qui crypte et décrypte un fichier.Le problème c'est que le fichier de cryptage s'affiche avec des caractères illisible.J'ai entendu de la classe Base64 mais j'arrive pas a la manipuler.Pouvez vous m'aider?
Voila le code
package application;


import java.io.BufferedReader;
import java.io.File;
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 DecrypBlowfish {
public final static int KEY_SIZE = 128; // [32..448]

private Key secretKey;

public DecrypBlowfish() {
}

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) throws IOException {
String fichier;
System.out.println("Quel est le nom de votre fichier ?");
fichier = LireString();
File f = new File(fichier);
File fl = new File("crepted.txt");
String path=fl.getCanonicalPath();
String filePath = f.getPath() ;
String p="azfgjkop";
DecrypBlowfish bf = new DecrypBlowfish();
String myText = bf.read(filePath);

byte[] cle = (new String(p)).getBytes(); // 24 caractères
SecretKey key = new SecretKeySpec(cle, "Blowfish");
String myKey = key.toString();
byte[] crypted = bf.crypt(myText, myKey);

bf.write(path, new String(crypted));
String textDecrypted = bf.decryptInString(crypted, myKey);
File filed = new File("decrepted.txt");
String npath=filed.getCanonicalPath();
bf.write(npath, textDecrypted);


}
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;
}
}

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
2 sept. 2013 à 08:07
Salut,

Il te suffit d'utiliser :
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);
2
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
2 sept. 2013 à 08:09
Mais il n'est pas certain du tout que le problème vienne de là...
0
Rejoignez-nous