Skip to content

Commit 1eace0d

Browse files
Merge pull request #24 from Mastercard/feature/aes-cbc-cleanup
* Removing un-neccessary SecretKeySpec for AESCBC and AESGCM classes
2 parents 3819e2f + f2e9a01 commit 1eace0d

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

src/main/java/com/mastercard/developer/encryption/aes/AESCBC.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ private AESCBC() {
1919

2020
@java.lang.SuppressWarnings("squid:S3329")
2121
public static byte[] decrypt(Key secretKey, JweObject object) throws GeneralSecurityException {
22+
// First 16 bytes are the MAC key, so we only use the second 16 bytes
2223
SecretKeySpec aesKey = new SecretKeySpec(secretKey.getEncoded(), 16, 16, "AES");
23-
2424
byte[] cipherText = EncodingUtils.base64Decode(object.getCipherText());
2525
byte[] iv = EncodingUtils.base64Decode(object.getIv());
26-
SecretKeySpec keyspec = new SecretKeySpec(aesKey.getEncoded(), "AES");
2726

28-
return cipher(keyspec, new IvParameterSpec(iv), cipherText, Cipher.DECRYPT_MODE);
27+
return cipher(aesKey, new IvParameterSpec(iv), cipherText, Cipher.DECRYPT_MODE);
2928
}
3029

3130
public static byte[] cipher(Key key, AlgorithmParameterSpec iv, byte[] bytes, int mode) throws GeneralSecurityException {

src/main/java/com/mastercard/developer/encryption/aes/AESGCM.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import com.mastercard.developer.utils.EncodingUtils;
66

77
import javax.crypto.Cipher;
8-
import javax.crypto.SecretKey;
98
import javax.crypto.spec.GCMParameterSpec;
10-
import javax.crypto.spec.SecretKeySpec;
119
import java.nio.charset.StandardCharsets;
1210
import java.security.GeneralSecurityException;
1311
import java.security.Key;
@@ -22,10 +20,9 @@ private AESGCM() {
2220

2321
public static byte[] decrypt(Key cek, JweObject object) throws GeneralSecurityException {
2422
byte[] aad = object.getRawHeader().getBytes(StandardCharsets.US_ASCII);
25-
SecretKey aesKey = new SecretKeySpec(cek.getEncoded(), "AES");
2623
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, EncodingUtils.base64Decode(object.getIv()));
2724
byte[] bytes = ByteUtils.concat(EncodingUtils.base64Decode(object.getCipherText()), EncodingUtils.base64Decode(object.getAuthTag()));
28-
return cipher(aesKey, gcmSpec, bytes, aad, Cipher.DECRYPT_MODE);
25+
return cipher(cek, gcmSpec, bytes, aad, Cipher.DECRYPT_MODE);
2926
}
3027

3128
public static byte[] cipher(Key key, GCMParameterSpec gcpSpec, byte[] bytes, byte[] aad, int mode) throws GeneralSecurityException {

0 commit comments

Comments
 (0)