Skip to content

Commit cdf2a28

Browse files
Potential fix for code scanning alert no. 20: Use of a broken or risky cryptographic algorithm
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 143dc98 commit cdf2a28

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

core/src/main/java/io/sentrius/sso/core/security/service/CryptoService.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentrius.sso.core.security.service;
22

33
import javax.crypto.Cipher;
4+
import javax.crypto.spec.GCMParameterSpec;
45
import javax.crypto.spec.SecretKeySpec;
56
import java.io.IOException;
67
import java.nio.charset.StandardCharsets;
@@ -30,7 +31,7 @@ public class CryptoService {
3031
final ApplicationKeyRepository applicationKeyRepository;
3132
private final byte[] key;
3233

33-
private static final String CIPHER_INSTANCE = "AES/ECB/PKCS5Padding";
34+
private static final String CIPHER_INSTANCE = "AES/GCM/NoPadding";
3435
private static final String CRYPT_ALGORITHM = "AES";
3536
private static final String HASH_ALGORITHM = "SHA-256";
3637
private final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
@@ -65,16 +66,28 @@ public String hash(String str, String salt) throws NoSuchAlgorithmException {
6566

6667
public String encrypt(String str) throws GeneralSecurityException {
6768
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE);
68-
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, CRYPT_ALGORITHM));
69+
byte[] iv = new byte[12];
70+
new SecureRandom().nextBytes(iv);
71+
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
72+
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, CRYPT_ALGORITHM), gcmSpec);
6973
byte[] encVal = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
70-
return Base64.getEncoder().encodeToString(encVal);
74+
byte[] encryptedIvAndText = new byte[iv.length + encVal.length];
75+
System.arraycopy(iv, 0, encryptedIvAndText, 0, iv.length);
76+
System.arraycopy(encVal, 0, encryptedIvAndText, iv.length, encVal.length);
77+
return Base64.getEncoder().encodeToString(encryptedIvAndText);
7178
}
7279

7380
public String encrypt(byte [] bytes) throws GeneralSecurityException {
7481
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE);
75-
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, CRYPT_ALGORITHM));
82+
byte[] iv = new byte[12];
83+
new SecureRandom().nextBytes(iv);
84+
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
85+
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, CRYPT_ALGORITHM), gcmSpec);
7686
byte[] encVal = cipher.doFinal(bytes);
77-
return Base64.getEncoder().encodeToString(encVal);
87+
byte[] encryptedIvAndText = new byte[iv.length + encVal.length];
88+
System.arraycopy(iv, 0, encryptedIvAndText, 0, iv.length);
89+
System.arraycopy(encVal, 0, encryptedIvAndText, iv.length, encVal.length);
90+
return Base64.getEncoder().encodeToString(encryptedIvAndText);
7891
}
7992

8093
public String decrypt(String encryptedStr) throws GeneralSecurityException {

0 commit comments

Comments
 (0)