#1 Le 15/07/2009, à 11:48
- haile_selassie
[Java] Cryptage DES : caractères accentués [Résolu]
Salut.
J'ai récupéré une classe java qui permet de crypter/décrypter des chaines de caractères, puis je l'ai arrangée à mon goût. Le seul soucis est que les caractères accentués ne sont pas décryptés (je suppose qu'ils ne sont donc pas cryptés).
Voici le code:
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class StringEncrypter
{
public static final String DES_ENCRYPTION_SCHEME = "DES";
public static final String DEFAULT_ENCRYPTION_KEY = "phrase de cryptage a changer";
private static KeySpec keySpec;
private static SecretKeyFactory keyFactory;
private static Cipher cipher;
private static final String UNICODE_FORMAT = "ISO-8859-2";
private static void init()
{
try
{
byte[] keyAsBytes = DEFAULT_ENCRYPTION_KEY.getBytes( UNICODE_FORMAT );
keySpec = new DESKeySpec( keyAsBytes );
keyFactory = SecretKeyFactory.getInstance(DES_ENCRYPTION_SCHEME);
cipher = Cipher.getInstance(DES_ENCRYPTION_SCHEME);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static String encrypt( String unencryptedString )
{
//on verifie que la chaine a crypter n'est pas vide ou nulle
if ( !(unencryptedString == null || unencryptedString.trim().length() == 0) ){
//on initialise les variables
init();
try
{
//on crypte la chaine de caracteres
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.ENCRYPT_MODE, key );
byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT );
byte[] ciphertext = cipher.doFinal( cleartext );
BASE64Encoder base64encoder = new BASE64Encoder();
return base64encoder.encode( ciphertext );
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}else
return null; //si la chaine a crypter est nulle ou vide, on retourne null
}
public static String decrypt( String encryptedString )
{
if ( !(encryptedString == null || encryptedString.trim().length() <= 0 )){
init();
try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.DECRYPT_MODE, key );
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] cleartext = base64decoder.decodeBuffer(encryptedString);
byte[] ciphertext = cipher.doFinal(cleartext);
return Outil.bytes2String(ciphertext);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}else
return null;
}
public static void main (String[] arg){
System.out.println(StringEncrypter.decrypt(StringEncrypter.encrypt("hello world")));
}
}
Je précise que j'ai essayé de changer la variable UNICODE_FORMAT par UTF-8 et ISO-8859-1, mais rien n'y fait :
quand je mets:
System.out.println(StringEncrypter.decrypt(StringEncrypter.encrypt("éàùè")));
Il me sort: "????".
Je précise également que j'ai essayé d'écrire le résultat dans un fichier, au cas où ça viendrait d'eclipse, mais ça ne donne rien de mieux.
Si quelqu'un a une solution à ça, elle serait la bienvenue
Merci.
Dernière modification par haile_selassie (Le 15/07/2009, à 13:52)
Hors ligne
#2 Le 15/07/2009, à 13:13
- alexduf
Re : [Java] Cryptage DES : caractères accentués [Résolu]
Bonjour,
C'est curieux, j'ai testé, et chez moi les accents fonctionnent.
Es-tu sûr que ce n'est pas la méthode Outil.bytes2String qui fait n'importe quoi ?
N'ayant pas le code de cette méthode, je l'ai remplacé par return new String(ciphertext); et je peux facilement convertir n'importe quel accent.
Hors ligne
#3 Le 15/07/2009, à 13:46
- alexduf
Re : [Java] Cryptage DES : caractères accentués [Résolu]
Pardon, c'est :
return new String(ciphertext, Charset.forName(UNICODE_FORMAT));
en plaçant
private static final String UNICODE_FORMAT = "UTF-8";
Hors ligne
#4 Le 15/07/2009, à 13:51
- haile_selassie
Re : [Java] Cryptage DES : caractères accentués [Résolu]
Effectivement, c'est ma méthode bytes2String qui rendait n'importe quoi. Elle était comme ça:
public static String bytes2String( byte[] bytes )
{
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++)
{
stringBuffer.append( (char) bytes[i] );
}
return stringBuffer.toString();
}
Ce n'était donc pas la bonne solution, et je ne connaissais pas la méthode que tu utilises pour transformer un tableau de bytes en String.
Merci
Hors ligne