Title: [BUG] AESEncryption: Static mutable Cipher causes IV cross-contamination, NPE, and decryption failures · Issue #7763 · TheAlgorithms/Java · GitHub
Open Graph Title: [BUG] AESEncryption: Static mutable Cipher causes IV cross-contamination, NPE, and decryption failures · Issue #7763 · TheAlgorithms/Java
X Title: [BUG] AESEncryption: Static mutable Cipher causes IV cross-contamination, NPE, and decryption failures · Issue #7763 · TheAlgorithms/Java
Description: Description In com.thealgorithms.ciphers.AESEncryption.java, cipher operations rely on a mutable static class variable private static Cipher aesCipher;. In decryptText(), the initialization vector (IV) is retrieved via aesCipher.getIV():...
Open Graph Description: Description In com.thealgorithms.ciphers.AESEncryption.java, cipher operations rely on a mutable static class variable private static Cipher aesCipher;. In decryptText(), the initialization vector ...
X Description: Description In com.thealgorithms.ciphers.AESEncryption.java, cipher operations rely on a mutable static class variable private static Cipher aesCipher;. In decryptText(), the initialization vector ...
Opengraph URL: https://github.com/TheAlgorithms/Java/issues/7763
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG] AESEncryption: Static mutable Cipher causes IV cross-contamination, NPE, and decryption failures","articleBody":"### Description\nIn `com.thealgorithms.ciphers.AESEncryption.java`, cipher operations rely on a mutable static class variable `private static Cipher aesCipher;`.\n\nIn `decryptText()`, the initialization vector (IV) is retrieved via `aesCipher.getIV()`:\n```java\npublic static String decryptText(byte[] byteCipherText, SecretKey secKey) throws ... {\n Cipher decryptionCipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\n GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, aesCipher.getIV());\n decryptionCipher.init(Cipher.DECRYPT_MODE, secKey, gcmParameterSpec);\n byte[] bytePlainText = decryptionCipher.doFinal(byteCipherText);\n return new String(bytePlainText);\n}\n```\n\nThis causes two major issues:\n1. **IV Cross-Contamination / Tag Mismatch:** If `encryptText()` is called twice on two different plaintexts ($M_1$ and $M_2$), `aesCipher` is updated with the IV generated for $M_2$. Calling `decryptText()` on $M_1$'s ciphertext then retrieves $M_2$'s IV, throwing `javax.crypto.AEADBadTagException: Tag mismatch!`.\n2. **`NullPointerException`:** If `decryptText()` is called in a separate session or process before `encryptText()` has been called, `aesCipher` is `null`, throwing `java.lang.NullPointerException`.\n3. **Thread Safety:** Concurrent calls from multiple threads cause race conditions on the shared static `aesCipher`.\n\n### Steps to reproduce\nRun the following test case:\n```java\n@Test\nvoid testMultipleEncryptionsAndDecryptions() throws Exception {\n SecretKey key = AESEncryption.getSecretEncryptionKey();\n byte[] cipher1 = AESEncryption.encryptText(\"First Message\", key);\n byte[] cipher2 = AESEncryption.encryptText(\"Second Message\", key);\n\n // Fails with javax.crypto.AEADBadTagException: Tag mismatch!\n String decrypted1 = AESEncryption.decryptText(cipher1, key);\n assertEquals(\"First Message\", decrypted1);\n}\n```\n\n### Expected behavior\nEncryption and decryption operations should be independent, thread-safe, and self-contained. The IV should either be prepended to the ciphertext (standard AES-GCM practice: `IV (12 bytes) || Ciphertext + Tag`) and extracted during decryption, or passed explicitly as a parameter to `decryptText()`.\n\n### Additional context\nThe static `aesCipher` variable should be eliminated so methods do not leak state across calls.\n","author":{"url":"https://github.com/Bhumit267","@type":"Person","name":"Bhumit267"},"datePublished":"2026-11-19T04:37:09Z","url":"https://github.com/TheAlgorithms/Java/issues/7763"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ba569605-727b-09f3-f811-b146b9689935 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D542:1F2D:28CF32C:33BF6BE:6B01D2F0 |
| html-safe-nonce | ffbca2b5767a71e097e1fb42e6dbce14b6a853d282f2173c809d806c96466eeb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTQyOjFGMkQ6MjhDRjMyQzozM0JGNkJFOjZCMDFEMkYwIiwidmlzaXRvcl9pZCI6IjU2NTQwNzUyMDU3MzIxMjc0NzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 01950ef299f2aa1b25adf1d9e1b46978b5b258f143a1062619cee74437b5c560 |
| hovercard-subject-tag | issue:6482491431 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/Java/7763/issue_layout |
| twitter:image | https://opengraph.githubassets.com/25d8cff651462c26db446a8c940ebfc00fdf224f674609f2c1f31186f1663f25/TheAlgorithms/Java/issues/7763 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/25d8cff651462c26db446a8c940ebfc00fdf224f674609f2c1f31186f1663f25/TheAlgorithms/Java/issues/7763 |
| og:image:alt | Description In com.thealgorithms.ciphers.AESEncryption.java, cipher operations rely on a mutable static class variable private static Cipher aesCipher;. In decryptText(), the initialization vector ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Bhumit267 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/Java git https://github.com/TheAlgorithms/Java.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 63477660 |
| octolytics-dimension-repository_nwo | TheAlgorithms/Java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 63477660 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/Java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width