import requests
import json
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import base64
game_key = <GAME_KEY>
secret_key = <SECRET_KEY>
aes = AES.new(secret_key, AES.MODE_ECB)
url = "https://clcs.qpyou.cn/chat/api/v1/send-chat-log"
## 메시지 암호화
def encrypt_msg(room_num:int,lang_code:int,chat_msg:str,chat_mode:int,channel_user_id:int) -> str:
json_data = {
"time_stamp":"2023-02-20T10:22:10+09:00", ## datetime.now().isoformat()
"room_num":room_num,
"lang_code":lang_code,
"chat_msg":chat_msg,
"chat_mode":chat_mode,
"channel_user_id":channel_user_id
}
json_str = json.dumps(json_data)
padded_str = pad(json_str.encode("utf-8"),AES.block_size)
encrypted_data = aes.encrypt(padded_str)
encrypted_data_b64 = base64.b64encode(encrypted_data).decode("utf-8")
return encrypted_data_b64
## Json 정의
json_payload = {
"game_key": game_key,
"chat_log": {
"chat_count": N, ## msg_array 리스트 사이즈
"msg_array": [
{
"time_stamp":"2023-02-20T10:22:10+09:00",
"msg":encrypt_msg(room_num,lang_code,.......)
},
{
"time_stamp":"2023-02-20T10:22:10+09:00",
"msg":encrypt_msg(room_num,lang_code,.......)
}, ...
]
}
}
def call_curl_post(url, json_payload):
headers = {'Content-type': 'application/json'}
response = requests.post(url, data=json.dumps(json_payload), headers=headers)
## 함수 호출
call_curl_post(url=url, json_payload)
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import net.minidev.json.JSONObject;
public class SampleCode {
private String gameKey = <GAME_KEY>;
private String secretKey = <SECRET_KEY>;
private String url = "https://clcs.qpyou.cn/chat/api/v1/send-chat-log"
public void callCurlPost() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> parameters = new HashMap<>();
parameters.put("game_key", gameKey);
parameters.put("chat_log", "{"chat_count" : N //msg_array 리스트 사이즈,"msg_array":[{"time_stamp":"2023-02-20T10:22:10+09:00", "msg":encrypt_msg(,,....)},{"time_stamp":"2023-02-20T10:22:10+09:00", "msg":encrypt_msg(,,....)}, ...]}");
JSONObject jsonParam = getJsonStringFromMap(parameters);
HttpEntity<JSONObject> requestEntity = new HttpEntity<>(jsonParam, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
System.out.println("Response status code: " + response.getStatusCode());
System.out.println("Response body: " + response.getBody());
}
// 메시지 암호화
private String encrypt_msg(int room_num, int lang_code, String chat_msg, int chat_mode, int channel_user_id) throws Exception {
Map<String,Object> map = new HashMap<String,Object>();
map.put("time_stamp", "2023-02-20T10:22:10+09:00"); //## "%Y-%m-%d %H:%M:%S",
map.put("room_num", room_num);
map.put("lang_code", lang_code);
map.put("chat_msg", chat_msg);
map.put("chat_mode", chat_mode);
map.put("channel_user_id", channel_user_id);
JSONObject encrypt_json = new JSONObject(map);
String encrypt_str = encrypt_json.toJSONString();
return encrypt(encrypt_str, secret_key);
}
// 암호화
private String encrypt(String plainText, String key) throws Exception {
byte[] keyBytes = key.getBytes("UTF-8");
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
return Base64.encodeBase64String(encryptedBytes);
}
private static JSONObject getJsonStringFromMap( Map<String, Object> map )
{
JSONObject jsonObject = new JSONObject();
for( Map.Entry<String, Object> entry : map.entrySet() ) {
String key = entry.getKey();
Object value = entry.getValue();
jsonObject.put(key, value);
}
return jsonObject;
}
}
//TEST CALL
curl
-d '{"game_key": 회사 키,"chat_log":{"chat_count" : 1,"msg_array":[{"2023-02-20T10:22:10+09:00", "msg":"암호화된 텍스트"}]}}'
-H "Content-Type: application/json"
-X POST https://test-clcs.qpyou.cn/chat/api/v1/send-chat-log
//HEADER
POST https://clcs-qpyou.cn/v1/chat/send-chat-log
Host: clcs-qpyou.cn
User-Agent: curl/7.43.0
Accept: */*
Content-Type: application/json
//BODY
{
"game_key":"abcdefghijklmnop",
"chat_log":{
"chat_count" : 2,
"msg_array":[
{
"time_stamp":"2023-02-20T10:22:10+09:00",
"msg":"yBDyG1rCthbkXCSpSgGM+ZfHs/B3bZtnxq7f6BOPX2c1Bk8xRAwF6HbkDeN/9prpqeD......"
},
{
"time_stamp":"2023-02-20T10:22:10+09:00",
"msg":"yBDyG1rCthbkXCSpSgGM+ZfHs/B3bZtnxq7f6BOPX2c1Bk8xRAwF6HbkDeN....."
}
]
}
}
+ 결과