Interface java
Fermé
mana
-
16 mai 2015 à 18:02
ElementW Messages postés 4764 Date d'inscription dimanche 12 juin 2011 Statut Contributeur Dernière intervention 5 octobre 2021 - 3 juin 2015 à 22:45
ElementW Messages postés 4764 Date d'inscription dimanche 12 juin 2011 Statut Contributeur Dernière intervention 5 octobre 2021 - 3 juin 2015 à 22:45
A voir également:
- Interface java
- Common interface 5v only ✓ - Forum Vidéo/TV
- Jeux java itel ✓ - Forum Jeux vidéo
- Java runtime - Télécharger - Langages
- Java apk - Télécharger - Langages
- Scanf en java ✓ - Forum Java
1 réponse
gpotter2
Messages postés
17
Date d'inscription
mardi 19 mai 2015
Statut
Membre
Dernière intervention
11 août 2015
20 mai 2015 à 12:48
20 mai 2015 à 12:48
Bonjour !
Pour le chiffrement, t'as deux options:
- réversible
- irréversible (utilise md5)
Une bonne option pour le réversible, c'est combiner du AES et du SHA-1, ce qui donne:
Ensuite, tu prends une petite classe du genre:
Note importante:
Le Base64 doit être importé (TonProjet=>Propriété=>Java Build Path => Libraries => Add externals Jars ==> commons-codec.jar)
Télécharger ici:
http://commons.apache.org/proper/commons-codec/download_codec.cgi
Pour le chiffrement, t'as deux options:
- réversible
- irréversible (utilise md5)
Une bonne option pour le réversible, c'est combiner du AES et du SHA-1, ce qui donne:
package main;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.[/contents/94-codage-base64 Base64];
/**
Aes encryption
*/
public class UtilAES {
private static SecretKeySpec secretKey ;
private static byte[] key ;
public UtilAES(String key){
setKey(key);
}
private void setKey(String myKey){
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public String encode(String strToEncrypt) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: "+e.toString());
}
return null;
}
public String decode(String strToDecrypt) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: "+e.toString());
}
return null;
}
}
Ensuite, tu prends une petite classe du genre:
public class Main {
public static void main(String[] args) {
String a_crypter = "Bonjour !";
String crypte = new UtilAES("cle_de_chiffrement").encode(a_crypter);
System.out.println(a_crypter + " ==> " + crypte);
}
Note importante:
Le Base64 doit être importé (TonProjet=>Propriété=>Java Build Path => Libraries => Add externals Jars ==> commons-codec.jar)
Télécharger ici:
http://commons.apache.org/proper/commons-codec/download_codec.cgi
20 mai 2015 à 12:54
Même si, bon, dans le cas présent je pense que ça n'importe peu, mais par principe il vaut mieux les éviter.
3 juin 2015 à 19:24
https://www.commentcamarche.net/faq/4470-mythes-md5-a-ete-casse
:)
3 juin 2015 à 22:45