|
|
@@ -3,6 +3,7 @@ package jp.yamoto.farm.common.utils;
|
|
|
|
|
|
import jp.yamoto.farm.common.config.AppConfig;
|
|
|
import jp.yamoto.farm.common.constant.Constants;
|
|
|
+import jp.yamoto.farm.common.exception.ServiceException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
@@ -27,37 +28,49 @@ public class DecryptUtils {
|
|
|
/**
|
|
|
* AES 暗号化
|
|
|
*/
|
|
|
- public static String encryptAES(String data) throws Exception {
|
|
|
- byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
|
|
|
- byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
|
|
|
+ public static String encryptAES(String data) {
|
|
|
+ try {
|
|
|
+ byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
|
|
|
+ byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
|
|
|
|
|
|
- SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
|
|
|
- IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
|
|
|
|
|
- Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
+ Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
|
|
|
- byte[] encrypted = cipher.doFinal(data.getBytes(Constants.UTF8));
|
|
|
- return Base64.getEncoder().encodeToString(encrypted);
|
|
|
+ byte[] encrypted = cipher.doFinal(data.getBytes(Constants.UTF8));
|
|
|
+ return Base64.getEncoder().encodeToString(encrypted);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
|
|
|
+ log.error("decryptAES error: {} {}", data, e.getMessage());
|
|
|
+ throw new ServiceException(MessageUtils.message("EM007"));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* AES 暗号化されたテキストを復号します。
|
|
|
*
|
|
|
*/
|
|
|
- public static String decryptAES(String encryptedData) throws Exception {
|
|
|
- byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
|
|
|
- byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
|
|
|
+ public static String decryptAES(String encryptedData) {
|
|
|
+ try {
|
|
|
+ byte[] keyBytes = hexStringToByteArray(AppConfig.getDecryptFieldKey());
|
|
|
+ byte[] ivBytes = hexStringToByteArray(AppConfig.getDecryptFieldIv());
|
|
|
|
|
|
- SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
|
|
|
- IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
|
|
|
|
|
- Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
|
|
|
- cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
+ Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
|
|
|
- byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
|
|
|
- byte[] decrypted = cipher.doFinal(encryptedBytes);
|
|
|
- return new String(decrypted, Constants.UTF8);
|
|
|
+ byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
|
|
|
+ byte[] decrypted = cipher.doFinal(encryptedBytes);
|
|
|
+ return new String(decrypted, Constants.UTF8);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
|
|
|
+ log.error("decryptAES error: {} {}", encryptedData, e.getMessage());
|
|
|
+ throw new ServiceException(MessageUtils.message("EM008"));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|