@@ -22,7 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2222import javax .crypto .Cipher ;
2323import javax .crypto .NoSuchPaddingException ;
2424import javax .crypto .SecretKeyFactory ;
25- import javax .crypto .spec .IvParameterSpec ;
25+ import javax .crypto .spec .GCMParameterSpec ;
2626import javax .crypto .spec .PBEKeySpec ;
2727import javax .crypto .spec .SecretKeySpec ;
2828
@@ -49,7 +49,7 @@ public class PBECipher {
4949 protected static final int SALT_SIZE = 8 ;
5050 protected static final int CHUNK_SIZE = 16 ;
5151 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 " ;
5353 protected static final int PBE_ITERATIONS = 310000 ;
5454 private static final SecureRandom _secureRandom = new SecureRandom ();
5555
@@ -84,7 +84,8 @@ public String encrypt64(final String clearText, final String password) throws Pl
8484
8585 allEncryptedBytes [SALT_SIZE ] = padLen ;
8686
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 );
8889
8990 return Base64 .getEncoder ().encodeToString (allEncryptedBytes );
9091 } catch (Exception e ) {
@@ -105,9 +106,12 @@ public String decrypt64(final String encryptedText, final String password) throw
105106
106107 byte padLen = allEncryptedBytes [SALT_SIZE ];
107108
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 );
109111
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 );
111115
112116 Cipher cipher = createCipher (password .toCharArray (), salt , Cipher .DECRYPT_MODE );
113117
@@ -129,15 +133,15 @@ private Cipher createCipher(final char[] pwd, byte[] salt, final int mode)
129133
130134 byte [] key = new byte [SPICE_SIZE ];
131135
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
133138
134139 System .arraycopy (keyAndIv , 0 , key , 0 , key .length );
135140
136- System .arraycopy (keyAndIv , key .length , iv , 0 , iv .length );
137-
138141 Cipher cipher = Cipher .getInstance (CIPHER_ALG );
139142
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 );
141145
142146 return cipher ;
143147 }
0 commit comments