7
7
import javax .crypto .spec .GCMParameterSpec ;
8
8
import javax .crypto .spec .IvParameterSpec ;
9
9
import javax .crypto .spec .SecretKeySpec ;
10
- import java .nio .charset .StandardCharsets ;
10
+ import java .nio .charset .Charset ;
11
11
import java .security .InvalidAlgorithmParameterException ;
12
12
import java .security .InvalidKeyException ;
13
13
import java .security .NoSuchAlgorithmException ;
14
14
import java .security .spec .AlgorithmParameterSpec ;
15
15
16
16
class AESCipher {
17
+ private static final Charset charset = Charset .forName ("UTF-8" );
17
18
private final SecretKeySpec secretKey ;
18
19
private final AlgorithmParameterSpec ivSpec ;
19
20
private final AESAlgorithm algorithm ;
@@ -34,14 +35,14 @@ class AESCipher {
34
35
35
36
String encrypt (String plainText ) throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException , InvalidKeyException , IllegalBlockSizeException , BadPaddingException {
36
37
Cipher cipher = getCipher (Cipher .ENCRYPT_MODE );
37
- byte [] cipherText = cipher .doFinal (plainText .getBytes (StandardCharsets . UTF_8 ));
38
+ byte [] cipherText = cipher .doFinal (plainText .getBytes (charset ));
38
39
return Base64Utils .encodeToString (cipherText );
39
40
}
40
41
41
42
String decrypt (String encryptedText ) throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException , InvalidKeyException , IllegalBlockSizeException , BadPaddingException {
42
43
Cipher cipher = getCipher (Cipher .DECRYPT_MODE );
43
44
byte [] cipherText = cipher .doFinal (Base64Utils .decode (encryptedText ));
44
- return new String (cipherText , StandardCharsets . UTF_8 );
45
+ return new String (cipherText , charset );
45
46
}
46
47
47
48
private Cipher getCipher (int opMode ) throws NoSuchPaddingException , NoSuchAlgorithmException , InvalidAlgorithmParameterException , InvalidKeyException {
0 commit comments