Bonjour,
J'ai un petit programme en Java qui permet de crypter une chaine avec l'algorithme blowfish, le problème c'est que, d'après ce que j'ai compris, il génère la clé de cryptage tout seul, elle est de type byte[], j'ai essayé de la convertir en String mais j'ai pas réussi. Le problème c'est quels sont les modifications nécessaires a faire pour que la clé de cryptage/décryptage soit entrée en paramètre en plus du texte a crypter ?

Voici l'algorithme en question :
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
// Updates: 2004.03.23
 
 
import java.io.*;
import java.math.*;
import java.util.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.spec.*;
import java.security.interfaces.*;
 
 
/** 
 * Cette classe propose des méthodes permettant de crypter et décrypter des 
 * messages avec l'algorithme de Blowfish.
 */
public class MyBlowfish {
  public final static int KEY_SIZE = 128;  // [32..448]
 
  private Key secretKey;
 
 
  public MyBlowfish() {
  }
 
 
  public Key getSecretKey() {
    return secretKey;
  }
 
 
  /**
   * Retourne toutes les informations de la clé sous forme d'un tableau de
   * bytes. Elle peut ainsi être stockée puis reconstruite ultérieurement en
   * utilisant la méthode setSecretKey(byte[] keyData)
   */
  public byte[] getSecretKeyInBytes() {
    return secretKey.getEncoded();
  }
 
 
  public void setSecretKey(Key secretKey) {
    this.secretKey = secretKey;
  }
 
 
  /**
   * Permet de reconstruire la clé secrète à partir de ses données, stockées 
   * dans un tableau de bytes.
   */
  public void setSecretKey(byte[] keyData) {
    secretKey = new SecretKeySpec(keyData, "Blowfish");    
  }
 
 
  public void generateKey() {
    try {
      KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");
      keyGen.init(KEY_SIZE);
      secretKey = keyGen.generateKey();  
    }
    catch (Exception e) {System.out.println(e);} 
  }
 
 
  public byte[] crypt(byte[] plaintext) {
    try {
      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) {
    return crypt(plaintext.getBytes());
  }
 
 
  public byte[] decryptInBytes(byte[] ciphertext) {
    try {
      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) {
    return new String(decryptInBytes(ciphertext));
  }
 
 
  /**
   * Cette méthode permet de tester le bon fonctionnement des autres.
   */
  public static void main(String[] args) {
    String plaintext = args[0];
    System.out.println("plaintext = " + plaintext);
    MyBlowfish bf = new MyBlowfish();
    bf.generateKey();
    byte[] secretKey = bf.getSecretKeyInBytes();
    byte[] ciphertext = bf.crypt(plaintext);   
    System.out.println("ciphertext = " + new BigInteger(ciphertext));
 
    bf.setSecretKey(secretKey);
    String plaintext2 = bf.decryptInString(ciphertext);
    System.out.println("plaintext2 = " + plaintext2);
    if (!plaintext2.equals(plaintext)) System.out.println("Error: plaintext2 != plaintext");
  }
}
Merci d'avance...