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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| public class Cryptage
{
/**
* Clé de cryptage
*/
private Key clef,secretKey;
/**
* Mot de passe permettant de créer la clé de cryptage
*/
private String cle;
/**
* Mot de passe décomposé en tableau de byte
*/
private byte[] passwordInBytes;
/**
* Constructeur qui va créer la clé de cryptage
*/
public Cryptage(String cle)
{
try
{
/**
* encodage avec ISO-8859-2 qui permet de traiter les langues dites "latines"
*/
passwordInBytes = cle.getBytes("ISO-8859-2");
/**
* algo de cryptage : Blowfish (le plus souvent utilisé)
*/
clef=new SecretKeySpec(passwordInBytes,"Blowfish");
}
catch (UnsupportedEncodingException e)
{
new LogException("../log/serveurException.log","serveur","58","58","58",e.toString()).enregistrementLog();
}
}
/**
* méthde retournant le mot de passe sous forme de tableau de byte
* @return
*/
public byte[] getSecretKeyInBites()
{
return clef.getEncoded();
}
/**
* Méthode permettant de crypter une chaine de caractères
* Elle va retourner un tableau de byte qui va représenter le cryptage de cette chaine de caractères
*
* Cette méhode va transformer la chaine de caractères en tableau de byte puis appeler la méthode
* cryptageByte
* @param txt = chaine de caractère à crypter
* @return
*/
public byte[] cryptageString(String txt)
{
return cryptageByte(txt.getBytes());
}
/**
* Méthode permettant de crypter un tableau de byte
* Elle va retourner un tableau de byte crypté à l'aide de l'algo de Blowfish et de la clé de cryptage
* @param txt
* @return
*/
public byte[] cryptageByte(byte[] txt)
{
try
{
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE,clef);
return cipher.doFinal(txt);
}
catch (Exception e)
{
new LogException("../log/serveurException.log","serveur","58","58","59",e.toString()).enregistrementLog();
return null;
}
}
/**
* Méthode permettant de décrypter un tableau de byte
* @param txt= tableau de byte à décrypter
* @return tableau de byte décypté
*/
public byte[] decryptageInByte(byte[] txt)
{
try
{
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE,clef);
return cipher.doFinal(txt);
}
catch (Exception e)
{
new LogException("../log/serveurException.log","serveur","58","58","60",e.toString()).enregistrementLog();
return null;
}
}
/**
* Cette méthode permet de décrypter un tableau de byte (en appelant la méthopde decryptageInByte) et
* de transformer le tableau de byte en chaine de caractère
* @param txt
* @return
*/
public String decryptageInString(byte[] txt)
{
return new String(decryptageInByte(txt));
} |
Partager