Skip to content

Commit

Permalink
Update AESCBC.java
Browse files Browse the repository at this point in the history
using nopadding for GCM
  • Loading branch information
tomcrofts authored Jun 6, 2024
1 parent df18ab9 commit 82e7d43
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@ public class AESCBC {
private AESCBC() {
}

private static final String CYPHER = "AES/GCM/NoPadding";

@java.lang.SuppressWarnings("squid:S3329")
public static byte[] decrypt(Key secretKey, JweObject object) throws GeneralSecurityException {
// First 16 bytes are the MAC key, so we only use the second 16 bytes
SecretKeySpec aesKey = new SecretKeySpec(secretKey.getEncoded(), 16, 16, "AES");
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, EncodingUtils.base64Decode(object.getIv()));
byte[] cipherText = EncodingUtils.base64Decode(object.getCipherText());
byte[] iv = EncodingUtils.base64Decode(object.getIv());

byte[] decrypted = cipher(aesKey, new IvParameterSpec(iv), cipherText, Cipher.DECRYPT_MODE);

int padCount = decrypted[decrypted.length - 1];
byte[] unpadded = new byte[decrypted.length - padCount];
System.arraycopy(decrypted, 0, unpadded, 0, unpadded.length);
return unpadded;
return cipher(aesKey, gcmSpec, new IvParameterSpec(iv), cipherText, Cipher.DECRYPT_MODE);
}

public static byte[] cipher(Key key, AlgorithmParameterSpec iv, byte[] bytes, int mode) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(mode, key, iv);
public static byte[] cipher(Key key,GCMParameterSpec gcpSpec, AlgorithmParameterSpec iv, byte[] bytes, int mode) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance(CYPHER);
cipher.init(mode, key, gcpSpec, iv);
return cipher.doFinal(bytes);
}
}

0 comments on commit 82e7d43

Please sign in to comment.