Bonsoir,
J'ai effectué quelques recherches sur le forum , et même si il y avait déjà des posts se référant a se sujet, je n'y est pas trouvé se que j'y souhaité.
Je dois générer une clée AES aléatoire de 256 bits puis encrypter 128 zéros avec cette clé.
J'ai deux questions :
1 ) Es que ce bout de code génère bien une clé de 256 bits
puisqu'a l'affichage de la clé : j'obtiens ==>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); kgen.init(random); // random = 256 ? SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); byte[] secretKeyAES = skeySpec.getEncoded();
KEY : 2321cd52a8e92fbac8020cbeff27be41
2) Si j'obtiens bien une clé 256 bits, comment se fait-il que les 128 zeros encrypté face plus de 128 bits alors que je devrais obtenir ciphertext de 128 bits...
A l'affichage, j'obtiens ceci ==>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public static byte[] cryptWithAES(byte[] text, byte[] bytesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytesKey, "AES")); byte[] encrypted = cipher.doFinal(text); return encrypted; } public static byte[] decryptInBytesWithAES(byte[] ciphertext, byte[] bytesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(bytesKey, "AES")); byte[] decrypted = cipher.doFinal(ciphertext); return decrypted; } myCryptoProg() throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { byte[] secretKeyAES = GenerateAESKey(); String myAESKey = HexaKey(secretKeyAES); System.out.println("KEY : "+ myAESKey); String num = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; System.out.println("Text : " + num ); byte[] encrypted = cryptWithAES(num.getBytes(), secretKeyAES); byte[] decrypted = decryptInBytesWithAES(encrypted, secretKeyAES); System.out.println("Encrypt : " + HexaKey(encrypted)); System.out.println("Decrypt : " + new String(decrypted)); }
Text : 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Encrypt : 3dc3be323f1dfd9a0471ab8e3bce69983dc3be323f1dfd9a0471ab8e3bce69983dc3be323f1dfd9a0471ab8e3bce69983dc3be323f1dfd9a0471ab8e3bce69983dc
3be323f1dfd9a0471ab8e3bce69983dc3be323f1dfd9a0471ab8e3bce69983dc3be323f1dfd9a0471ab8e3bce69983dc3be323f1dfd9a0471ab8e3bce6998ba2ff22
a7291a41a21d76267603d0b65
Decrypt : 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Merci d'avance.
JacKsoN
Partager