https://api.tosspayments.com/v2/sellers
# Headers
# Content-Type: text/plain
# TossPayments-api-security-mode: ENCRYPTION
# Authorization: Basic ...https://api.tosspayments.com/v2/sellers
# Headers
# Content-Type: text/plain
# TossPayments-api-security-mode: ENCRYPTION
# Authorization: Basic ..."""
암호화하면 텍스트는 나오고 복호화에서 cryptography.exceptions.InvalidTag 오류가 계속 나는거 보니 암호화도 잘 됐는지 의문인 상태입니다
node.js로도 해보고 다른 라이브러리도 써봤는데 동일한 오류가 계속 반복해서 나오고 있습니다
키 길이 64, hex 길이 32인 것도 확인했는데 어떤게 문제인걸까요?
"""
# 추가) Postman에서 ENCRYPTION 모드 헤더를 뺴고 보내보면 다음과 같은 응답이 옵니다
error = {
"version": "2022-11-16",
"traceId": "3ffd277fc1daf32c0f60390d81b95340",
"entityBody": null,
"entityType": null,
"error": {
"code": "INVALID_ENCRYPTION",
"message": "Invalid encryption format."
}
}"""
암호화하면 텍스트는 나오고 복호화에서 cryptography.exceptions.InvalidTag 오류가 계속 나는거 보니 암호화도 잘 됐는지 의문인 상태입니다
node.js로도 해보고 다른 라이브러리도 써봤는데 동일한 오류가 계속 반복해서 나오고 있습니다
키 길이 64, hex 길이 32인 것도 확인했는데 어떤게 문제인걸까요?
"""
# 추가) Postman에서 ENCRYPTION 모드 헤더를 뺴고 보내보면 다음과 같은 응답이 옵니다
error = {
"version": "2022-11-16",
"traceId": "3ffd277fc1daf32c0f60390d81b95340",
"entityBody": null,
"entityType": null,
"error": {
"code": "INVALID_ENCRYPTION",
"message": "Invalid encryption format."
}
}# pip install Authlib
# python version: 3.13.1
import binascii
import uuid
from datetime import datetime
from authlib.jose import JsonWebEncryption
target = {
"refSellerId": "test_id_1",
"businessType": "INDIVIDUAL",
"individual": {
"email": "email@email.com",
"name": "테스트",
"phone": "01011112222",
},
"account": {
"accountNumber": "12312312312312",
"bankCode": "90",
"holderName": "테스트",
},
}
def hexDecode(hex_key):
return binascii.unhexlify(hex_key)
def encrypt(target, hex_key):
# 보안 키 바이트로 전환
key = binascii.unhexlify(hex_key)
# JWE 헤더 생성
headers = {
"alg": "dir",
"enc": "A256GCM",
"iat": datetime.now().astimezone().isoformat(),
"nonce": str(uuid.uuid4()),
}
# Request Body 암호화
jwe = JsonWebEncryption()
encrypted = jwe.serialize_compact(headers, target.encode("utf-8"), key)
return encrypted
def decrypt(encrypted_jwe, hex_key):
# 보안 키 바이트로 전환
key = hexDecode(hex_key)
# JWE 응답 복호화
jwe = JsonWebEncryption()
decrypted = jwe.deserialize_compact(encrypted_jwe, key)
return decrypted["payload"].decode("utf-8")# pip install Authlib
# python version: 3.13.1
import binascii
import uuid
from datetime import datetime
from authlib.jose import JsonWebEncryption
target = {
"refSellerId": "test_id_1",
"businessType": "INDIVIDUAL",
"individual": {
"email": "email@email.com",
"name": "테스트",
"phone": "01011112222",
},
"account": {
"accountNumber": "12312312312312",
"bankCode": "90",
"holderName": "테스트",
},
}
def hexDecode(hex_key):
return binascii.unhexlify(hex_key)
def encrypt(target, hex_key):
# 보안 키 바이트로 전환
key = binascii.unhexlify(hex_key)
# JWE 헤더 생성
headers = {
"alg": "dir",
"enc": "A256GCM",
"iat": datetime.now().astimezone().isoformat(),
"nonce": str(uuid.uuid4()),
}
# Request Body 암호화
jwe = JsonWebEncryption()
encrypted = jwe.serialize_compact(headers, target.encode("utf-8"), key)
return encrypted
def decrypt(encrypted_jwe, hex_key):
# 보안 키 바이트로 전환
key = hexDecode(hex_key)
# JWE 응답 복호화
jwe = JsonWebEncryption()
decrypted = jwe.deserialize_compact(encrypted_jwe, key)
return decrypted["payload"].decode("utf-8")// yarn add jose
// node version: 22.13.1
// jose version: 6.1.0
import * as jose from "jose";
async function decryptJwe(payload: any) {
const securityText = process.env.KEY!;
const securityBuffer = Buffer.from(securityText, "hex");
const securityKey = new Uint8Array(securityBuffer);
const decrypted = await jose.compactDecrypt(payload, securityKey);
console.log("decrypted => ", decrypted);
const plainText = new TextDecoder().decode(decrypted.plaintext);
const object = JSON.parse(plainText);
console.log("object => ", object);
return decrypted;
}// yarn add jose
// node version: 22.13.1
// jose version: 6.1.0
import * as jose from "jose";
async function decryptJwe(payload: any) {
const securityText = process.env.KEY!;
const securityBuffer = Buffer.from(securityText, "hex");
const securityKey = new Uint8Array(securityBuffer);
const decrypted = await jose.compactDecrypt(payload, securityKey);
console.log("decrypted => ", decrypted);
const plainText = new TextDecoder().decode(decrypted.plaintext);
const object = JSON.parse(plainText);
console.log("object => ", object);
return decrypted;
}