개인정보 암복호화
AES-256-GCM + RSA-OAEP 암복호화 규격과 샘플 코드
OPEN API로 주고받는 개인정보(CI, 이름, 휴대폰번호, 생년월일 등)는 AES-256-GCM으로 암호화하고, AES 키는 RSA(OAEP)로 암호화해 전달합니다.
| 항목 | 규격 |
|---|---|
| 데이터 암호화 | AES/GCM/NoPadding, 키 32바이트(256bit), IV 12바이트, 태그 16바이트 |
| 키 암호화 | RSA/ECB/OAEPWithSHA-256AndMGF1Padding (OAEP 해시 SHA-256, MGF1 해시 SHA-1) |
| 인코딩 | 암호문·키·IV 모두 Base64 |
암복호화 흐름
개인정보는 토스 → 제휴사와 제휴사 → 토스 양방향 모두 암호화하며, 방향에 따라 사용하는 RSA 키가 다릅니다.
방화벽
양측 서버 간 방화벽 허용이 필요합니다. 허용할 토스 IP는 담당 매니저가 개별 안내합니다.
토스 → 제휴사 (개인정보 수신)
등록 이력 조회, 인증 이력 조회 등 토스가 제휴사에 개인정보를 보낼 때 사용합니다.
| 단계 | 주체 | 설명 |
|---|---|---|
| 1 | 제휴사 | RSA 키 쌍을 생성하고 공개키를 토스에 전달 |
| 2 | 토스 | AES 키·IV를 생성해 개인정보를 AES-GCM 암호화 |
| 3 | 토스 | AES 키를 제휴사 공개키로 RSA 암호화 |
| 4 | 토스 | encryptedKey, iv, 암호화된 데이터를 API 응답으로 전달 |
| 5 | 제휴사 | encryptedKey를 자신의 개인키로 RSA 복호화 → AES 키 획득 |
| 6 | 제휴사 | AES 키 + iv로 데이터 AES-GCM 복호화 |
제휴사 → 토스 (개인정보 전송)
얼굴입장 등록 준비 등 제휴사가 토스에 개인정보를 보낼 때 사용합니다.
| 단계 | 주체 | 설명 |
|---|---|---|
| 1 | 제휴사 | RSA Public Key 조회 API로 토스의 공개키를 조회 |
| 2 | 제휴사 | AES 키·IV를 생성해 개인정보를 AES-GCM 암호화 |
| 3 | 제휴사 | AES 키를 토스 공개키로 RSA 암호화 |
| 4 | 제휴사 | encryptedKey, iv, 암호화된 데이터를 API 요청으로 전달 |
| 5 | 토스 | encryptedKey를 자신의 개인키로 RSA 복호화 → AES 키 획득 |
| 6 | 토스 | AES 키 + iv로 데이터 AES-GCM 복호화 |
구현 확인은 암호화 테스트 API로 할 수 있습니다.
AES 샘플 코드
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.SecretKeySpec
class AesEncryptManager {
companion object {
const val GCM_TAG_BYTE_LENGTH = 16
}
fun encryptGcm(target: ByteArray, secretKey: ByteArray, iv: ByteArray): String {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
val nonceSpec = GCMParameterSpec(GCM_TAG_BYTE_LENGTH * Byte.SIZE_BITS, iv)
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(secretKey, "AES"), nonceSpec)
return Base64.getEncoder().encodeToString(cipher.doFinal(target))
}
fun decryptGcm(target: String, secretKey: ByteArray, iv: ByteArray): ByteArray {
val encrypted = Base64.getDecoder().decode(target)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
val nonceSpec = GCMParameterSpec(GCM_TAG_BYTE_LENGTH * Byte.SIZE_BITS, iv)
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(secretKey, "AES"), nonceSpec)
return cipher.doFinal(encrypted)
}
}RSA 샘플 코드
fun encryptWithRSA(plain: ByteArray, publicKey: PublicKey): ByteArray {
val cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
return cipher.doFinal(plain)
}
fun decryptWithRSA(encryptedTarget: String): ByteArray {
val privateKeyStr = Base64.getDecoder().decode("privateKey를 여기에 입력하세요")
val keyFactory = KeyFactory.getInstance("RSA")
val privateKey = keyFactory.generatePrivate(PKCS8EncodedKeySpec(privateKeyStr))
val cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
cipher.init(Cipher.DECRYPT_MODE, privateKey)
return cipher.doFinal(Base64.getDecoder().decode(encryptedTarget))
}전체 샘플 코드
import java.security.KeyFactory
import java.security.PublicKey
import java.security.SecureRandom
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.SecretKeySpec
class SampleEncryptManager {
companion object {
private const val GCM_TAG_BYTE_LENGTH = 16
}
fun generateAesKey(): ByteArray = ByteArray(32).also { SecureRandom().nextBytes(it) }
fun generateIv(): ByteArray = ByteArray(12).also { SecureRandom().nextBytes(it) }
fun convertEncodedToPublicKey(encoded: ByteArray, algorithm: String): PublicKey =
KeyFactory.getInstance(algorithm).generatePublic(X509EncodedKeySpec(encoded))
fun encryptGcm(target: ByteArray, secretKey: ByteArray, iv: ByteArray): String {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(secretKey, "AES"), GCMParameterSpec(GCM_TAG_BYTE_LENGTH * Byte.SIZE_BITS, iv))
return Base64.getEncoder().encodeToString(cipher.doFinal(target))
}
fun decryptGcm(target: String, secretKey: ByteArray, iv: ByteArray): ByteArray {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(secretKey, "AES"), GCMParameterSpec(GCM_TAG_BYTE_LENGTH * Byte.SIZE_BITS, iv))
return cipher.doFinal(Base64.getDecoder().decode(target))
}
fun encryptWithRSA(plain: ByteArray, publicKey: PublicKey): ByteArray {
val cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
return cipher.doFinal(plain)
}
fun decryptWithRSA(encryptedTarget: String): ByteArray {
val privateKeyBytes = Base64.getDecoder().decode("privateKey를 여기에 입력하세요")
val privateKey = KeyFactory.getInstance("RSA").generatePrivate(PKCS8EncodedKeySpec(privateKeyBytes))
val cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
cipher.init(Cipher.DECRYPT_MODE, privateKey)
return cipher.doFinal(Base64.getDecoder().decode(encryptedTarget))
}
}
fun main() {
val manager = SampleEncryptManager()
// 1. 데이터 암호화 — AES 키·IV 생성 후 AES-GCM 암호화
val aesKey = manager.generateAesKey()
val ivBytes = manager.generateIv()
val encryptedName = manager.encryptGcm("김토스".toByteArray(), aesKey, ivBytes)
// AES 키를 RSA 공개키로 암호화
val publicKey = manager.convertEncodedToPublicKey(Base64.getDecoder().decode("publicKey를 여기에 입력하세요"), "RSA")
val encryptedKey = Base64.getEncoder().encodeToString(manager.encryptWithRSA(aesKey, publicKey))
val iv = Base64.getEncoder().encodeToString(ivBytes)
// 2. API 요청·응답 값: encryptedName, encryptedKey, iv
// 3. 복호화 — RSA로 AES 키 복원 후 AES-GCM 복호화
val decryptedKey = manager.decryptWithRSA(encryptedKey)
val name = String(manager.decryptGcm(encryptedName, decryptedKey, Base64.getDecoder().decode(iv)), Charsets.UTF_8)
println(name)
}