Bonjour à tous,
J'experimente un code qui utilise l'algorithme de blowfish.
Avant toute chose voici le code:
Lorsque je ne passais pas par un fichier, tout marchait niquel, cependant cela n'a aucun interet. J'ai donc voulu enregistré le texte chiffré dans un fichier, puis le charger pour le dechiffrer ensuite. Cependant là probleme:
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 import javax.crypto.*; import java.io.*; import java.security.*; public class main { /** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub String texte = "Hello world"; File text = new File("test.txt"); FileWriter writer = new FileWriter(text); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(text))); text.createNewFile(); Key key = keygen(); byte[] texte_crypte = Encrypt(texte, key); writer.write(texte_crypte.toString()); writer.flush(); System.out.println(key.toString()); System.out.println(Decrypt(reader.readLine().getBytes(), key)); } public static byte[] Encrypt(String text, Key key) { try { Cipher cipher = Cipher.getInstance("Blowfish"); cipher.init(Cipher.ENCRYPT_MODE, key); System.out.println("Text encrypted"); return cipher.doFinal(text.getBytes()); }catch(Exception e) { e.printStackTrace(); } return null; } public static String Decrypt(byte[] text, Key key) { try { Cipher cipher = Cipher.getInstance("Blowfish"); cipher.init(cipher.DECRYPT_MODE, key); System.out.println("Text decrypted"); return new String(cipher.doFinal(text), "UTF8"); }catch(Exception e) { e.printStackTrace(); } return null; } public static Key keygen() { try { KeyGenerator keygen = KeyGenerator.getInstance("Blowfish"); keygen.init(56); System.out.println("key generates"); return keygen.generateKey(); }catch(Exception e) { e.printStackTrace(); } return null; } }
Peut etre est une erreur lorsque j'enregistre le texte dans le fichier? Faut il rester en binaire?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..) at main.Decrypt(main.java:41) at main.main(main.java:23)
De plus, si je veux enregistrer la clé, je dois la transfrmer en String? cependant il m'affiche une valeurs bizarre:
javax.crypto.spec.SecretKeySpec@26850376
Donc ma question est:
Comment enregistré la clé généré et le texte chiffré?
Merci d'avance
Partager