Skip to content

Commit

Permalink
commons-lib: add secure padding scheme, #TASK-7192
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Feb 17, 2025
1 parent 628a2ca commit 1450aea
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ private CryptoUtils() {

// Method to generate a new AES key
public static SecretKey generateKey(int n) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES/GCM/NoPadding");
keyGenerator.init(n);
SecretKey key = keyGenerator.generateKey();
return key;
}

// Method to encrypt a string using a given key
public static String encrypt(String strToEncrypt, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(strToEncrypt.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}

// Method to decrypt a string using a given key
public static String decrypt(String strToDecrypt, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(strToDecrypt));
return new String(decryptedBytes);
Expand Down

0 comments on commit 1450aea

Please sign in to comment.