-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to Java 21 #60
Conversation
final SecretKeySpec key, | ||
final byte[] response) | ||
throws GeneralSecurityException, IOException { | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
Check failure
Code scanning / CodeQL
Use of a broken or risky cryptographic algorithm High
AES/CBC/PKCS5Padding
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI about 13 hours ago
To fix the problem, we should replace the use of the AES/CBC/PKCS5Padding mode with a more secure mode, such as AES/GCM/NoPadding. This mode provides authenticated encryption, ensuring both confidentiality and integrity of the data.
- Update the
Cipher.getInstance
calls to use "AES/GCM/NoPadding". - Modify the encryption and decryption methods to handle the GCM-specific parameters, such as the GCM initialization vector (IV) and authentication tag.
- Ensure that the IV is generated securely and included in the encrypted output, and that it is extracted correctly during decryption.
-
Copy modified lines R33-R38 -
Copy modified lines R41-R43 -
Copy modified lines R66-R68
@@ -32,11 +32,13 @@ | ||
throws GeneralSecurityException, IOException { | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, key); | ||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | ||
byte[] iv = new byte[12]; // GCM recommended IV length is 12 bytes | ||
SecureRandom random = new SecureRandom(); | ||
random.nextBytes(iv); | ||
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); // 128-bit authentication tag length | ||
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec); | ||
byte[] encrypted = cipher.doFinal(response); | ||
AlgorithmParameters p = cipher.getParameters(); | ||
byte[] params = cipher.getParameters().getEncoded(); | ||
ByteBuffer resp = ByteBuffer.allocate( | ||
Integer.BYTES + params.length + encrypted.length); | ||
resp.putInt(params.length); | ||
resp.put(params); | ||
Integer.BYTES + iv.length + encrypted.length); | ||
resp.putInt(iv.length); | ||
resp.put(iv); | ||
resp.put(encrypted); | ||
@@ -63,6 +65,5 @@ | ||
|
||
AlgorithmParameters algP = AlgorithmParameters.getInstance("AES"); | ||
algP.init(params); | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.DECRYPT_MODE, key, algP); | ||
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, params); | ||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | ||
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec); | ||
|
|
||
AlgorithmParameters algP = AlgorithmParameters.getInstance("AES"); | ||
algP.init(params); | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
Check failure
Code scanning / CodeQL
Use of a broken or risky cryptographic algorithm High
AES/CBC/PKCS5Padding
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI about 13 hours ago
To fix the problem, we should replace the use of the AES/CBC/PKCS5Padding mode with a more secure mode, such as AES/GCM/NoPadding. This change will ensure that the encryption provides both confidentiality and integrity, reducing the risk of attacks.
- Replace the
Cipher.getInstance("AES/CBC/PKCS5Padding")
calls withCipher.getInstance("AES/GCM/NoPadding")
. - Update the encryption and decryption logic to handle the GCM mode, which includes managing the GCM parameters (nonce and authentication tag).
-
Copy modified lines R33-R38 -
Copy modified lines R41-R43 -
Copy modified lines R66-R70
@@ -32,11 +32,13 @@ | ||
throws GeneralSecurityException, IOException { | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, key); | ||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | ||
byte[] iv = new byte[12]; // GCM recommended IV length is 12 bytes | ||
SecureRandom random = new SecureRandom(); | ||
random.nextBytes(iv); | ||
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); | ||
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec); | ||
byte[] encrypted = cipher.doFinal(response); | ||
AlgorithmParameters p = cipher.getParameters(); | ||
byte[] params = cipher.getParameters().getEncoded(); | ||
ByteBuffer resp = ByteBuffer.allocate( | ||
Integer.BYTES + params.length + encrypted.length); | ||
resp.putInt(params.length); | ||
resp.put(params); | ||
Integer.BYTES + iv.length + encrypted.length); | ||
resp.putInt(iv.length); | ||
resp.put(iv); | ||
resp.put(encrypted); | ||
@@ -63,6 +65,7 @@ | ||
|
||
AlgorithmParameters algP = AlgorithmParameters.getInstance("AES"); | ||
algP.init(params); | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.DECRYPT_MODE, key, algP); | ||
byte[] iv = new byte[paramsLen]; | ||
challenge.get(iv); | ||
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); | ||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | ||
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec); | ||
|
No description provided.