diff --git a/common-toolbox/src/main/java/com/onixbyte/common/util/AesUtil.java b/common-toolbox/src/main/java/com/onixbyte/common/util/AesUtil.java index 74da677..62ee697 100644 --- a/common-toolbox/src/main/java/com/onixbyte/common/util/AesUtil.java +++ b/common-toolbox/src/main/java/com/onixbyte/common/util/AesUtil.java @@ -80,6 +80,22 @@ public final class AesUtil { private AesUtil() { } + /** + * Encrypts the specified data using the AES algorithm with the provided secret key. + * + * @param data the data to be encrypted + * @param secret the secret key used for encryption + * @param ivParam the iv param + * @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, byte[] ivParam) 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(ivParam)); + return cipher.doFinal(data); + } + /** * Encrypts the specified data using the AES algorithm with the provided secret key. * @@ -89,9 +105,22 @@ public final class AesUtil { * @throws GeneralSecurityException if any cryptographic error occurs during encryption */ public static byte[] encrypt(byte[] data, byte[] secret) throws GeneralSecurityException { + return encrypt(data, secret, 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 key used for decryption + * @param ivParam the iv param + * @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, byte[] ivParam) 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)); + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivParam)); return cipher.doFinal(data); } @@ -104,10 +133,24 @@ public final class AesUtil { * @throws GeneralSecurityException if any cryptographic error occurs during decryption */ 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); + return decrypt(data, secret, secret); + } + + /** + * Encrypts the specified string data using the AES algorithm with the provided secret key. + * + * @param data the string data to be encrypted + * @param secret the secret key used for encryption + * @param ivParam the iv param + * @return the encrypted data encoded in Base64 + * @throws GeneralSecurityException if any cryptographic error occurs during encryption + */ + public static String encrypt(String data, String secret, String ivParam) throws GeneralSecurityException { + return Base64.getEncoder().encodeToString(encrypt( + data.getBytes(StandardCharsets.UTF_8), + secret.getBytes(StandardCharsets.UTF_8), + ivParam.getBytes(StandardCharsets.UTF_8) + )); } /** @@ -119,8 +162,24 @@ public final class AesUtil { * @throws GeneralSecurityException if any cryptographic error occurs during encryption */ 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))); + return encrypt(data, secret, secret); + } + + /** + * Decrypts the specified Base64-encoded string data using the AES algorithm with the provided secret key. + * + * @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, String ivParam) throws GeneralSecurityException { + var decrypted = decrypt( + Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)), + secret.getBytes(StandardCharsets.UTF_8), + ivParam.getBytes(StandardCharsets.UTF_8) + ); + return new String(decrypted, StandardCharsets.UTF_8); } /** @@ -132,9 +191,7 @@ public final class AesUtil { * @throws GeneralSecurityException if any cryptographic error occurs during decryption */ 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); + return decrypt(data, secret, secret); } /** diff --git a/gradle.properties b/gradle.properties index 29c0ce1..00e3c6a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,22 +1,27 @@ # -# Copyright (C) 2024-2025 OnixByte. +# Copyright (c) 2024-2025 OnixByte # -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: # -# http://www.apache.org/licenses/LICENSE-2.0 +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# -# See the License for the specific language governing permissions and -# limitations under the License. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. # -artefactVersion=2.4.0 +artefactVersion=3.0.0 projectUrl=https://onixbyte.com/projects/onixbyte-toolbox projectGithubUrl=https://github.com/onixbyte/onixbyte-toolbox -licenseName=The Apache License, Version 2.0 +licenseName=MIT licenseUrl=https://onixbyte.com/projects/onixbyte-toolbox/LICENSE.txt diff --git a/gradlew.bat b/gradlew.bat index 25da30d..e2b0bab 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,17 +1,23 @@ @rem -@rem Copyright 2015 the original author or authors. +@rem Copyright (c) 2024-2025 OnixByte @rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at +@rem Permission is hereby granted, free of charge, to any person obtaining a copy +@rem of this software and associated documentation files (the "Software"), to deal +@rem in the Software without restriction, including without limitation the rights +@rem to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +@rem copies of the Software, and to permit persons to whom the Software is +@rem furnished to do so, subject to the following conditions: @rem -@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem The above copyright notice and this permission notice shall be included in all +@rem copies or substantial portions of the Software. @rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. +@rem THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +@rem IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +@rem FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +@rem AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +@rem LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +@rem OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +@rem SOFTWARE. @rem @if "%DEBUG%"=="" @echo off