@@ -22,7 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
22
22
import javax .crypto .Cipher ;
23
23
import javax .crypto .NoSuchPaddingException ;
24
24
import javax .crypto .SecretKeyFactory ;
25
- import javax .crypto .spec .IvParameterSpec ;
25
+ import javax .crypto .spec .GCMParameterSpec ;
26
26
import javax .crypto .spec .PBEKeySpec ;
27
27
import javax .crypto .spec .SecretKeySpec ;
28
28
@@ -49,7 +49,7 @@ public class PBECipher {
49
49
protected static final int SALT_SIZE = 8 ;
50
50
protected static final int CHUNK_SIZE = 16 ;
51
51
protected static final String KEY_ALG = "AES" ;
52
- protected static final String CIPHER_ALG = "AES/CBC/PKCS5Padding " ;
52
+ protected static final String CIPHER_ALG = "AES/GCM/NoPadding " ;
53
53
protected static final int PBE_ITERATIONS = 310000 ;
54
54
private static final SecureRandom _secureRandom = new SecureRandom ();
55
55
@@ -84,7 +84,8 @@ public String encrypt64(final String clearText, final String password) throws Pl
84
84
85
85
allEncryptedBytes [SALT_SIZE ] = padLen ;
86
86
87
- System .arraycopy (encryptedBytes , 0 , allEncryptedBytes , SALT_SIZE + 1 , len );
87
+ System .arraycopy (iv , 0 , allEncryptedBytes , SALT_SIZE + 1 , iv .length );
88
+ System .arraycopy (encryptedBytes , 0 , allEncryptedBytes , SALT_SIZE + 1 + iv .length , len );
88
89
89
90
return Base64 .getEncoder ().encodeToString (allEncryptedBytes );
90
91
} catch (Exception e ) {
@@ -105,9 +106,12 @@ public String decrypt64(final String encryptedText, final String password) throw
105
106
106
107
byte padLen = allEncryptedBytes [SALT_SIZE ];
107
108
108
- byte [] encryptedBytes = new byte [totalLen - SALT_SIZE - 1 - padLen ];
109
+ byte [] iv = new byte [12 ]; // GCM standard nonce size
110
+ System .arraycopy (allEncryptedBytes , SALT_SIZE + 1 , iv , 0 , iv .length );
109
111
110
- System .arraycopy (allEncryptedBytes , SALT_SIZE + 1 , encryptedBytes , 0 , encryptedBytes .length );
112
+ byte [] encryptedBytes = new byte [totalLen - SALT_SIZE - 1 - iv .length ];
113
+
114
+ System .arraycopy (allEncryptedBytes , SALT_SIZE + 1 + iv .length , encryptedBytes , 0 , encryptedBytes .length );
111
115
112
116
Cipher cipher = createCipher (password .toCharArray (), salt , Cipher .DECRYPT_MODE );
113
117
@@ -129,15 +133,15 @@ private Cipher createCipher(final char[] pwd, byte[] salt, final int mode)
129
133
130
134
byte [] key = new byte [SPICE_SIZE ];
131
135
132
- byte [] iv = new byte [SPICE_SIZE ];
136
+ byte [] iv = new byte [12 ]; // GCM standard nonce size
137
+ _secureRandom .nextBytes (iv ); // Generate a random nonce
133
138
134
139
System .arraycopy (keyAndIv , 0 , key , 0 , key .length );
135
140
136
- System .arraycopy (keyAndIv , key .length , iv , 0 , iv .length );
137
-
138
141
Cipher cipher = Cipher .getInstance (CIPHER_ALG );
139
142
140
- cipher .init (mode , new SecretKeySpec (key , KEY_ALG ), new IvParameterSpec (iv ));
143
+ GCMParameterSpec gcmSpec = new GCMParameterSpec (128 , iv ); // 128-bit authentication tag length
144
+ cipher .init (mode , new SecretKeySpec (key , KEY_ALG ), gcmSpec );
141
145
142
146
return cipher ;
143
147
}
0 commit comments