Repository navigation
Expand file tree
/
Copy pathAES.java
More file actions
85 lines (78 loc) · 3.3 KB
/
Copy pathAES.java
File metadata and controls
85 lines (78 loc) · 3.3 KB
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
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.security.InvalidKeyException;
/**
* Classe permettant de tester le chiffrement et le dechiffrement avec AES.
* @author Cyril Rabat
* @version 23/10/2017
*/
public class ChiffrementAES {
/**
* Methode principale.
* @param args[0] cle de chiffrement de 16 caractères
* @param args[1] message que l'on veut chiffrer
*/
public static void main(String[] args) {
// Verification des arguments
if(args.length != 2) {
System.err.println("Utilisation :");
System.err.println(" java ChiffrementAES motDePasse message");
System.err.println(" où :");
System.err.println(" - motDePasse : mot de passe de 16 caractères");
System.err.println(" - message : message que l'on veut chiffrer");
System.exit(-1);
}
String motDePasse = args[0];
// Chiffrement du message
SecretKeySpec specification = new SecretKeySpec(motDePasse.getBytes(), "AES");
byte[] bytes = null;
try {
Cipher chiffreur = Cipher.getInstance("AES");
chiffreur.init(Cipher.ENCRYPT_MODE, specification);
bytes = chiffreur.doFinal(args[1].getBytes());
} catch(NoSuchAlgorithmException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(NoSuchPaddingException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(InvalidKeyException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(IllegalBlockSizeException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(BadPaddingException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
}
System.out.println("Message origine : " + args[1]);
System.out.println("Message chiffré : " + new String(bytes));
// Dechiffrement du message
try {
Cipher dechiffreur = Cipher.getInstance("AES");
dechiffreur.init(Cipher.DECRYPT_MODE, specification);
bytes = dechiffreur.doFinal(bytes);
} catch(NoSuchAlgorithmException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(NoSuchPaddingException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(InvalidKeyException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(IllegalBlockSizeException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
} catch(BadPaddingException e) {
System.err.println("Erreur lors du chiffrement : " + e);
System.exit(-1);
}
System.out.println("Message déchiffré : " + new String(bytes));
}
}