Bonjour,

J'essai de créer mon propre outil de génération et de lecture de keystore.
Jusque là je pensais avoir réussi : mon keystore est bien lu par l'outil et par keytool.

Mais dans l'application que j'utilise le fichier semble "corrompu", alors qu'une autre classe avec les paramètres en dur, le keystore généré est fonctionnel

L'erreur est "unable to fetch keystore"

Voici un extrait des 2 classes :

Celle qui ne génère pas un keystore fonctionnel et prend en arguments :

-genkey -alias1 -unecledecryptage128bits -alias1 -AES -128 -keystore.jceks -JCEKS -password
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
public static void main(String[] args) {
	  if (args.length > 2){
		  if (args[0].substring(1).equals("genkey")){
			  keyAlias = args[1].substring(1);
			  key = args[2].substring(1);
			  passwordKey = args[3].substring(1).toCharArray();
			  keyAlgorithm = args[4].substring(1);
			  keySize = Integer.parseInt(args[5].substring(1));
			  keystoreFile = args[6].substring(1);
			  keystoreType = args[7].substring(1);
			  passwordKeyStore = args[8].substring(1).toCharArray();		  
 
			 generateKeystore(passwordKeyStore, passwordKey);
		  }else if (args[0].substring(1).equals("list")){
			  keystoreFile = args[1].substring(1);
			  keystoreType = args[2].substring(1);
			  char[] passwordKeyStore = args[3].substring(1).toCharArray();
			  list(passwordKeyStore);
		  }else System.exit(1);
...
 public static void generateKeystore(char[] keystorePassword, char[] keyPassword) {
 
	  try {		  
		  System.out.println("keystorePassword " + new String(keystorePassword)+":");
		  System.out.println("keyPassword " + new String(keyPassword)+":");
		  System.out.println("keystoreType " + keystoreType+":");
		  System.out.println("keyAlias " + keyAlias+":");
		  System.out.println("keySize " + keySize+":");
		  System.out.println("algorithm " + keyAlgorithm+":");
		  System.out.println("keystoreFile " + keystoreFile+":");
		  System.out.println("key " + key+":");
 
 
		  KeyStore store;
 
		  store = KeyStore.getInstance(keystoreType);
		  store.load(null, null);
 
		  /*if (new File(keystoreFile).exists()){
    	  FileInputStream in = new FileInputStream(keystoreFile);
    	  store.load(in,keystorePassword);
    	  in.close();
      }*/
 
		  System.out.print("Generating secret key (" + keySize + " bits) for " + keyAlgorithm + " algorithm... ");
 
		  //byte[] raw = Base64.decodeBase64(key);
 
		  BASE64Decoder bd = new BASE64Decoder();
		  byte raw[] = bd.decodeBuffer(key);
		  SecretKeySpec skey = new SecretKeySpec(raw, "AES");
 
		  System.out.println("Done.");
 
		  System.out.print("Generating Java KeyStore: " + keystoreFile + "... ");
 
		  KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(skey);
 
		  store.setEntry(keyAlias, entry, new KeyStore.PasswordProtection(keyPassword));
 
		  // Save keystore to filesystem
		  FileOutputStream fos = new java.io.FileOutputStream(keystoreFile);
		  store.store(fos, keystorePassword);
 
		  fos.flush();
		  fos.close();
 
		  System.out.println("Done.");
 
	  } catch (KeyStoreException e) {
      System.out.println("Error: Keystore error : " + keystoreType);
      e.printStackTrace();
      System.exit(1);
    } catch (NoSuchAlgorithmException e) {
      System.out.println("Error: Unsupported algorithm : " + keyAlgorithm);
      e.printStackTrace();
      System.exit(1);
    } catch (CertificateException e) {
      System.out.println("Error: Certificate error.");
      e.printStackTrace();
      System.exit(1);
    } catch (IOException e) {
      System.out.println("Error: I/O error when generating keystore : " + keystoreFile);
      e.printStackTrace();
      System.exit(1);
    }
  }

Ici la classe brut qui marche, donc pas d'arguments :

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
 
          private static String keystoreFile="keystore.jceks";
	  private static String keyAlias="alias1";
	  private static String key="unecledecryptage128bits " ;
	  private static String keyAlgorithm="AES";
	  private static int keySize=128;
 
public static void main(String[] args) {
 
char[] passwordKeyStore = "password".toCharArray();
char[] passwordKey ="alias1".toCharArray();
 
generateKeystore(passwordKeyStore, passwordKey);
 
public static  void generateKeystore(char[] keystorePassword, char[] keyPassword) {
 
			  try {
				  parseConfig();
				  KeyStore store;
 
				  store = KeyStore.getInstance("jceks");
				  store.load(null, null);
 
				  System.out.println("Generating secret key (" + keySize + " bits) for " + keyAlgorithm + " algorithm... ");
 
				  //////////////////////////////////////////////////////////////////////////////////////////////
 
				  System.out.print((new StringBuilder("Generating secret key (")).append(keySize).append(" bits) for ").append(keyAlgorithm).append(" algorithm... ").toString());
				  BASE64Decoder bd = new BASE64Decoder();
				  byte raw[] = bd.decodeBuffer(key);
				  SecretKeySpec skey = new SecretKeySpec(raw, "AES");
				  System.out.println("Done.");
				  System.out.print((new StringBuilder("Generating Java KeyStore: ")).append(keystoreFile).append("... ").toString());
				  java.security.KeyStore.SecretKeyEntry entry = new java.security.KeyStore.SecretKeyEntry(skey);
				  store.setEntry(keyAlias, entry, new java.security.KeyStore.PasswordProtection(keyPassword));
				  FileOutputStream fos = new FileOutputStream(keystoreFile);
				  store.store(fos, keystorePassword);
 
				  fos.flush();
				  fos.close();
 
			      System.out.println("Done.");
 
			    } catch (KeyStoreException e) {
			      System.out.println("Error: Keystore error.");
			      e.printStackTrace();
			      System.exit(1);
			    } catch (NoSuchAlgorithmException e) {
			      System.out.println("Error: Unsupported algorithm.");
			      e.printStackTrace();
			      System.exit(1);
			    } catch (CertificateException e) {
			      System.out.println("Error: Certificate error.");
			      e.printStackTrace();
			      System.exit(1);
			    } catch (IOException e) {
			      System.out.println("Error: I/O error when generating keystore.");
			      e.printStackTrace();
			      System.exit(1);
			    }
 
			  }
Les deux classes sont compilées sous Unix et les paramètres sont vérifiés ils semblent être identiques

Je sèche complètement, je ne sais pas pourquoi le comportement diffère autant.

Merci de votre aide