Interface java

Fermé
mana - 16 mai 2015 à 18:02
ElementW Messages postés 4816 Date d'inscription dimanche 12 juin 2011 Statut Contributeur Dernière intervention 5 octobre 2021 - 3 juin 2015 à 22:45
Bonjour,

mes cher(e)s ami(e)s j'ai besoin de votre aide svpppp
je travail avec eclipse java ,je veux créer une interface dans laquelle je mets mes données aprés je fais appel aux algorithmes de chiffrement aprés je veux afficher le résultat de chiffrement a cotés des donneés comment puis je réaliser ça !
exmpl:
Nom kol (aprés chiffrement)
Merci bcccccp

A voir également:

1 réponse

Utilisateur anonyme
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:

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
0
ElementW Messages postés 4816 Date d'inscription dimanche 12 juin 2011 Statut Contributeur Dernière intervention 5 octobre 2021 1 225
20 mai 2015 à 12:54
Attention: le MD5 et le SHA-1 sont des algorithmes dont des faiblesses ont été prouvées concernant la génération de collisions. Il est donc préférable de ne pas s'en servir et de choisir un meilleur algorithme, SHA-2 ou SHA-3 par exemple.
Même si, bon, dans le cas présent je pense que ça n'importe peu, mais par principe il vaut mieux les éviter.
0
Utilisateur anonyme > ElementW Messages postés 4816 Date d'inscription dimanche 12 juin 2011 Statut Contributeur Dernière intervention 5 octobre 2021
3 juin 2015 à 19:24
Ah bon, je suis d'accord pour SHA-1 (on peut le remplacer par exemple, par SHA-256) mais sinon.....

https://www.commentcamarche.net/faq/4470-mythes-md5-a-ete-casse

:)
0
ElementW Messages postés 4816 Date d'inscription dimanche 12 juin 2011 Statut Contributeur Dernière intervention 5 octobre 2021 1 225 > Utilisateur anonyme
3 juin 2015 à 22:45
Sinon rien, ce que je dis est la même chose que cet article. Je n'ai jamais dit que le MD5/SHA1 ont été cassés.
0