Skip to content

Commit f7b46c2

Browse files
committed
fix(crypto): use explicit UTF-8 charset for string/bytes conversion
Use StandardCharsets.UTF_8 instead of platform-default charset in CryptoEnvironmentProvider and CRYPTO to ensure consistent encryption output across all JVM locales and platforms.
1 parent 1c10ff2 commit f7b46c2

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

jpos/src/main/java/org/jpos/core/CryptoEnvironmentProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.crypto.spec.GCMParameterSpec;
2626
import javax.crypto.spec.SecretKeySpec;
2727
import java.nio.ByteBuffer;
28+
import java.nio.charset.StandardCharsets;
2829
import java.security.SecureRandom;
2930
import java.util.Base64;
3031

@@ -82,7 +83,7 @@ public String get(String config) {
8283
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
8384
byte[] plaintext = cipher.doFinal(ciphertext);
8485

85-
return new String(plaintext);
86+
return new String(plaintext, StandardCharsets.UTF_8);
8687
} catch (Exception e) {
8788
throw new RuntimeException("Failed to decrypt value", e);
8889
}
@@ -108,7 +109,7 @@ public static String encrypt(String value) {
108109
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
109110
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BITS, iv);
110111
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
111-
byte[] ciphertext = cipher.doFinal(value.getBytes());
112+
byte[] ciphertext = cipher.doFinal(value.getBytes(StandardCharsets.UTF_8));
112113

113114
// Combine IV and ciphertext
114115
ByteBuffer buf = ByteBuffer.allocate(iv.length + ciphertext.length);

jpos/src/main/java/org/jpos/q2/cli/CRYPTO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.crypto.spec.GCMParameterSpec;
2626
import javax.crypto.spec.SecretKeySpec;
2727
import java.nio.ByteBuffer;
28+
import java.nio.charset.StandardCharsets;
2829
import java.security.SecureRandom;
2930
import java.util.Base64;
3031

@@ -70,7 +71,7 @@ public String encrypt(String value) {
7071
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
7172
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BITS, iv);
7273
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
73-
byte[] ciphertext = cipher.doFinal(value.getBytes());
74+
byte[] ciphertext = cipher.doFinal(value.getBytes(StandardCharsets.UTF_8));
7475

7576
// Combine IV and ciphertext
7677
ByteBuffer buf = ByteBuffer.allocate(iv.length + ciphertext.length);

0 commit comments

Comments
 (0)