|
17 | 17 |
|
18 | 18 | package com.onixbyte.devkit.utils; |
19 | 19 |
|
20 | | -import org.slf4j.Logger; |
21 | | -import org.slf4j.LoggerFactory; |
22 | | - |
23 | | -import javax.crypto.BadPaddingException; |
24 | 20 | import javax.crypto.Cipher; |
25 | | -import javax.crypto.IllegalBlockSizeException; |
26 | | -import javax.crypto.NoSuchPaddingException; |
27 | 21 | import javax.crypto.spec.IvParameterSpec; |
28 | 22 | import javax.crypto.spec.SecretKeySpec; |
29 | 23 | import java.nio.charset.StandardCharsets; |
30 | | -import java.security.InvalidAlgorithmParameterException; |
31 | | -import java.security.InvalidKeyException; |
32 | | -import java.security.NoSuchAlgorithmException; |
| 24 | +import java.security.GeneralSecurityException; |
33 | 25 | import java.util.Base64; |
34 | | -import java.util.Objects; |
35 | 26 | import java.util.UUID; |
36 | 27 |
|
37 | 28 | /** |
|
42 | 33 | * The utility methods in this class are useful for scenarios where data needs to be securely |
43 | 34 | * encrypted and decrypted. |
44 | 35 | * </p> |
45 | | - * |
| 36 | + * |
46 | 37 | * <p><b>Example usage:</b></p> |
47 | | - * <pre> |
48 | | - * {@code |
| 38 | + * <pre>{@code |
49 | 39 | * // Encrypting and decrypting byte array data |
50 | 40 | * byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8); |
51 | 41 | * byte[] data = "Hello World".getBytes(StandardCharsets.UTF_8); |
52 | 42 | * byte[] encryptedData = AesUtil.encrypt(data, secretKey); |
53 | 43 | * byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey); |
54 | 44 | * System.out.println(new String(decryptedData, StandardCharsets.UTF_8)); // Output: Hello World |
55 | | - * |
| 45 | + * |
56 | 46 | * // Encrypting and decrypting string data |
57 | 47 | * String secret = "43f72073956d4c81"; |
58 | 48 | * String encryptedString = AesUtil.encrypt("Hello World", secret); |
59 | 49 | * String decryptedString = AesUtil.decrypt(encryptedString, secret); |
60 | 50 | * System.out.println(decryptedString); // Output: Hello World |
61 | | - * |
| 51 | + * |
62 | 52 | * // Generating a random secret key |
63 | 53 | * String randomSecret = AesUtil.generateRandomSecret(); |
64 | | - * System.out.println(randomSecret); // Output: A ramdomly generated 16-character long secret |
65 | | - * } |
66 | | - * </pre> |
| 54 | + * System.out.println(randomSecret); // Output: A randomly generated 16-character long secret |
| 55 | + * }</pre> |
67 | 56 | * |
68 | 57 | * @author hubin@baomidou |
69 | 58 | * @version 1.1.0 |
70 | 59 | * @since 1.1.0 |
71 | 60 | */ |
72 | 61 | public final class AesUtil { |
73 | 62 |
|
74 | | - private final static Logger log = LoggerFactory.getLogger(AesUtil.class); |
75 | | - |
76 | 63 | /** |
77 | | - * Encrypts the data using the AES algorithm with the given secret. |
| 64 | + * Encrypts the specified data using the AES algorithm with the provided secret key. |
78 | 65 | * |
79 | 66 | * @param data the data to be encrypted |
80 | | - * @param secret the secret to encrypt the data |
81 | | - * @return the encryption result or {@code null} if encryption failed |
| 67 | + * @param secret the secret key used for encryption |
| 68 | + * @return the encrypted data as a byte array |
| 69 | + * @throws GeneralSecurityException if any cryptographic error occurs during encryption |
82 | 70 | */ |
83 | | - public static byte[] encrypt(byte[] data, byte[] secret) { |
84 | | - try { |
85 | | - var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES); |
86 | | - var cipher = Cipher.getInstance(AES_CBC_CIPHER); |
87 | | - cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret |
88 | | - return cipher.doFinal(data); |
89 | | - } catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException | |
90 | | - InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | |
91 | | - BadPaddingException exception) { |
92 | | - log.error(exception.getMessage()); |
93 | | - for (var stackTraceElement : exception.getStackTrace()) { |
94 | | - log.error("{}", stackTraceElement.toString()); |
95 | | - } |
96 | | - } |
97 | | - return null; |
| 71 | + public static byte[] encrypt(byte[] data, byte[] secret) throws GeneralSecurityException { |
| 72 | + var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES); |
| 73 | + var cipher = Cipher.getInstance(AES_CBC_CIPHER); |
| 74 | + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); |
| 75 | + return cipher.doFinal(data); |
98 | 76 | } |
99 | 77 |
|
100 | 78 | /** |
101 | | - * Decrypts the data using the AES algorithm with the given secret. |
| 79 | + * Decrypts the specified data using the AES algorithm with the provided secret key. |
102 | 80 | * |
103 | | - * @param data the data to be decrypted |
104 | | - * @param secret the secret to encrypt the data |
105 | | - * @return the decryption result or {@code null} if decryption failed |
| 81 | + * @param data the data to be decrypted |
| 82 | + * @param secret the secret key used for decryption |
| 83 | + * @return the decrypted data as a byte array |
| 84 | + * @throws GeneralSecurityException if any cryptographic error occurs during decryption |
106 | 85 | */ |
107 | | - public static byte[] decrypt(byte[] data, byte[] secret) { |
108 | | - try { |
109 | | - var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES); |
110 | | - var cipher = Cipher.getInstance(AES_CBC_CIPHER); |
111 | | - cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret |
112 | | - return cipher.doFinal(data); |
113 | | - } catch (NoSuchAlgorithmException | NoSuchPaddingException | |
114 | | - UnsupportedOperationException | InvalidKeyException | |
115 | | - InvalidAlgorithmParameterException | IllegalBlockSizeException | |
116 | | - BadPaddingException exception) { |
117 | | - log.error(exception.getMessage()); |
118 | | - for (var stackTraceElement : exception.getStackTrace()) { |
119 | | - log.error("{}", stackTraceElement.toString()); |
120 | | - } |
121 | | - } |
122 | | - return null; |
| 86 | + public static byte[] decrypt(byte[] data, byte[] secret) throws GeneralSecurityException { |
| 87 | + var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES); |
| 88 | + var cipher = Cipher.getInstance(AES_CBC_CIPHER); |
| 89 | + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); |
| 90 | + return cipher.doFinal(data); |
123 | 91 | } |
124 | 92 |
|
125 | 93 | /** |
126 | | - * Encrypts the data using the AES algorithm with the given secret. |
| 94 | + * Encrypts the specified string data using the AES algorithm with the provided secret key. |
127 | 95 | * |
128 | | - * @param data the data to be encrypted |
129 | | - * @param secret the secret to encrypt the data |
130 | | - * @return the encryption result or {@code null} if encryption failed |
| 96 | + * @param data the string data to be encrypted |
| 97 | + * @param secret the secret key used for encryption |
| 98 | + * @return the encrypted data encoded in Base64 |
| 99 | + * @throws GeneralSecurityException if any cryptographic error occurs during encryption |
131 | 100 | */ |
132 | | - public static String encrypt(String data, String secret) { |
| 101 | + public static String encrypt(String data, String secret) throws GeneralSecurityException { |
133 | 102 | return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8), |
134 | 103 | secret.getBytes(StandardCharsets.UTF_8))); |
135 | 104 | } |
136 | 105 |
|
137 | 106 | /** |
138 | | - * Decrypts the data using the AES algorithm with the given secret. |
| 107 | + * Decrypts the specified Base64-encoded string data using the AES algorithm with the provided secret key. |
139 | 108 | * |
140 | | - * @param data the data to be decrypted |
141 | | - * @param secret the secret to encrypt the data |
142 | | - * @return the decryption result or {@code null} if decryption failed |
| 109 | + * @param data the Base64-encoded string data to be decrypted |
| 110 | + * @param secret the secret key used for decryption |
| 111 | + * @return the decrypted string data |
| 112 | + * @throws GeneralSecurityException if any cryptographic error occurs during decryption |
143 | 113 | */ |
144 | | - public static String decrypt(String data, String secret) { |
145 | | - return new String(Objects.requireNonNull( |
146 | | - decrypt(Base64.getDecoder().decode(data.getBytes()), |
147 | | - secret.getBytes(StandardCharsets.UTF_8))) |
148 | | - ); |
| 114 | + public static String decrypt(String data, String secret) throws GeneralSecurityException { |
| 115 | + var decrypted = decrypt(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)), |
| 116 | + secret.getBytes(StandardCharsets.UTF_8)); |
| 117 | + return new String(decrypted, StandardCharsets.UTF_8); |
149 | 118 | } |
150 | 119 |
|
151 | 120 | /** |
|
0 commit comments