Cryptage et decryptage java
Bonjour,
J'ai actuellement un soucis.
J'ai une application qui se génére à partir d'un fichier xml. Pour la sécuriser, j'ai décidé de crypter ce fichier XML j'ai utilisé la classe suivante
Code:
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
|
package Cryptage;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.Key;
import java.util.Vector;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* @author xavier
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Cryptage
{
private Key clef;
private String cle;
private byte[] passwordInBytes;
public Cryptage(String cle)
{
try
{
//passwordInBytes = cle.getBytes("ISO-8859-2");
//passwordInBytes = cle.getBytes("UTF8");
passwordInBytes = cle.getBytes("ISO-8859-2");
clef=new SecretKeySpec(passwordInBytes,"Blowfish");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public byte[] getSecretKeyInBites()
{
return clef.getEncoded();
}
public byte[] cryptageString(String txt)
{
return cryptageByte(txt.getBytes());
}
public byte[] cryptageByte(byte[] txt)
{
try
{
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE,clef);
return cipher.doFinal(txt);
}
catch (Exception e)
{
return null;
}
}
public byte[] decryptageInByte(byte[] txt)
{
try
{
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE,clef);
return cipher.doFinal(txt);
}
catch (Exception e)
{
System.out.println(e);
return null;
}
}
public String decryptageInString(byte[] txt)
{
return new String(decryptageInByte(txt));
}
} |
Cette classe marche parfaitement on peut crypter decrypter sans probleme.
Ensuite j'ai fait une application pour crypter mon fichier XML. Suite à un affichage dans la console et l'enregistrement du resultat dans un fichier. Je peux affirmer que l'application a marché.
MAIS QUAND JE RELANCE MON APPLICATION:
qui doit decrypter se fichier et ensuite créer un fichier temporaire j'ai le message d'erreur suivant :
Code:
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes
Je ne sais plus quoi chercher. Si vous avez une solution je suis preneur.
Merci d'avance Xavier
galerie virtuelle