[Java] Demande aide pour chiffrage

ShE3py

Enbogueuse
Support
26 Septembre 2015
4 129
162
461
247
21
Mìlhüsa
Bonjour,

Je tente de faire une connexion chiffré entre un serveur et un client, voici comment sa se passe :
Quand un joueur se connecte, sa créer la clé de chiffrage :
(getNameToWrite() renvoie l'adresse et le port du client, ex: 127.0.0.1:1234)
(kick() ferme le socket)

Code:
   private SecretKey createSecretKey() {
     System.out.println("Creating secret key for " + getNameToWrite() + "...");
     KeyGenerator gen = null;
     try {
       en = KeyGenerator.getInstance("DESede");
     } catch(NoSuchAlgorithmException e) {
       e.printStackTrace();
       System.err.println("Unknown algorithm [" + getNameToWrite() + "].");
       kick();
     }
     gen.init(168);
     System.out.println("Key generate.");
     
     return gen.generateKey();
   }

Après, sa lui envoie la clé wrappé :
Code:
   private String wrap(SecretKey key) {
     Cipher cipher = getCipher(key, Cipher.WRAP_MODE);
   
     byte[] wrappedKey = null;
     try {
       wrappedKey = cipher.wrap(key);
     } catch(InvalidKeyException e) {
       e.printStackTrace();
       System.err.println("Invalid key for " + getNameToWrite() + ".");
       kick();
     } catch(IllegalBlockSizeException e) {
       e.printStackTrace();
       System.err.println("Illegal block size for " + getNameToWrite() + ".");
       kick();
     }
   
     List<Byte> bytes = new ArrayList<Byte>();
     for(byte b : wrappedKey) bytes.add(b);
   
     String wrappedStr = String.valueOf(bytes);
   
     return wrappedStr;
   }
 
   private Cipher cipher = null;
   private Cipher getCipher(SecretKey key, int opmode) {
     try {
       if(cipher == null) cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
     } catch(NoSuchAlgorithmException e) {
       e.printStackTrace();
       System.err.println("Unkown algorithm [" + getNameToWrite() + "].");
       kick();
     } catch(NoSuchPaddingException e) {
       e.printStackTrace();
       System.err.println("No such padding [" + getNameToWrite() + "].");
       kick();
     }
   
     try {
       cipher.init(opmode, key);
     } catch(InvalidKeyException e) {
       e.printStackTrace();
       System.err.println("Invalid key [" + getNameToWrite() + "].");
       kick();
     }
   
     return cipher;
   }

Côté client : quand il reçoit le message :
(wrappedKey est un 'String' qui est la clé wrappé)
(out est un 'PrintWriter' qui symbolise l'envoie au serveur)

Code:
   SecretKey key = unwrap(wrappedKey);
   out.println(encrypt(key, "test !"));
   out.flush();
 
   private static SecretKey unwrap(String wrappedKey) {
     KeyGenerator gen = null;
     try {
       gen = KeyGenerator.getInstance("DESede");
     } catch(NoSuchAlgorithmException e) {
       e.printStackTrace();
       System.err.println("Unknown algorithm.");
       Incarnation.exit(1);
     }
   
     gen.init(168);
   
     SecretKey key = gen.generateKey();
     Cipher cipher = getCipher(key, Cipher.UNWRAP_MODE);
   
   
     // Conversion du String en byte[]
     List<Byte> bytes = new ArrayList<Byte>();
     String wrapped = wrappedKey;
     wrapped = wrapped.substring(1);
     wrapped = wrapped.substring(0, wrapped.length() - 1);
     for(String str : wrapped.split(", ")) bytes.add(Byte.valueOf(str));
   
     byte[] bytesArray = new byte[bytes.size()];
     for(int b = 0; b < bytes.size(); b++) bytesArray[b] = bytes.get(b);
   
     SecretKey unwrappedKey = null;
     try {
       unwrappedKey = (SecretKey) cipher.unwrap(bytesArray, "DESede", Cipher.SECRET_KEY);
     } catch(InvalidKeyException e) {
       e.printStackTrace();
       System.err.println("Invalid key.");
       Incarnation.exit(1);
     } catch(NoSuchAlgorithmException e) {
       e.printStackTrace();
       System.err.println("No such algorithm.");
       Incarnation.exit(1);
     }

     return unwrappedKey;
   }

et il crash au cipher.unwrap() :
Code:
java.security.InvalidKeyException: The wrapped key is not padded correctly
   at com.sun.crypto.provider.CipherCore.unwrap(CipherCore.java:1095)
   at com.sun.crypto.provider.DESedeCipher.engineUnwrap(DESedeCipher.java:423)
   at javax.crypto.Cipher.unwrap(Cipher.java:2550)
   at fr.she3py.games.incarnation.ServerConnection.unwrap(ServerConnection.java:152)
   at fr.she3py.games.incarnation.ServerConnection.connect(ServerConnection.java:72)
   at fr.she3py.games.incarnation.ServerConnection.main(ServerConnection.java:35)

Le 'byte[]' est identique, même taille (32), même valeures.
J'ai essaie en AES et en désactivant le padding.

Merci d'avoir lu(e),
ShE3py.
 
Dernière édition par un modérateur: