Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 41 additions & 72 deletions devkit-utils/src/main/java/com/onixbyte/devkit/utils/AesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@

package com.onixbyte.devkit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.GeneralSecurityException;
import java.util.Base64;
import java.util.Objects;
import java.util.UUID;

/**
Expand All @@ -42,110 +33,88 @@
* The utility methods in this class are useful for scenarios where data needs to be securely
* encrypted and decrypted.
* </p>
*
*
* <p><b>Example usage:</b></p>
* <pre>
* {@code
* <pre>{@code
* // Encrypting and decrypting byte array data
* byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8);
* byte[] data = "Hello World".getBytes(StandardCharsets.UTF_8);
* byte[] encryptedData = AesUtil.encrypt(data, secretKey);
* byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey);
* System.out.println(new String(decryptedData, StandardCharsets.UTF_8)); // Output: Hello World
*
*
* // Encrypting and decrypting string data
* String secret = "43f72073956d4c81";
* String encryptedString = AesUtil.encrypt("Hello World", secret);
* String decryptedString = AesUtil.decrypt(encryptedString, secret);
* System.out.println(decryptedString); // Output: Hello World
*
*
* // Generating a random secret key
* String randomSecret = AesUtil.generateRandomSecret();
* System.out.println(randomSecret); // Output: A ramdomly generated 16-character long secret
* }
* </pre>
* System.out.println(randomSecret); // Output: A randomly generated 16-character long secret
* }</pre>
*
* @author hubin@baomidou
* @version 1.1.0
* @since 1.1.0
*/
public final class AesUtil {

private final static Logger log = LoggerFactory.getLogger(AesUtil.class);

/**
* Encrypts the data using the AES algorithm with the given secret.
* Encrypts the specified data using the AES algorithm with the provided secret key.
*
* @param data the data to be encrypted
* @param secret the secret to encrypt the data
* @return the encryption result or {@code null} if encryption failed
* @param secret the secret key used for encryption
* @return the encrypted data as a byte array
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
*/
public static byte[] encrypt(byte[] data, byte[] secret) {
try {
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
BadPaddingException exception) {
log.error(exception.getMessage());
for (var stackTraceElement : exception.getStackTrace()) {
log.error("{}", stackTraceElement.toString());
}
}
return null;
public static byte[] encrypt(byte[] data, byte[] secret) throws GeneralSecurityException {
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
return cipher.doFinal(data);
Comment on lines +74 to +75
Copy link

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the static secret key as IV can lead to IV reuse vulnerabilities in CBC mode; consider generating a random IV per encryption and prefixing it to the ciphertext.

Suggested change
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
return cipher.doFinal(data);
var iv = new byte[16]; // AES block size is 16 bytes
var secureRandom = new java.security.SecureRandom();
secureRandom.nextBytes(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
var encryptedData = cipher.doFinal(data);
var result = new byte[iv.length + encryptedData.length];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(encryptedData, 0, result, iv.length, encryptedData.length);
return result;

Copilot uses AI. Check for mistakes.
}

/**
* Decrypts the data using the AES algorithm with the given secret.
* Decrypts the specified data using the AES algorithm with the provided secret key.
*
* @param data the data to be decrypted
* @param secret the secret to encrypt the data
* @return the decryption result or {@code null} if decryption failed
* @param data the data to be decrypted
* @param secret the secret key used for decryption
* @return the decrypted data as a byte array
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
*/
public static byte[] decrypt(byte[] data, byte[] secret) {
try {
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
UnsupportedOperationException | InvalidKeyException |
InvalidAlgorithmParameterException | IllegalBlockSizeException |
BadPaddingException exception) {
log.error(exception.getMessage());
for (var stackTraceElement : exception.getStackTrace()) {
log.error("{}", stackTraceElement.toString());
}
}
return null;
public static byte[] decrypt(byte[] data, byte[] secret) throws GeneralSecurityException {
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
return cipher.doFinal(data);
}

/**
* Encrypts the data using the AES algorithm with the given secret.
* Encrypts the specified string data using the AES algorithm with the provided secret key.
*
* @param data the data to be encrypted
* @param secret the secret to encrypt the data
* @return the encryption result or {@code null} if encryption failed
* @param data the string data to be encrypted
* @param secret the secret key used for encryption
* @return the encrypted data encoded in Base64
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
*/
public static String encrypt(String data, String secret) {
public static String encrypt(String data, String secret) throws GeneralSecurityException {
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8),
secret.getBytes(StandardCharsets.UTF_8)));
}

/**
* Decrypts the data using the AES algorithm with the given secret.
* Decrypts the specified Base64-encoded string data using the AES algorithm with the provided secret key.
*
* @param data the data to be decrypted
* @param secret the secret to encrypt the data
* @return the decryption result or {@code null} if decryption failed
* @param data the Base64-encoded string data to be decrypted
* @param secret the secret key used for decryption
* @return the decrypted string data
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
*/
public static String decrypt(String data, String secret) {
return new String(Objects.requireNonNull(
decrypt(Base64.getDecoder().decode(data.getBytes()),
secret.getBytes(StandardCharsets.UTF_8)))
);
public static String decrypt(String data, String secret) throws GeneralSecurityException {
var decrypted = decrypt(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)),
secret.getBytes(StandardCharsets.UTF_8));
return new String(decrypted, StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package com.onixbyte.devkit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand Down Expand Up @@ -58,8 +55,6 @@
*/
public final class Base64Util {

private final static Logger log = LoggerFactory.getLogger(Base64Util.class);

/**
* Ensure that there is only one Base64 Encoder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package com.onixbyte.devkit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.BooleanSupplier;
Expand Down Expand Up @@ -47,8 +44,6 @@
*/
public final class BoolUtil {

private final static Logger log = LoggerFactory.getLogger(BoolUtil.class);

/**
* Logical and calculation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,4 @@ public void then(Runnable trueHandler) {
then(trueHandler, null);
}

/**
* Get the boolean result.
* <p>
* <b>Note:</b> {@link BranchUtil} is not responsible for getting a raw boolean result, consider use
* {@link BoolUtil} to replace.
*
* @return the result
* @see BoolUtil
*/
@Deprecated(forRemoval = true)
public boolean getResult() {
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package com.onixbyte.devkit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -33,8 +30,6 @@
*/
public final class CollectionUtil {

private static final Logger log = LoggerFactory.getLogger(CollectionUtil.class);

/**
* Private constructor to prevent instantiation of this utility class.
*/
Expand Down Expand Up @@ -68,7 +63,7 @@ public static <T, C extends Collection<T>> List<C> chunk(C originalCollection,
throw new IllegalArgumentException("Collection must not be null.");
}

if (maxSize < 0) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize must greater than 0.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@

package com.onixbyte.devkit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -70,8 +66,6 @@
*/
public final class HashUtil {

private final static Logger log = LoggerFactory.getLogger(HashUtil.class);

/**
* Calculates the MD2 hash value of the specified string using the given charset.
*
Expand Down
114 changes: 0 additions & 114 deletions devkit-utils/src/main/java/com/onixbyte/devkit/utils/ListUtil.java

This file was deleted.

Loading