disparition! Gérard Majax 8 octets dans un Stream

disparition! Gérard Majax 8 octets dans un Stream - Java - Programmation

Marsh Posté le 03-12-2004 à 07:31:16    

Je suis en train de faire mumuse avec une servlet qui permet de crypter/décrypter en 3DES à la volée ..
tout fonctionne impec .. sauuuf, qu'un mystère subsiste.
 
mes fonctions suivantes me font toujours disparaître les 8 derniers octets de mon stream ??
 
encode :

Code :
  1. public byte[] perform(byte[] code, String key) throws InvalidKeyException, IOException {
  2.  //byte[] cle = key.getBytes();
  3.  byte[] cle = (new String("ma clé sur 24 caractères" )).getBytes(); // 24 caractères
  4.  generateKey(cle);
  5.  c.init(Cipher.ENCRYPT_MODE, this.key);
  6.  ByteArrayOutputStream os = new ByteArrayOutputStream();
  7.  CipherOutputStream output = new CipherOutputStream(os,c);
  8.  output.write(code);
  9.  output.flush();
  10.  System.out.println("encode:avant, code:"+code.length+" octets,"+code);
  11.  code = os.toByteArray();
  12.  System.out.println("encode:après, code:"+code.length+" octets,"+code);
  13.  output.close();
  14.  os.close();
  15.  return code;
  16. }


 
décode :

Code :
  1. public byte[] perform(byte[] code, String key) throws InvalidKeyException, IOException {
  2. //  byte[] cle = key.getBytes();
  3.  byte[] cle = (new String("ma clé sur 24 caractères" )).getBytes(); // 24 caractères
  4.  generateKey(cle);
  5.  c.init(Cipher.DECRYPT_MODE, this.key);
  6.  ByteArrayInputStream is = new ByteArrayInputStream(code);
  7.  CipherInputStream input = new CipherInputStream(is,c);
  8.  int read=0;
  9.  int pos=0;
  10.  int taille = code.length;
  11.  input.skip(taille);
  12.  System.out.println("decode:avant, code:"+code.length+" octets,"+code);
  13.  while((read=input.read()) > 0) {
  14.   code[pos]=(byte)read;
  15.   pos++;
  16.  }
  17.  System.out.println("decode:apres, code:"+code.length+" octets,"+code);
  18.  is.close();
  19.  return code;
  20. }


 
Si quelqu'un a déjà rencontrer ce problème ... :(

Reply

Marsh Posté le 03-12-2004 à 07:31:16   

Reply

Marsh Posté le 03-12-2004 à 08:08:04    

après calcul mental, c'est 6 octets en fait :p

Reply

Marsh Posté le 03-12-2004 à 11:23:44    

Code :
  1. /*
  2. * Created on 3 déc. 2004
  3. *
  4. * TODO To change the template for this generated file go to
  5. * Window - Preferences - Java - Code Style - Code Templates
  6. */
  7. package test;
  8. import java.security.InvalidKeyException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.spec.InvalidKeySpecException;
  11. import javax.crypto.BadPaddingException;
  12. import javax.crypto.Cipher;
  13. import javax.crypto.IllegalBlockSizeException;
  14. import javax.crypto.NoSuchPaddingException;
  15. import javax.crypto.SecretKey;
  16. import javax.crypto.SecretKeyFactory;
  17. import javax.crypto.spec.DESedeKeySpec;
  18. /**
  19. * @author nfs
  20. *
  21. * TODO To change the template for this generated type comment go to
  22. * Window - Preferences - Java - Code Style - Code Templates
  23. */
  24. public class TestClass {
  25. private Cipher c = null;
  26. private SecretKey key = null;
  27. public static void main(String[] args) {
  28.  try {
  29.   TestClass test = new TestClass();
  30.   byte[] cadeau = test.encode((new String("test test" )).getBytes(),"" );
  31.   byte[] resultat = test.decode(cadeau,"" );
  32.   System.out.println(new String(resultat));
  33.  }catch(Exception e) {
  34.   e.printStackTrace();
  35.  }
  36. }
  37. public TestClass() {
  38.  try {
  39.   c = Cipher.getInstance("DESede" );
  40.  } catch (NoSuchAlgorithmException e) {
  41.   e.printStackTrace();
  42.  } catch (NoSuchPaddingException e) {
  43.   e.printStackTrace();
  44.  }
  45. }
  46. private void generateKey(byte[] cle) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
  47.  DESedeKeySpec keyspec = new DESedeKeySpec(cle);
  48.  SecretKeyFactory desEdeFactory = SecretKeyFactory.getInstance("DESede" );
  49.  key = desEdeFactory.generateSecret(keyspec);
  50. }
  51. public byte[] encode(byte[] code, String key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
  52.  //byte[] cle = key.getBytes();
  53.  byte[] cle = (new String("ma clé sur 24 caractères" )).getBytes(); // 24 caractères
  54.  generateKey(cle);
  55.  c.init(Cipher.ENCRYPT_MODE, this.key);
  56.  return c.doFinal(code);
  57. }
  58. public byte[] decode(byte[] code, String key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
  59.  //        byte[] cle = key.getBytes();
  60.  byte[] cle = (new String("ma clé sur 24 caractères" )).getBytes(); // 24 caractères
  61.  generateKey(cle);
  62.  c.init(Cipher.DECRYPT_MODE, this.key);
  63.  return c.doFinal(code);
  64. }
  65. }


 
version light et ça marche très bien.

Reply

Marsh Posté le 29-12-2004 à 08:25:50    

bon je reviens à la charge parce qu'apparement j'ai le même soucis sur un autre bout de code utilisant un CipherOutputStream ... :(
quelqu'un l'a déjà utilisé?

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed