8
8
import javax .crypto .KeyGenerator ;
9
9
import javax .crypto .SecretKeyFactory ;
10
10
import javax .crypto .SealedObject ;
11
+ import javax .crypto .spec .GCMParameterSpec ;
11
12
import javax .crypto .spec .IvParameterSpec ;
12
13
import javax .crypto .spec .PBEKeySpec ;
13
14
import javax .crypto .spec .SecretKeySpec ;
26
27
27
28
public class AESUtil {
28
29
29
- public static String encrypt (String algorithm , String input , SecretKey key , IvParameterSpec iv )
30
+ public static String encrypt (String algorithm , String input , SecretKey key , GCMParameterSpec iv )
30
31
throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException ,
31
32
InvalidKeyException , BadPaddingException , IllegalBlockSizeException {
32
33
Cipher cipher = Cipher .getInstance (algorithm );
@@ -36,7 +37,7 @@ public static String encrypt(String algorithm, String input, SecretKey key, IvPa
36
37
.encodeToString (cipherText );
37
38
}
38
39
39
- public static String decrypt (String algorithm , String cipherText , SecretKey key , IvParameterSpec iv )
40
+ public static String decrypt (String algorithm , String cipherText , SecretKey key , GCMParameterSpec iv )
40
41
throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException ,
41
42
InvalidKeyException , BadPaddingException , IllegalBlockSizeException {
42
43
Cipher cipher = Cipher .getInstance (algorithm );
@@ -62,13 +63,13 @@ public static SecretKey getKeyFromPassword(String password, String salt)
62
63
return secret ;
63
64
}
64
65
65
- public static IvParameterSpec generateIv () {
66
- byte [] iv = new byte [16 ];
66
+ public static GCMParameterSpec generateIv () {
67
+ byte [] iv = new byte [12 ];
67
68
new SecureRandom ().nextBytes (iv );
68
- return new IvParameterSpec ( iv );
69
+ return new GCMParameterSpec ( 128 , iv );
69
70
}
70
71
71
- public static void encryptFile (String algorithm , SecretKey key , IvParameterSpec iv ,
72
+ public static void encryptFile (String algorithm , SecretKey key , GCMParameterSpec iv ,
72
73
File inputFile , File outputFile ) throws IOException , NoSuchPaddingException ,
73
74
NoSuchAlgorithmException , InvalidAlgorithmParameterException , InvalidKeyException ,
74
75
BadPaddingException , IllegalBlockSizeException {
@@ -92,7 +93,7 @@ public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec
92
93
outputStream .close ();
93
94
}
94
95
95
- public static void decryptFile (String algorithm , SecretKey key , IvParameterSpec iv ,
96
+ public static void decryptFile (String algorithm , SecretKey key , GCMParameterSpec iv ,
96
97
File encryptedFile , File decryptedFile ) throws IOException , NoSuchPaddingException ,
97
98
NoSuchAlgorithmException , InvalidAlgorithmParameterException , InvalidKeyException ,
98
99
BadPaddingException , IllegalBlockSizeException {
@@ -117,7 +118,7 @@ public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec
117
118
}
118
119
119
120
public static SealedObject encryptObject (String algorithm , Serializable object , SecretKey key ,
120
- IvParameterSpec iv ) throws NoSuchPaddingException , NoSuchAlgorithmException ,
121
+ GCMParameterSpec iv ) throws NoSuchPaddingException , NoSuchAlgorithmException ,
121
122
InvalidAlgorithmParameterException , InvalidKeyException , IOException , IllegalBlockSizeException {
122
123
Cipher cipher = Cipher .getInstance (algorithm );
123
124
cipher .init (Cipher .ENCRYPT_MODE , key , iv );
@@ -126,7 +127,7 @@ public static SealedObject encryptObject(String algorithm, Serializable object,
126
127
}
127
128
128
129
public static Serializable decryptObject (String algorithm , SealedObject sealedObject , SecretKey key ,
129
- IvParameterSpec iv ) throws NoSuchPaddingException , NoSuchAlgorithmException ,
130
+ GCMParameterSpec iv ) throws NoSuchPaddingException , NoSuchAlgorithmException ,
130
131
InvalidAlgorithmParameterException , InvalidKeyException , ClassNotFoundException ,
131
132
BadPaddingException , IllegalBlockSizeException , IOException {
132
133
Cipher cipher = Cipher .getInstance (algorithm );
@@ -135,19 +136,19 @@ public static Serializable decryptObject(String algorithm, SealedObject sealedOb
135
136
return unsealObject ;
136
137
}
137
138
138
- public static String encryptPasswordBased (String plainText , SecretKey key , IvParameterSpec iv )
139
+ public static String encryptPasswordBased (String plainText , SecretKey key , GCMParameterSpec iv )
139
140
throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException ,
140
141
InvalidKeyException , BadPaddingException , IllegalBlockSizeException {
141
- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding " );
142
+ Cipher cipher = Cipher .getInstance ("AES/GCM/NoPadding " );
142
143
cipher .init (Cipher .ENCRYPT_MODE , key , iv );
143
144
return Base64 .getEncoder ()
144
145
.encodeToString (cipher .doFinal (plainText .getBytes ()));
145
146
}
146
147
147
- public static String decryptPasswordBased (String cipherText , SecretKey key , IvParameterSpec iv )
148
+ public static String decryptPasswordBased (String cipherText , SecretKey key , GCMParameterSpec iv )
148
149
throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException ,
149
150
InvalidKeyException , BadPaddingException , IllegalBlockSizeException {
150
- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5PADDING " );
151
+ Cipher cipher = Cipher .getInstance ("AES/GCM/NoPadding " );
151
152
cipher .init (Cipher .DECRYPT_MODE , key , iv );
152
153
return new String (cipher .doFinal (Base64 .getDecoder ()
153
154
.decode (cipherText )));
0 commit comments