Repository navigation
Conversation
…xt in AESEncryption
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7767 +/- ##
============================================
+ Coverage 82.42% 82.65% +0.22%
- Complexity 8508 8618 +110
============================================
Files 847 848 +1
Lines 26289 26481 +192
Branches 5133 5182 +49
============================================
+ Hits 21670 21888 +218
+ Misses 3828 3809 -19
+ Partials 791 784 -7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
left a comment
There was a problem hiding this comment.
Thanks for removing the shared Cipher and making the encrypted output self-contained. Before merging, please make encryption and decryption agree on the GCM parameters, validate the full minimum payload length, and document and test the resulting format.
| aesCipher.init(Cipher.ENCRYPT_MODE, secKey); | ||
| return aesCipher.doFinal(plainText.getBytes()); | ||
| Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | ||
| cipher.init(Cipher.ENCRYPT_MODE, secKey); |
There was a problem hiding this comment.
Decryption assumes a 12-byte IV and a 128-bit tag, while this initialization leaves both choices to the JCE provider. Please use an explicit GCMParameterSpec with the same tag length and a fresh, securely generated 12-byte IV for each encryption. This makes the byte layout consistent across providers.
There was a problem hiding this comment.
Addressed in e96e34e5. encryptText now generates a fresh 12-byte IV via SecureRandom on each invocation and initializes the cipher with an explicit GCMParameterSpec(GCM_TAG_LENGTH, iv) so encryption and decryption agree on the 12-byte IV and 128-bit authentication tag across JCE providers.
| */ | ||
| public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { | ||
| // AES defaults to AES/ECB/PKCS5Padding in Java 7 | ||
| if (byteCipherText.length < GCM_IV_LENGTH) { |
There was a problem hiding this comment.
Even an empty plaintext produces 28 bytes (a 12-byte IV plus a 16-byte authentication tag). Please validate GCM_IV_LENGTH + GCM_TAG_LENGTH / Byte.SIZE here. A 27-byte input currently passes this check and reaches doFinal, where it is rejected by the provider.
There was a problem hiding this comment.
Addressed in e96e34e5. decryptText now checks byteCipherText == null || byteCipherText.length < MIN_CIPHERTEXT_LENGTH, where MIN_CIPHERTEXT_LENGTH is GCM_IV_LENGTH + GCM_TAG_LENGTH / Byte.SIZE (28 bytes), rejecting inputs below the minimum payload length before reaching doFinal.
|
|
||
| ByteBuffer buffer = ByteBuffer.allocate(iv.length + cipherBytes.length); | ||
| buffer.put(iv); | ||
| buffer.put(cipherBytes); |
There was a problem hiding this comment.
Please document IV || ciphertext || authentication tag in the Javadoc for both encryptText and decryptText, including the 12-byte IV and 16-byte tag, so callers know what bytes to store and pass back.
There was a problem hiding this comment.
Addressed in e96e34e5. Updated the Javadoc for both encryptText and decryptText to explicitly document the IV || ciphertext || authentication tag layout, including the 12-byte IV, 16-byte (128-bit) tag, and 28-byte minimum payload length.
| } | ||
|
|
||
| @Test | ||
| public void testShortCiphertextThrowsException() throws Exception { |
There was a problem hiding this comment.
Please add a test at the 27-byte rejection boundary and a successful 28-byte empty-plaintext round trip. Also add authentication tests that verify that decryption rejects modified ciphertext and a different key.
There was a problem hiding this comment.
Addressed in e96e34e5. Added tests covering the 27-byte rejection boundary (and null input), a 28-byte empty-plaintext round trip, distinct IVs across repeated encryptions, and AEADBadTagException verification for modified ciphertext, modified authentication tags, and decryption with a different key.
|
@DenizAltunkapan Thank you for the review. All requested changes have been implemented and verified (latest commit: Summary of Changes
Tests & Verification
Ready for another look whenever you have a moment! |
clang-format -i --style=file path/to/your/file.javaFixes #7763
Description
In
AESEncryption.java,private static Cipher aesCipher;was stored as a mutable static variable. When decrypting,decryptText()relied onaesCipher.getIV().This caused several serious issues:
encryptText()was called on multiple plaintexts (aesCipherwas updated with the IV generated forjavax.crypto.AEADBadTagException: Tag mismatch!.NullPointerException: CallingdecryptText()without first runningencryptText()in the same JVM session resulted in an NPE becauseaesCipherwas null.Changes
private static Cipher aesCipher;, making encryption and decryption completely stateless and thread-safe.IV || Ciphertext + Tag) and extracting it during decryption.encryptText(String, SecretKey)anddecryptText(byte[], SecretKey)).AESEncryptionTest.javaverifying multiple consecutive encryptions/decryptions and validating error handling for short ciphertexts.Verification
mvn test -Dtest=AESEncryptionTestpasses (7/7 tests pass).mvn checkstyle:checkpasses with 0 violations.