|
|
@@ -1,8 +1,16 @@
|
|
|
package jp.yamoto.farm.common.utils;
|
|
|
|
|
|
|
|
|
+import jp.yamoto.farm.common.config.AppConfig;
|
|
|
+import jp.yamoto.farm.common.constant.Constants;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.security.*;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
/**
|
|
|
* デコードユーティリティクラス
|
|
|
* <p>
|
|
|
@@ -13,6 +21,55 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Slf4j
|
|
|
public class DecryptUtils {
|
|
|
|
|
|
+ private static final String AES_ALGORITHM = "AES";
|
|
|
+ private static final String AES_CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AES 暗号化
|
|
|
+ */
|
|
|
+ public static String encryptAES(String data, String key) throws Exception {
|
|
|
+ byte[] keyBytes = Base64.getDecoder().decode(key);
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
|
|
|
+
|
|
|
+ byte[] iv = new byte[16];
|
|
|
+ SecureRandom secureRandom = new SecureRandom();
|
|
|
+ secureRandom.nextBytes(iv);
|
|
|
+ IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
|
+
|
|
|
+ Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
|
|
|
+
|
|
|
+ byte[] encrypted = cipher.doFinal(data.getBytes(Constants.UTF8));
|
|
|
+
|
|
|
+ byte[] combined = new byte[iv.length + encrypted.length];
|
|
|
+ System.arraycopy(iv, 0, combined, 0, iv.length);
|
|
|
+ System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);
|
|
|
+
|
|
|
+ return Base64.getEncoder().encodeToString(combined);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AES 暗号化されたテキストを復号します。
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static String decryptAES(String encryptedData, String key) throws Exception {
|
|
|
+ byte[] combined = Base64.getDecoder().decode(encryptedData);
|
|
|
+ byte[] keyBytes = Base64.getDecoder().decode(key);
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AES_ALGORITHM);
|
|
|
+
|
|
|
+ byte[] iv = new byte[16];
|
|
|
+ byte[] encrypted = new byte[combined.length - 16];
|
|
|
+ System.arraycopy(combined, 0, iv, 0, 16);
|
|
|
+ System.arraycopy(combined, 16, encrypted, 0, encrypted.length);
|
|
|
+
|
|
|
+ IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
|
+ Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
|
|
|
+
|
|
|
+ byte[] decrypted = cipher.doFinal(encrypted);
|
|
|
+ return new String(decrypted, Constants.UTF8);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 暗号化されたテキストを復号します。
|
|
|
*
|
|
|
@@ -27,18 +84,21 @@ public class DecryptUtils {
|
|
|
* 注意:復号中に例外が発生した場合、スタックトレースを出力し、空文字列を返します。
|
|
|
*/
|
|
|
public static String decryptField(String encryptedText) {
|
|
|
- return encryptedText;
|
|
|
-// try {
|
|
|
-// if (StringUtils.isEmpty(encryptedText)) {
|
|
|
-// return encryptedText;
|
|
|
-// }
|
|
|
-//
|
|
|
+ //return encryptedText;
|
|
|
+ try {
|
|
|
+ if (StringUtils.isEmpty(encryptedText)) {
|
|
|
+ return encryptedText;
|
|
|
+ }
|
|
|
+
|
|
|
// // URLセーフなBase64エンコードを標準Base64エンコードに変換
|
|
|
// String base64Encoded = encryptedText.replace('-', '+').replace('_', '/');
|
|
|
// // Base64エンコードの長さを4の倍数にパディング
|
|
|
// while (base64Encoded.length() % 4 != 0) {
|
|
|
// base64Encoded += "=";
|
|
|
// }
|
|
|
+
|
|
|
+ return decryptAES(encryptedText, AppConfig.getDecryptFieldKey());
|
|
|
+
|
|
|
//
|
|
|
// // Base64文字列をデコード
|
|
|
// byte[] encryptedBytes = Base64.getDecoder().decode(base64Encoded);
|
|
|
@@ -55,11 +115,20 @@ public class DecryptUtils {
|
|
|
// // 復号
|
|
|
// byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedString));
|
|
|
// return new String(decryptedBytes);
|
|
|
-// } catch (Exception e) {
|
|
|
-// // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
|
|
|
-// e.printStackTrace();
|
|
|
-// log.error("decryptField error: {} {}", encryptedText, e.getMessage());
|
|
|
-// return encryptedText;
|
|
|
-// }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 例外が発生した場合、スタックトレースを出力し、空文字列を返す
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("decryptField error: {} {}", encryptedText, e.getMessage());
|
|
|
+ return encryptedText;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String key = "rtKDbersTvuTdyhbadsddd";
|
|
|
+ String s = DecryptUtils.encryptAES("abdafasdc@nextosd.com", key);
|
|
|
+
|
|
|
+ System.out.println(s);
|
|
|
+
|
|
|
+ System.out.println(DecryptUtils.decryptAES(s, key));
|
|
|
}
|
|
|
}
|