1
1
package com .example .crypto .artifacts ;
2
+
3
+ import java .security .*;
2
4
import javax .crypto .Cipher ;
3
5
import javax .crypto .KeyGenerator ;
4
6
import javax .crypto .SecretKey ;
5
7
import javax .crypto .spec .IvParameterSpec ;
6
- import javax .crypto .spec .GCMParameterSpec ;
7
- import java .security .*;
8
- import java .util .Base64 ;
9
- import java .util .Properties ;
10
- import java .util .Random ;
11
- import java .io .FileInputStream ;
12
- import java .io .IOException ;
13
- import java .util .Arrays ;
14
8
15
9
public class Test {
16
10
17
- public static SecretKey generateAESKey ()throws Exception {
11
+ public static SecretKey generateAESKey () throws Exception {
18
12
KeyGenerator keyGen = KeyGenerator .getInstance ("AES" );
19
13
keyGen .init (256 );
20
14
return keyGen .generateKey ();
21
15
}
22
16
23
-
24
- private static byte [] getRandomWrapper1 ()throws Exception {
17
+ private static byte [] getRandomWrapper1 () throws Exception {
25
18
byte [] val = new byte [16 ];
26
19
new SecureRandom ().nextBytes (val );
27
20
return val ;
28
21
}
29
22
30
- private static byte [] getRandomWrapper2A ()throws Exception {
23
+ private static byte [] getRandomWrapper2A () throws Exception {
31
24
byte [] val ;
32
- val = getRandomWrapper1 ();
25
+ val = getRandomWrapper1 ();
33
26
funcA1 (val );
34
27
return val ;
35
28
}
36
29
37
- private static byte [] getRandomWrapper2b ()throws Exception {
30
+ private static byte [] getRandomWrapper2b () throws Exception {
38
31
byte [] val ;
39
- val = getRandomWrapper1 ();
32
+ val = getRandomWrapper1 ();
40
33
return val ;
41
34
}
42
35
43
- private static void funcA1 (byte [] iv )throws Exception {
36
+ private static void funcA1 (byte [] iv ) throws Exception {
44
37
IvParameterSpec ivSpec = new IvParameterSpec (iv );
45
38
Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
46
39
SecretKey key = generateAESKey ();
47
40
cipher .init (Cipher .ENCRYPT_MODE , key , ivSpec ); // BAD: Reuse of `iv` in funcB1
48
41
byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
49
42
}
50
43
51
- private static void funcB1 ()throws Exception {
44
+ private static void funcB1 () throws Exception {
52
45
byte [] iv = getRandomWrapper2A ();
53
46
IvParameterSpec ivSpec = new IvParameterSpec (iv );
54
47
Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
@@ -57,7 +50,7 @@ private static void funcB1()throws Exception {
57
50
byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
58
51
}
59
52
60
- private static void funcA2 ()throws Exception {
53
+ private static void funcA2 () throws Exception {
61
54
byte [] iv = getRandomWrapper2b ();
62
55
IvParameterSpec ivSpec = new IvParameterSpec (iv );
63
56
Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
@@ -66,7 +59,7 @@ private static void funcA2()throws Exception {
66
59
byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
67
60
}
68
61
69
- private static void funcB2 ()throws Exception {
62
+ private static void funcB2 () throws Exception {
70
63
byte [] iv = getRandomWrapper2b ();
71
64
IvParameterSpec ivSpec = new IvParameterSpec (iv );
72
65
Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
@@ -75,7 +68,6 @@ private static void funcB2()throws Exception {
75
68
byte [] ciphertext = cipher .doFinal ("Simple Test Data" .getBytes ());
76
69
}
77
70
78
-
79
71
private static void funcA3 () throws Exception {
80
72
byte [] iv = getRandomWrapper2b ();
81
73
IvParameterSpec ivSpec1 = new IvParameterSpec (iv );
@@ -91,16 +83,12 @@ private static void funcA3() throws Exception {
91
83
byte [] ciphertext2 = cipher2 .doFinal ("Simple Test Data" .getBytes ());
92
84
}
93
85
94
-
95
-
96
-
97
86
public static void main (String [] args ) {
98
- try {
87
+ try {
99
88
funcA2 ();
100
89
funcB1 ();
101
90
funcB2 ();
102
- }
103
- catch (Exception e ) {
91
+ } catch (Exception e ) {
104
92
e .printStackTrace ();
105
93
}
106
94
}
0 commit comments