|
| 1 | +package com.ureca.unity.global.config; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Value; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +import javax.crypto.Cipher; |
| 7 | +import javax.crypto.spec.GCMParameterSpec; |
| 8 | +import javax.crypto.spec.SecretKeySpec; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.security.SecureRandom; |
| 11 | +import java.util.Base64; |
| 12 | + |
| 13 | +@Component |
| 14 | +public class OAuthTokenCrypto { |
| 15 | + |
| 16 | + private static final String ALG = "AES"; |
| 17 | + private static final String TRANSFORM = "AES/GCM/NoPadding"; |
| 18 | + private static final int IV_LEN = 12; // GCM 권장 12 bytes |
| 19 | + private static final int TAG_BITS = 128; |
| 20 | + |
| 21 | + private final byte[] keyBytes; |
| 22 | + private final SecureRandom random = new SecureRandom(); |
| 23 | + |
| 24 | + public OAuthTokenCrypto(@Value("${security.oauth-token.secret}") String secret) { |
| 25 | + // 최소 수정 버전: secret을 16/24/32 bytes로 맞춰 쓰는 걸 권장 |
| 26 | + // 여기서는 "UTF-8 바이트" 기준으로 32바이트로 잘라/패딩 처리 |
| 27 | + byte[] raw = secret.getBytes(StandardCharsets.UTF_8); |
| 28 | + this.keyBytes = new byte[32]; |
| 29 | + for (int i = 0; i < this.keyBytes.length; i++) { |
| 30 | + this.keyBytes[i] = i < raw.length ? raw[i] : 0; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public String encrypt(String plain) { |
| 35 | + if (plain == null) return null; |
| 36 | + try { |
| 37 | + byte[] iv = new byte[IV_LEN]; |
| 38 | + random.nextBytes(iv); |
| 39 | + |
| 40 | + Cipher cipher = Cipher.getInstance(TRANSFORM); |
| 41 | + SecretKeySpec key = new SecretKeySpec(keyBytes, ALG); |
| 42 | + cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TAG_BITS, iv)); |
| 43 | + |
| 44 | + byte[] ct = cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8)); |
| 45 | + |
| 46 | + // 저장 포맷: base64( iv + ciphertext ) |
| 47 | + byte[] out = new byte[iv.length + ct.length]; |
| 48 | + System.arraycopy(iv, 0, out, 0, iv.length); |
| 49 | + System.arraycopy(ct, 0, out, iv.length, ct.length); |
| 50 | + |
| 51 | + return Base64.getEncoder().encodeToString(out); |
| 52 | + } catch (Exception e) { |
| 53 | + throw new IllegalStateException("OAuth token encrypt failed", e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public String decrypt(String enc) { |
| 58 | + if (enc == null) return null; |
| 59 | + try { |
| 60 | + byte[] in = Base64.getDecoder().decode(enc); |
| 61 | + if (in.length < IV_LEN + 1) return enc; // 방어: 평문이 들어온 경우 그냥 반환 |
| 62 | + |
| 63 | + byte[] iv = new byte[IV_LEN]; |
| 64 | + byte[] ct = new byte[in.length - IV_LEN]; |
| 65 | + |
| 66 | + System.arraycopy(in, 0, iv, 0, IV_LEN); |
| 67 | + System.arraycopy(in, IV_LEN, ct, 0, ct.length); |
| 68 | + |
| 69 | + Cipher cipher = Cipher.getInstance(TRANSFORM); |
| 70 | + SecretKeySpec key = new SecretKeySpec(keyBytes, ALG); |
| 71 | + cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(TAG_BITS, iv)); |
| 72 | + |
| 73 | + byte[] pt = cipher.doFinal(ct); |
| 74 | + return new String(pt, StandardCharsets.UTF_8); |
| 75 | + } catch (Exception e) { |
| 76 | + // 방어: 기존 데이터가 평문/다른 포맷이면 그냥 원문 반환(마이그레이션 단계에서 유용) |
| 77 | + return enc; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments